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

Commit 4df2b39

Browse files
authored
Do not add default query if it exists in the original URL (#733)
2 parents d67877f + 67cfbe2 commit 4df2b39

File tree

4 files changed

+78
-234
lines changed

4 files changed

+78
-234
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 testDefaultQueryDoesNotOverriedQueryItemInOriginalURL() 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)
Lines changed: 56 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -1,272 +1,98 @@
11
import Foundation
22
import XCTest
3+
import OHHTTPStubs
34

45
import WordPressShared
56
@testable import WordPressKit
67

78
extension WordPressComRestApiTests {
89

9-
func testThatAppendingLocaleWorks() {
10-
// Given
11-
let path = "/path/path"
12-
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
10+
func testAddsLocaleToURLQueryByDefault() async throws {
11+
var request: URLRequest?
12+
stub(condition: { _ in true }, response: {
13+
request = $0
14+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
15+
})
1316

14-
// When
1517
let api = WordPressComRestApi()
16-
let localeAppendedPath = api.buildRequestURLFor(path: path)
17-
18-
// Then
19-
XCTAssertNotNil(localeAppendedPath)
20-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
21-
XCTAssertNotNil(actualURL)
22-
23-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
24-
XCTAssertNotNil(actualURLComponents)
25-
26-
let expectedPath = path
27-
let actualPath = actualURLComponents!.path
28-
XCTAssertEqual(expectedPath, actualPath)
29-
30-
let actualQueryItems = actualURLComponents!.queryItems
31-
XCTAssertNotNil(actualQueryItems)
32-
33-
let expectedQueryItemCount = 1
34-
let actualQueryItemCount = actualQueryItems!.count
35-
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
36-
37-
let actualQueryItem = actualQueryItems!.first
38-
XCTAssertNotNil(actualQueryItem!)
39-
40-
let actualQueryItemKey = actualQueryItem!.name
41-
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault
42-
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey)
18+
let _ = await api.perform(.get, URLString: "/path/path")
4319

44-
let actualQueryItemValue = actualQueryItem!.value
45-
XCTAssertNotNil(actualQueryItemValue)
46-
47-
let expectedQueryItemValue = preferredLanguageIdentifier
48-
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!)
20+
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
21+
XCTAssertEqual(request?.url?.query, "locale=\(preferredLanguageIdentifier)")
4922
}
5023

51-
func testThatAppendingLocaleWorksWithExistingParams() {
52-
// Given
24+
func testAddsLocaleToURLQueryByDefaultAndMaintainsInputParameters() async throws {
25+
var request: URLRequest?
26+
stub(condition: { _ in true }, response: {
27+
request = $0
28+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
29+
})
30+
5331
let path = "/path/path"
5432
let params: [String: AnyObject] = [
5533
"someKey": "value" as AnyObject
5634
]
5735

58-
// When
5936
let api = WordPressComRestApi()
60-
let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: params)
61-
62-
// Then
63-
XCTAssertNotNil(localeAppendedPath)
64-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
65-
XCTAssertNotNil(actualURL)
66-
67-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
68-
XCTAssertNotNil(actualURLComponents)
69-
70-
let expectedPath = "/path/path"
71-
let actualPath = actualURLComponents!.path
72-
XCTAssertEqual(expectedPath, actualPath)
73-
74-
let actualQueryItems = actualURLComponents!.queryItems
75-
XCTAssertNotNil(actualQueryItems)
76-
77-
let expectedQueryItemCount = 1
78-
let actualQueryItemCount = actualQueryItems!.count
79-
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
80-
81-
let actualQueryString = actualURLComponents?.query
82-
XCTAssertNotNil(actualQueryString)
83-
84-
let queryStringIncludesLocale = actualQueryString!.contains(WordPressComRestApi.LocaleKeyDefault)
85-
XCTAssertTrue(queryStringIncludesLocale)
86-
}
87-
88-
func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() {
89-
// Given
90-
let preferredLanguageIdentifier = "foo"
91-
let path = "/path/path?locale=\(preferredLanguageIdentifier)"
92-
93-
// When
94-
let api = WordPressComRestApi()
95-
let localeAppendedPath = api.buildRequestURLFor(path: path)
96-
97-
// Then
98-
XCTAssertNotNil(localeAppendedPath)
99-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
100-
XCTAssertNotNil(actualURL)
101-
102-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
103-
XCTAssertNotNil(actualURLComponents)
104-
105-
let expectedPath = "/path/path"
106-
let actualPath = actualURLComponents!.path
107-
XCTAssertEqual(expectedPath, actualPath)
108-
109-
let actualQueryItems = actualURLComponents!.queryItems
110-
XCTAssertNotNil(actualQueryItems)
111-
112-
let expectedQueryItemCount = 1
113-
let actualQueryItemCount = actualQueryItems!.count
114-
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
115-
116-
let actualQueryItem = actualQueryItems!.first
117-
XCTAssertNotNil(actualQueryItem!)
118-
119-
let actualQueryItemKey = actualQueryItem!.name
120-
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault
121-
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey)
122-
123-
let actualQueryItemValue = actualQueryItem!.value
124-
XCTAssertNotNil(actualQueryItemValue)
125-
126-
let expectedQueryItemValue = preferredLanguageIdentifier
127-
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!)
128-
}
37+
let _ = await api.perform(.get, URLString: path, parameters: params)
12938

