Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 2e67d20

Browse files
committed
Do not add default query if it exists in the original URL
1 parent 427ac61 commit 2e67d20

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

WordPressKit/HTTPRequestBuilder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ final class HTTPRequestBuilder {
153153
// Add default query items if they don't exist in `appendedQuery`.
154154
var newQuery = appendedQuery
155155
if !defaultQuery.isEmpty {
156+
let allQuery = (original.queryItems ?? []) + newQuery
156157
let toBeAdded = defaultQuery.filter { item in
157-
!newQuery.contains(where: { $0.name == item.name})
158+
!allQuery.contains(where: { $0.name == item.name})
158159
}
159160
newQuery.append(contentsOf: toBeAdded)
160161
}

WordPressKitTests/Utilities/HTTPRequestBuilderTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ class HTTPRequestBuilderTests: XCTestCase {
205205
try XCTAssertEqual(builder.query(name: "foo", value: "bar").build().url?.query, "locale=zh&foo=bar")
206206
}
207207

208+
func testDefaultQueryExistsInOriginalURL() throws {
209+
let url = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org/hello?locale=foo")!)
210+
.query(defaults: [URLQueryItem(name: "locale", value: "en")])
211+
.build()
212+
.url
213+
214+
XCTAssertEqual(url?.query, "locale=foo")
215+
}
216+
208217
func testJSONBody() throws {
209218
var request = try HTTPRequestBuilder(url: URL(string: "https://wordpress.org")!)
210219
.method(.post)

0 commit comments

Comments
 (0)