Преглед изворни кода

[Issue #226] Fixing optional cookie entry in cURL output

Mattt Thompson пре 11 година
родитељ
комит
fb5592e01b
2 измењених фајлова са 25 додато и 1 уклоњено
  1. 1 1
      Source/Alamofire.swift
  2. 24 0
      Tests/RequestTests.swift

+ 1 - 1
Source/Alamofire.swift

@@ -1257,7 +1257,7 @@ extension Request: DebugPrintable {
         if let cookieStorage = session.configuration.HTTPCookieStorage {
             if let cookies = cookieStorage.cookiesForURL(URL) as? [NSHTTPCookie] {
                 if !cookies.isEmpty {
-                    let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value);" }
+                    let string = cookies.reduce(""){ $0 + "\($1.name)=\($1.value ?? String());" }
                     components.append("-b \"\(string.substringToIndex(string.endIndex.predecessor()))\"")
                 }
             }

+ 24 - 0
Tests/RequestTests.swift

@@ -127,4 +127,28 @@ class AlamofireRequestDebugDescriptionTestCase: XCTestCase {
         XCTAssert(request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil)
         XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
     }
+
+    func testPOSTRequestWithCookieDebugDescription() {
+        let URL = "http://httpbin.org/post"
+
+        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
+
+        let properties = [
+            NSHTTPCookieDomain: "httpbin.org",
+            NSHTTPCookiePath: "/post",
+            NSHTTPCookieName: "foo",
+            NSHTTPCookieValue: "bar",
+        ]
+        let cookie = NSHTTPCookie(properties: properties)!
+        configuration.HTTPCookieStorage?.setCookie(cookie)
+
+        let manager = Alamofire.Manager(configuration: configuration)
+        let request = manager.request(.POST, URL)
+        let components = cURLCommandComponents(request)
+
+        XCTAssert(components[0..<3] == ["$", "curl", "-i"], "components should be equal")
+        XCTAssert(components[3..<5] == ["-X", "POST"], "command should contain explicit -X flag")
+        XCTAssert(components[5..<7] == ["-b", "\"foo=bar\""], "command should contain -b flag")
+        XCTAssert(components.last! == "\"\(URL)\"", "URL component should be equal")
+    }
 }