瀏覽代碼

Switched authorizationHeader static method over to returning optional tuple.

In the case that utf8 encoding fails, this method should return `nil`, not an empty dictionary. It’s also strange to use a dictionary to return a single key-value pair in the success case. By switching to an optional tuple, the caller must be more explicit about handling the `nil` case making the call safer.
Christian Noon 9 年之前
父節點
當前提交
612abecb13
共有 3 個文件被更改,包括 14 次插入9 次删除
  1. 5 5
      Source/Request.swift
  2. 7 2
      Tests/AuthenticationTests.swift
  3. 2 2
      Tests/SessionManagerTests.swift

+ 5 - 5
Source/Request.swift

@@ -178,18 +178,18 @@ open class Request {
         return self
     }
 
-    /// Returns a base64 encoded basic authentication credential as an authorization header dictionary.
+    /// Returns a base64 encoded basic authentication credential as an authorization header tuple.
     ///
     /// - parameter user:     The user.
     /// - parameter password: The password.
     ///
-    /// - returns: A dictionary with Authorization key and credential value or empty dictionary if encoding fails.
-    open static func authorizationHeaderFrom(user: String, password: String) -> [String: String] {
-        guard let data = "\(user):\(password)".data(using: String.Encoding.utf8) else { return [:] }
+    /// - returns: A tuple with Authorization header and credential value if encoding succeeds, `nil` otherwise.
+    open static func authorizationHeaderFrom(user: String, password: String) -> (key: String, value: String)? {
+        guard let data = "\(user):\(password)".data(using: .utf8) else { return nil }
 
         let credential = data.base64EncodedString(options: [])
 
-        return ["Authorization": "Basic \(credential)"]
+        return (key: "Authorization", value: "Basic \(credential)")
     }
 
     // MARK: Progress

+ 7 - 2
Tests/AuthenticationTests.swift

@@ -125,17 +125,22 @@ class BasicAuthenticationTestCase: AuthenticationTestCase {
 
     func testHiddenHTTPBasicAuthentication() {
         // Given
-        let authorizationHeader = Request.authorizationHeaderFrom(user: user, password: password)
         let urlString = "http://httpbin.org/hidden-basic-auth/\(user)/\(password)"
         let expectation = self.expectation(description: "\(urlString) 200")
 
+        var headers: [String: String]?
+
+        if let authorizationHeader = Request.authorizationHeaderFrom(user: user, password: password) {
+            headers = [authorizationHeader.key: authorizationHeader.value]
+        }
+
         var request: URLRequest?
         var response: HTTPURLResponse?
         var data: Data?
         var error: Error?
 
         // When
-        manager.request(urlString, withMethod: .get, headers: authorizationHeader)
+        manager.request(urlString, withMethod: .get, headers: headers)
             .response { responseRequest, responseResponse, responseData, responseError in
                 request = responseRequest
                 response = responseResponse

+ 2 - 2
Tests/SessionManagerTests.swift

@@ -55,8 +55,8 @@ class SessionManagerTestCase: BaseTestCase {
             adaptedCount += 1
 
             if shouldApplyAuthorizationHeader && adaptedCount > 1 {
-                Request.authorizationHeaderFrom(user: "user", password: "password").forEach { header, value in
-                    urlRequest.setValue(value, forHTTPHeaderField: header)
+                if let header = Request.authorizationHeaderFrom(user: "user", password: "password") {
+                    urlRequest.setValue(header.value, forHTTPHeaderField: header.key)
                 }
             }