소스 검색

Fixed issue where internal quotes were not escaped correctly in cURL output.

Christian Noon 9 년 전
부모
커밋
f443cd8a76
2개의 변경된 파일16개의 추가작업 그리고 3개의 파일을 삭제
  1. 3 1
      Source/Request.swift
  2. 13 2
      Tests/RequestTests.swift

+ 3 - 1
Source/Request.swift

@@ -550,7 +550,9 @@ extension Request: CustomDebugStringConvertible {
             HTTPBodyData = request.HTTPBody,
             HTTPBody = String(data: HTTPBodyData, encoding: NSUTF8StringEncoding)
         {
-            let escapedBody = HTTPBody.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
+            var escapedBody = HTTPBody.stringByReplacingOccurrencesOfString("\\\"", withString: "\\\\\"")
+            escapedBody = escapedBody.stringByReplacingOccurrencesOfString("\"", withString: "\\\"")
+
             components.append("-d \"\(escapedBody)\"")
         }
 

+ 13 - 2
Tests/RequestTests.swift

@@ -566,21 +566,32 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
         // Given
         let URLString = "https://httpbin.org/post"
 
+        let parameters = [
+            "foo": "bar",
+            "fo\"o": "b\"ar",
+            "f'oo": "ba'r"
+        ]
+
         // When
-        let request = manager.request(.POST, URLString, parameters: ["foo": "bar"], encoding: .JSON)
+        let request = manager.request(.POST, URLString, parameters: parameters, encoding: .JSON)
         let components = cURLCommandComponents(request)
 
         // Then
         XCTAssertEqual(components[0..<3], ["$", "curl", "-i"], "components should be equal")
         XCTAssertEqual(components[3..<5], ["-X", "POST"], "command should contain explicit -X flag")
+
         XCTAssertTrue(
             request.debugDescription.rangeOfString("-H \"Content-Type: application/json\"") != nil,
             "command should contain 'application/json' Content-Type"
         )
+
+        let expectedBody = "-d \"{\\\"f'oo\\\":\\\"ba'r\\\",\\\"fo\\\\\\\"o\\\":\\\"b\\\\\\\"ar\\\",\\\"foo\\\":\\\"bar\\\"}\""
+
         XCTAssertTrue(
-            request.debugDescription.rangeOfString("-d \"{\\\"foo\\\":\\\"bar\\\"}\"") != nil,
+            request.debugDescription.rangeOfString(expectedBody) != nil,
             "command data should contain JSON encoded parameters"
         )
+
         XCTAssertEqual(components.last ?? "", "\"\(URLString)\"", "URL component should be equal")
     }