Skip to content

Commit

Permalink
Merge pull request #68 from strvcom/fix/url-query-key-percent-encoding
Browse files Browse the repository at this point in the history
[fix] URL query keys percent encoding
  • Loading branch information
cejanen authored Apr 10, 2024
2 parents 8c2f0d2 + d329c5f commit de68c75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/Networking/Misc/URLQueryItem+PercentEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extension URLQueryItem {

func percentEncoded() -> URLQueryItem {
var newQueryItem = self
newQueryItem.value = value?
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
newQueryItem.name = name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? name
newQueryItem.value = value?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

return newQueryItem
}
Expand Down
15 changes: 9 additions & 6 deletions Tests/NetworkingTests/URLParametersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ final class URLParametersTests: XCTestCase {
}

func testDefaultEncoding() async throws {
let nameString = "name]surname"
let namePercentEncodedString = "name%5Dsurname"

let router = Router.urlParameters(["name": nameString])
let keyString = "name[first]"
let keyPercentEncodedString = "name%5Bfirst%5D"
let valueString = "name]surname"
let valuePercentEncodedString = "name%5Dsurname"

let router = Router.urlParameters([keyString: valueString])
let request = try router.asRequest()

guard let url = request.url else {
Expand All @@ -44,9 +46,10 @@ final class URLParametersTests: XCTestCase {
}

let queryItems = percentEncodedQueryItems(from: url)

XCTAssertEqual(
queryItems.first(where: { $0.name == "name" })?.value,
namePercentEncodedString
queryItems.first(where: { $0.name == keyPercentEncodedString })?.value,
valuePercentEncodedString
)
}

Expand Down

0 comments on commit de68c75

Please sign in to comment.