Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#88] Fix query params array encoding #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/RealHTTP/Client/HTTPRequest/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,7 @@ public extension HTTPRequest {
let paramsData = HTTPBody.URLParametersData(parameters,
boolEncoding: boolEncoding,
arrayEncoding: arrayEncoding)
paramsData.encodedParametersToDictionary().forEach { item in
add(queryItem: URLQueryItem(name: item.key, value: item.value))
}
add(queryItems: paramsData.encodedParametersToURLQueryItems())
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ extension HTTPBody {
return components
}

/// Creates an array of `URLQueryItem` from the contained parameters.
///
/// - Returns: [URLQueryItem]
internal func encodedParametersToURLQueryItems() -> [URLQueryItem] {
guard let parameters = self.parameters, parameters.isEmpty == false else {
return []
}

var queryItems = [URLQueryItem]()

for key in parameters.keys.sorted(by: <) {
let value = parameters[key]!
let results = encodeKey(key, withValue: value)
for result in results {
queryItems.append(URLQueryItem(name: result.0, value: result.1))
}
}

return queryItems
}

/// Encode a single object according to settings.
///
/// - Parameters:
Expand Down
22 changes: 22 additions & 0 deletions Tests/RealHTTPTests/Requests+Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ class RequestsTests: XCTestCase {
XCTAssert(parsedParams.params("p4").count == 2, "Failed to encode array")
}

func test_queryParametersArrayEncoding() {
let req = HTTPRequest {
$0.path = "/some"
$0.add(parameters: ["param": [1, 2]], arrayEncoding: .withBrackets)
$0.add(parameters: ["anotherParam": [1, 2]], arrayEncoding: .noBrackets)
}

guard let queryItems = req.query else {
return XCTFail("Query Items not found")
}

XCTAssertEqual(queryItems.count, 4)
XCTAssertEqual(queryItems[0].name, "param[]".queryEscaped)
XCTAssertEqual(queryItems[0].value, "1")
XCTAssertEqual(queryItems[1].name, "param[]".queryEscaped)
XCTAssertEqual(queryItems[1].value, "2")
XCTAssertEqual(queryItems[2].name, "anotherParam".queryEscaped)
XCTAssertEqual(queryItems[2].value, "1")
XCTAssertEqual(queryItems[3].name, "anotherParam".queryEscaped)
XCTAssertEqual(queryItems[3].value, "2")
}

func test_streamUpload() async throws {
setupStubber(echo: true)
defer { stopStubber() }
Expand Down
Loading