130-
func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() {
131-
// Given
132-
let inputPath = "/path/path"
133-
let expectedLocaleValue = "foo"
134-
let params: [String: AnyObject] = [
135-
WordPressComRestApi.LocaleKeyDefault: expectedLocaleValue as AnyObject
136-
]
137-
138-
// When
139-
let requestURLString = WordPressComRestApi().buildRequestURLFor(path: inputPath, parameters: params)
140-
141-
// Then
14239
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
143-
XCTAssertFalse(requestURLString!.contains(preferredLanguageIdentifier))
40+
let query = try XCTUnwrap(request?.url?.query?.split(separator: "&"))
41+
XCTAssertEqual(Set(query), Set(["locale=\(preferredLanguageIdentifier)", "someKey=value"]))
14442
}
14543

146-
func testThatLocaleIsNotAppendedWhenDisabled() {
147-
// Given
148-
let path = "/path/path"
44+
func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() async {
45+
var request: URLRequest?
46+
stub(condition: { _ in true }, response: {
47+
request = $0
48+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
49+
})
14950

150-
// When
15151
let api = WordPressComRestApi()
152-
api.appendsPreferredLanguageLocale = false
153-
let localeAppendedPath = api.buildRequestURLFor(path: path)
154-
155-
// Then
156-
XCTAssertNotNil(localeAppendedPath)
157-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
158-
XCTAssertNotNil(actualURL)
159-
160-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
161-
XCTAssertNotNil(actualURLComponents)
162-
163-
let expectedPath = path
164-
let actualPath = actualURLComponents!.path
165-
XCTAssertEqual(expectedPath, actualPath)
52+
let _ = await api.perform(.get, URLString: "/path?locale=foo")
16653

167-
let actualQueryItems = actualURLComponents!.queryItems
168-
XCTAssertNil(actualQueryItems)
54+
try XCTAssertEqual(XCTUnwrap(request?.url?.query), "locale=foo")
16955
}
17056

171-
func testThatAlternateLocaleKeyIsHonoredWhenSpecified() {
172-
// Given
173-
let path = "/path/path"
174-
let expectedKey = "foo"
175-
176-
// When
177-
let api = WordPressComRestApi(localeKey: expectedKey)
178-
let localeAppendedPath = api.buildRequestURLFor(path: path)
179-
180-
// Then
181-
XCTAssertNotNil(localeAppendedPath)
182-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
183-
XCTAssertNotNil(actualURL)
184-
185-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
186-
XCTAssertNotNil(actualURLComponents)
187-
188-
let expectedPath = path
189-
let actualPath = actualURLComponents!.path
190-
XCTAssertEqual(expectedPath, actualPath)
191-
192-
let actualQueryItems = actualURLComponents!.queryItems
193-
XCTAssertNotNil(actualQueryItems)
194-
195-
let expectedQueryItemCount = 1
196-
let actualQueryItemCount = actualQueryItems!.count
197-
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
198-
199-
let actualQueryItem = actualQueryItems!.first
200-
XCTAssertNotNil(actualQueryItem!)
57+
func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() async throws {
58+
var request: URLRequest?
59+
stub(condition: { _ in true }, response: {
60+
request = $0
61+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
62+
})
20163

202-
let actualQueryItemKey = actualQueryItem!.name
203-
XCTAssertEqual(expectedKey, actualQueryItemKey)
204-
}
205-
206-
func testThatAppendingLocaleWorksWhenPassingNilParameters() {
207-
// Given
208-
let path = "/path/path"
209-
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
210-
211-
// When
21264
let api = WordPressComRestApi()
213-
let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path, parameters: nil)
214-
215-
// Then
216-
XCTAssertNotNil(localeAppendedPath)
217-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
218-
XCTAssertNotNil(actualURL)
219-
220-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
221-
XCTAssertNotNil(actualURLComponents)
222-
223-
let expectedPath = path
224-
let actualPath = actualURLComponents!.path
225-
XCTAssertEqual(expectedPath, actualPath)
65+
let _ = await api.perform(.get, URLString: "/path", parameters: ["locale": "foo"] as [String: AnyObject])
22666

227-
let actualQueryItems = actualURLComponents!.queryItems
228-
XCTAssertNotNil(actualQueryItems)
229-
230-
let expectedQueryItemCount = 1
231-
let actualQueryItemCount = actualQueryItems!.count
232-
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
233-
234-
let actualQueryItem = actualQueryItems!.first
235-
XCTAssertNotNil(actualQueryItem!)
236-
237-
let actualQueryItemKey = actualQueryItem!.name
238-
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault
239-
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey)
240-
241-
let actualQueryItemValue = actualQueryItem!.value
242-
XCTAssertNotNil(actualQueryItemValue)
243-
244-
let expectedQueryItemValue = preferredLanguageIdentifier
245-
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!)
67+
try XCTAssertEqual(XCTUnwrap(request?.url?.query), "locale=foo")
24668
}
24769

