Browse Source

Only set cookies in debugPrintable when appropriate.

Alex Plescan 10 years ago
parent
commit
c40d7907ea
2 changed files with 41 additions and 6 deletions
  1. 8 6
      Source/Request.swift
  2. 33 0
      Tests/RequestTests.swift

+ 8 - 6
Source/Request.swift

@@ -418,12 +418,14 @@ extension Request: DebugPrintable {
         // Temporarily disabled on OS X due to build failure for CocoaPods
         // See https://github.com/CocoaPods/swift/issues/24
         #if !os(OSX)
-            if let cookieStorage = session.configuration.HTTPCookieStorage,
-                cookies = cookieStorage.cookiesForURL(URL!) as? [NSHTTPCookie]
-                where !cookies.isEmpty
-            {
-                let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" }
-                components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
+            if session.configuration.HTTPShouldSetCookies {
+                if let cookieStorage = session.configuration.HTTPCookieStorage,
+                    cookies = cookieStorage.cookiesForURL(URL!) as? [NSHTTPCookie]
+                    where !cookies.isEmpty
+                {
+                    let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" }
+                    components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
+                }
             }
         #endif
 

+ 33 - 0
Tests/RequestTests.swift

@@ -196,6 +196,16 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
         return manager
     }()
 
+    let managerDisallowingCookies: Alamofire.Manager = {
+        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
+        configuration.HTTPShouldSetCookies = false
+
+        let manager = Alamofire.Manager(configuration: configuration)
+        manager.startRequestsImmediately = false
+
+        return manager
+    }()
+
     // MARK: Tests
 
     func testGETRequestDebugDescription() {
@@ -269,6 +279,29 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
         #endif
     }
 
+    func testPOSTRequestWithCookiesDisabledDebugDescription() {
+        // Given
+        let URLString = "http://httpbin.org/post"
+
+        let properties = [
+            NSHTTPCookieDomain: "httpbin.org",
+            NSHTTPCookiePath: "/post",
+            NSHTTPCookieName: "foo",
+            NSHTTPCookieValue: "bar",
+        ]
+
+        let cookie = NSHTTPCookie(properties: properties)!
+        managerDisallowingCookies.session.configuration.HTTPCookieStorage?.setCookie(cookie)
+
+        // When
+        let request = managerDisallowingCookies.request(.POST, URLString)
+        let components = cURLCommandComponents(request)
+
+        // Then
+        let cookieComponents = components.filter { $0 == "-b" }
+        XCTAssertTrue(cookieComponents.isEmpty, "command should not contain -b flag")
+    }
+
     // MARK: Test Helper Methods
 
     private func cURLCommandComponents(request: Request) -> [String] {