Skip to content

Commit

Permalink
Removed duplicate parameter encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Niedertscheider committed Oct 9, 2021
1 parent cec6f2f commit 4b56a32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Postie/Encoder/RequestEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 18 additions & 2 deletions Tests/PostieTests/RequestPathParameterCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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<T: Encodable>(request: T, file: StaticString = #filePath, line: UInt = #line) -> URLRequest? {
let encoder = RequestEncoder(baseURL: baseURL)
let encoded: URLRequest
Expand Down

0 comments on commit 4b56a32

Please sign in to comment.