Browse Source

Content Type validation now always succeeds when server data is nil or empty.

Christian Noon 10 years ago
parent
commit
e0c611a400
2 changed files with 29 additions and 0 deletions
  1. 2 0
      Source/Validation.swift
  2. 27 0
      Tests/ValidationTests.swift

+ 2 - 0
Source/Validation.swift

@@ -130,6 +130,8 @@ extension Request {
     */
     public func validate<S : SequenceType where S.Generator.Element == String>(contentType acceptableContentTypes: S) -> Self {
         return validate { _, response in
+            guard let validData = self.delegate.data where validData.length > 0 else { return .Success }
+
             if let
                 responseContentType = response.MIMEType,
                 responseMIMEType = MIMEType(responseContentType)

+ 27 - 0
Tests/ValidationTests.swift

@@ -208,6 +208,33 @@ class ContentTypeValidationTestCase: BaseTestCase {
         }
     }
 
+    func testThatValidationForRequestWithNoAcceptableContentTypeResponseSucceedsWhenNoDataIsReturned() {
+        // Given
+        let URLString = "https://httpbin.org/status/204"
+        let expectation = expectationWithDescription("request should succeed and return no data")
+
+        var error: NSError?
+
+        // When
+        Alamofire.request(.GET, URLString)
+            .validate(contentType: [])
+            .response { _, response, data, responseError in
+                error = responseError
+
+                print(response)
+                if let data = data {
+                    print(String(data: data, encoding: NSUTF8StringEncoding)!)
+                }
+
+                expectation.fulfill()
+            }
+
+        waitForExpectationsWithTimeout(timeout, handler: nil)
+
+        // Then
+        XCTAssertNil(error, "error should be nil")
+    }
+
     func testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceedsWhenResponseIsNil() {
         // Given
         class MockManager: Manager {