Skip to content

Commit

Permalink
Merge pull request Alamofire#662 from Alamofire/test/no_content_type_…
Browse files Browse the repository at this point in the history
…wildcard_validation
  • Loading branch information
cnoon committed Aug 11, 2015
2 parents 6b61df2 + 1f74754 commit 362a127
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion Tests/ValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Alamofire
@testable import Alamofire
import Foundation
import XCTest

Expand Down Expand Up @@ -197,6 +197,84 @@ class ContentTypeValidationTestCase: BaseTestCase {
XCTAssertEqual(error.code, Error.Code.ContentTypeValidationFailed.rawValue, "code should be content type validation failure")
}
}

func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceedsWhenResponseIsNil() {
// Given
class MockManager: Manager {
override func request(URLRequest: URLRequestConvertible) -> Request {
var dataTask: NSURLSessionDataTask!

dispatch_sync(queue) {
dataTask = self.session.dataTaskWithRequest(URLRequest.URLRequest)
}

let request = MockRequest(session: session, task: dataTask)
delegate[request.delegate.task] = request.delegate

if startRequestsImmediately {
request.resume()
}

return request
}
}

class MockRequest: Request {
override var response: NSHTTPURLResponse? {
return MockHTTPURLResponse(
URL: NSURL(string: request!.URLString)!,
statusCode: 204,
HTTPVersion: "HTTP/1.1",
headerFields: nil
)
}
}

class MockHTTPURLResponse: NSHTTPURLResponse {
override var MIMEType: String? { return nil }
}

let manager: Manager = {
let configuration: NSURLSessionConfiguration = {
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders

return configuration
}()

return MockManager(configuration: configuration)
}()

let URLString = "https://httpbin.org/delete"
let expectation = expectationWithDescription("request should be stubbed and return 204 status code")

var response: NSHTTPURLResponse?
var data: NSData?
var error: NSError?

// When
manager.request(.DELETE, URLString)
.validate(contentType: ["*/*"])
.response { _, responseResponse, responseData, responseError in
response = responseResponse
data = responseData
error = responseError

expectation.fulfill()
}

waitForExpectationsWithTimeout(defaultTimeout, handler: nil)

// Then
XCTAssertNotNil(response, "response should not be nil")
XCTAssertNotNil(data, "data should not be nil")
XCTAssertNil(error, "error should be nil")

if let response = response {
XCTAssertEqual(response.statusCode, 204, "response status code should be 204")
XCTAssertNil(response.MIMEType, "response mime type should be nil")
}
}
}

// MARK: -
Expand Down

0 comments on commit 362a127

Please sign in to comment.