Explorar el Código

Added tests verifying configuration headers are sent with all configuration types.

Christian Noon hace 10 años
padre
commit
10f94788f0
Se han modificado 1 ficheros con 87 adiciones y 0 borrados
  1. 87 0
      Tests/ManagerTests.swift

+ 87 - 0
Tests/ManagerTests.swift

@@ -88,3 +88,90 @@ class ManagerTestCase: BaseTestCase {
         XCTAssertNil(manager, "manager should be nil")
     }
 }
+
+// MARK: -
+
+class ManagerConfigurationHeadersTestCase: BaseTestCase {
+    enum ConfigurationType {
+        case Default, Ephemeral, Background
+    }
+
+    func testThatDefaultConfigurationHeadersAreSentWithRequest() {
+        // Given, When, Then
+        executeAuthorizationHeaderTestForConfigurationType(.Default)
+    }
+
+    func testThatEphemeralConfigurationHeadersAreSentWithRequest() {
+        // Given, When, Then
+        executeAuthorizationHeaderTestForConfigurationType(.Ephemeral)
+    }
+
+    func testThatBackgroundConfigurationHeadersAreSentWithRequest() {
+        // Given, When, Then
+        executeAuthorizationHeaderTestForConfigurationType(.Background)
+    }
+
+    private func executeAuthorizationHeaderTestForConfigurationType(type: ConfigurationType) {
+        // Given
+        let manager: Manager = {
+            let configuration: NSURLSessionConfiguration = {
+                let configuration: NSURLSessionConfiguration
+
+                switch type {
+                case .Default:
+                    configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
+                case .Ephemeral:
+                    configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
+                case .Background:
+                    let identifier = "com.alamofire.test.manager-configuration-tests"
+                    configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
+                }
+
+                var headers = Alamofire.Manager.defaultHTTPHeaders
+                headers["Authorization"] = "Bearer 123456"
+                configuration.HTTPAdditionalHeaders = headers
+
+                return configuration
+            }()
+
+            return Manager(configuration: configuration)
+        }()
+
+        let expectation = expectationWithDescription("request should complete successfully")
+
+        var request: NSURLRequest?
+        var response: NSHTTPURLResponse?
+        var result: Result<AnyObject>?
+
+        // When
+        manager.request(.GET, "https://httpbin.org/headers")
+            .responseJSON { responseRequest, responseResponse, responseResult in
+                request = responseRequest
+                response = responseResponse
+                result = responseResult
+
+                expectation.fulfill()
+        }
+
+        waitForExpectationsWithTimeout(defaultTimeout, handler: nil)
+
+        // Then
+        XCTAssertNotNil(request, "request should not be nil")
+        XCTAssertNotNil(response, "response should not be nil")
+
+        if let result = result {
+            XCTAssertTrue(result.isSuccess, "result should be a success")
+
+            if let
+                headers = result.value?["headers" as NSString] as? [String: String],
+                authorization = headers["Authorization"]
+            {
+                XCTAssertEqual(authorization, "Bearer 123456", "authorization header value does not match")
+            } else {
+                XCTFail("failed to extract authorization header value")
+            }
+        } else {
+            XCTFail("result should not be nil")
+        }
+    }
+}