From 4b56a3231ce593ec71d16f6a63336460d3d93c06 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sun, 10 Oct 2021 00:58:48 +0200 Subject: [PATCH] Removed duplicate parameter encoding --- Sources/Postie/Encoder/RequestEncoding.swift | 2 +- .../RequestPathParameterCodingTests.swift | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Sources/Postie/Encoder/RequestEncoding.swift b/Sources/Postie/Encoder/RequestEncoding.swift index eda0dbb..fc6d7a5 100644 --- a/Sources/Postie/Encoder/RequestEncoding.swift +++ b/Sources/Postie/Encoder/RequestEncoding.swift @@ -85,7 +85,7 @@ internal class RequestEncoding: Encoder { return pathParameters.reduce(path) { partialResult, parameter in let key = parameter.key let value = parameter.value - let replacement = value.serialized.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? value.serialized + let replacement = value.serialized return partialResult.replacingOccurrences(of: "{\(key)}", with: replacement) } diff --git a/Tests/PostieTests/RequestPathParameterCodingTests.swift b/Tests/PostieTests/RequestPathParameterCodingTests.swift index 451c412..17248ae 100644 --- a/Tests/PostieTests/RequestPathParameterCodingTests.swift +++ b/Tests/PostieTests/RequestPathParameterCodingTests.swift @@ -109,7 +109,7 @@ class RequestPathParameterCodingTests: XCTestCase { guard let urlRequest = encodeRequest(request: request) else { return } - XCTAssertEqual(urlRequest.url?.path, "/some%20name") + XCTAssertEqual(urlRequest.url?.absoluteString, "https://local.url/some%20name") } func testEncoding_stringParameterCustomNaming_shouldUseCustomName() { @@ -125,7 +125,7 @@ class RequestPathParameterCodingTests: XCTestCase { guard let urlRequest = encodeRequest(request: request) else { return } - XCTAssertEqual(urlRequest.url?.path, "/some%20name") + XCTAssertEqual(urlRequest.url?.absoluteString, "https://local.url/some%20name") } func testEncoding_intParameterCustomNaming_shouldUseCustomName() { @@ -205,6 +205,22 @@ class RequestPathParameterCodingTests: XCTestCase { XCTAssertEqual(urlRequest.url?.path, "/321") } + func testEncoding_paramValueContainsUnescapedCharacters_shouldEscapeCharacters() { + struct Request: Encodable { + + typealias Response = EmptyResponse + + @RequestPath var path = "/{id}" + @RequestPathParameter var id: String? + } + var request = Request() + request.id = "{ABC}" + guard let urlRequest = encodeRequest(request: request) else { + return + } + XCTAssertEqual(urlRequest.url?.absoluteString, "https://local.url/%7BABC%7D") + } + internal func encodeRequest(request: T, file: StaticString = #filePath, line: UInt = #line) -> URLRequest? { let encoder = RequestEncoder(baseURL: baseURL) let encoded: URLRequest