248-
func testThatLocaleIsNotAppendedWhenDisabledAndParametersAreNil() {
249-
// Given
250-
let path = "/path/path"
70+
func testThatLocaleIsNotAppendedWhenDisabled() async {
71+
var request: URLRequest?
72+
stub(condition: { _ in true }, response: {
73+
request = $0
74+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
75+
})
25176

252-
// When
25377
let api = WordPressComRestApi()
25478
api.appendsPreferredLanguageLocale = false
255-
let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: nil)
79+
let _ = await api.perform(.get, URLString: "/path")
25680

257-
// Then
258-
XCTAssertNotNil(localeAppendedPath)
259-
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL)
260-
XCTAssertNotNil(actualURL)
81+
XCTAssertNotNil(request?.url)
82+
XCTAssertNil(request?.url?.query)
83+
}
26184

262-
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
263-
XCTAssertNotNil(actualURLComponents)
85+
func testThatAlternateLocaleKeyIsHonoredWhenSpecified() async {
86+
var request: URLRequest?
87+
stub(condition: { _ in true }, response: {
88+
request = $0
89+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
90+
})
26491

265-
let expectedPath = path
266-
let actualPath = actualURLComponents!.path
267-
XCTAssertEqual(expectedPath, actualPath)
92+
let api = WordPressComRestApi(localeKey: "foo")
26893

269-
let actualQueryItems = actualURLComponents!.queryItems
270-
XCTAssertNil(actualQueryItems)
94+
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
95+
let _ = await api.perform(.get, URLString: "/path/path")
96+
XCTAssertEqual(request?.url?.query, "foo=\(preferredLanguageIdentifier)")
27197
}
27298
}

WordPressKitTests/WordPressComRestApiTests.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,22 @@ class WordPressComRestApiTests: XCTestCase {
130130
self.waitForExpectations(timeout: 2, handler: nil)
131131
}
132132

133-
func testBaseUrl() {
133+
func testBaseUrl() async throws {
134+
var request: URLRequest?
135+
stub(condition: { _ in true }, response: {
136+
request = $0
137+
return HTTPStubsResponse(error: URLError(.networkConnectionLost))
138+
})
139+
134140
let defaultApi = WordPressComRestApi()
135141
XCTAssertEqual(defaultApi.baseURL.absoluteString, "https://public-api.wordpress.com/")
136-
XCTAssertTrue(defaultApi.buildRequestURLFor(path: "/path")!.hasPrefix("https://public-api.wordpress.com/path"))
142+
let _ = await defaultApi.perform(.get, URLString: "/path")
143+
try XCTAssertTrue(XCTUnwrap(request?.url?.absoluteString).hasPrefix("https://public-api.wordpress.com/path"))
137144

138145
let localhostApi = WordPressComRestApi(baseURL: URL(string: "http://localhost:8080")!)
139146
XCTAssertEqual(localhostApi.baseURL.absoluteString, "http://localhost:8080")
140-
XCTAssertTrue(localhostApi.buildRequestURLFor(path: "/path")!.hasPrefix("http://localhost:8080/path"))
147+
let _ = await localhostApi.perform(.get, URLString: "/local")
148+
try XCTAssertTrue(XCTUnwrap(request?.url?.absoluteString).hasPrefix("http://localhost:8080/local"))
141149
}
142150

143151
func testURLStringWithQuery() async {

0 commit comments

Comments
 (0)