From d329c5fcc6d52672b148f026deebfb857a47e136 Mon Sep 17 00:00:00 2001 From: Matej Molnar Date: Wed, 10 Apr 2024 16:21:03 +0200 Subject: [PATCH] [fix] add percent encoding also for keys in url queries --- .../Misc/URLQueryItem+PercentEncoding.swift | 4 ++-- Tests/NetworkingTests/URLParametersTests.swift | 15 +++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Sources/Networking/Misc/URLQueryItem+PercentEncoding.swift b/Sources/Networking/Misc/URLQueryItem+PercentEncoding.swift index 27577fe8..1f31d63c 100644 --- a/Sources/Networking/Misc/URLQueryItem+PercentEncoding.swift +++ b/Sources/Networking/Misc/URLQueryItem+PercentEncoding.swift @@ -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 } diff --git a/Tests/NetworkingTests/URLParametersTests.swift b/Tests/NetworkingTests/URLParametersTests.swift index 5b6244c1..b090254e 100644 --- a/Tests/NetworkingTests/URLParametersTests.swift +++ b/Tests/NetworkingTests/URLParametersTests.swift @@ -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 { @@ -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 ) }