Browse Source

Added to for wildcard validation that contains response with nil MIME type.

This is the test logic required to ensure that the fix in PR #598 works properly to solve the issues posted in Issue #510.
Christian Noon 10 years ago
parent
commit
1f747546ba
1 changed files with 79 additions and 1 deletions
  1. 79 1
      Tests/ValidationTests.swift

+ 79 - 1
Tests/ValidationTests.swift

@@ -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
 
@@ -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: -