Skip to content

Commit

Permalink
Do not add default query if it exists in the original URL
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Feb 21, 2024
1 parent 427ac61 commit 2e67d20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion WordPressKit/HTTPRequestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ final class HTTPRequestBuilder {
// Add default query items if they don't exist in `appendedQuery`.
var newQuery = appendedQuery
if !defaultQuery.isEmpty {
let allQuery = (original.queryItems ?? []) + newQuery
let toBeAdded = defaultQuery.filter { item in
!newQuery.contains(where: { $0.name == item.name})
!allQuery.contains(where: { $0.name == item.name})
}
newQuery.append(contentsOf: toBeAdded)
}
Expand Down
9 changes: 9 additions & 0 deletions WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ class HTTPRequestBuilderTests: XCTestCase {
try XCTAssertEqual(builder.query(name: "foo", value: "bar").build().url?.query, "locale=zh&foo=bar")
}

func testDefaultQueryExistsInOriginalURL() throws {
let url = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/hello?locale=foo")!)
.query(defaults: [URLQueryItem(name: "locale", value: "en")])
.build()
.url

XCTAssertEqual(url?.query, "locale=foo")
}

func testJSONBody() throws {
var request = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org")!)
.method(.post)
Expand Down

0 comments on commit 2e67d20

Please sign in to comment.