浏览代码

Escape quotes in headers when building cURLRepresentation (#2474)

Jonah 7 年之前
父节点
当前提交
6359d9535c
共有 2 个文件被更改,包括 14 次插入1 次删除
  1. 2 1
      Source/Request.swift
  2. 12 0
      Tests/RequestTests.swift

+ 2 - 1
Source/Request.swift

@@ -333,7 +333,8 @@ extension Request: CustomDebugStringConvertible {
         }
 
         for (field, value) in headers {
-            components.append("-H \"\(field): \(value)\"")
+            let escapedValue = String(describing: value).replacingOccurrences(of: "\"", with: "\\\"")
+            components.append("-H \"\(field): \(escapedValue)\"")
         }
 
         if let httpBodyData = request.httpBody, let httpBody = String(data: httpBodyData, encoding: .utf8) {

+ 12 - 0
Tests/RequestTests.swift

@@ -529,6 +529,18 @@ class RequestDebugDescriptionTestCase: BaseTestCase {
         XCTAssertEqual(components.last, "\"\(urlString)\"")
     }
 
+    func testGETRequestWithJSONHeaderDebugDescription() {
+        // Given
+        let urlString = "https://httpbin.org/get"
+
+        // When
+        let headers: [String: String] = [ "X-Custom-Header": "{\"key\": \"value\"}" ]
+        let request = manager.request(urlString, headers: headers)
+
+        // Then
+        XCTAssertNotNil(request.debugDescription.range(of: "-H \"X-Custom-Header: {\\\"key\\\": \\\"value\\\"}\""))
+    }
+
     func testGETRequestWithDuplicateHeadersDebugDescription() {
         // Given
         let urlString = "https://httpbin.org/get"