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

Commit 7b3a3cf

Browse files
Merge pull request #113 from wordpress-mobile/issue/109-locale-handling-nil-hotfix
Ensure that nil parameters don't defeat locale handling
2 parents 6b11bb3 + 02ea624 commit 7b3a3cf

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

WordPressKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "WordPressKit"
3-
s.version = "2.1.0"
3+
s.version = "2.1.0.1"
44
s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."
55

66
s.description = <<-DESC

WordPressKit/WordPressComRestApi.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,11 @@ open class WordPressComRestApi: NSObject {
351351

352352
var componentsWithLocale = urlComponents
353353
var existingQueryItems = componentsWithLocale.queryItems ?? []
354-
355354
let existingLocaleQueryItems = existingQueryItems.filter { $0.name == localeKey }
356-
if let parameters = parameters, parameters[localeKey] == nil, existingLocaleQueryItems.isEmpty {
355+
356+
let inputParameters = parameters ?? [:]
357+
358+
if inputParameters[localeKey] == nil, existingLocaleQueryItems.isEmpty {
357359
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
358360
let localeQueryItem = URLQueryItem(name: localeKey, value: preferredLanguageIdentifier)
359361

WordPressKitTests/WordPressComRestApiTests+Locale.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,70 @@ extension WordPressComRestApiTests {
199199
let actualQueryItemKey = actualQueryItem!.name
200200
XCTAssertEqual(expectedKey, actualQueryItemKey)
201201
}
202+
203+
func testThatAppendingLocaleWorksWhenPassingNilParameters() {
204+
// Given
205+
let path = "/path/path"
206+
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug
207+
208+
// When
209+
let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path, parameters: nil)
210+
211+
// Then
212+
XCTAssertNotNil(localeAppendedPath)
213+
let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString))
214+
XCTAssertNotNil(actualURL)
215+
216+
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
217+
XCTAssertNotNil(actualURLComponents)
218+
219+
let expectedPath = path
220+
let actualPath = actualURLComponents!.path
221+
XCTAssertEqual(expectedPath, actualPath)
222+
223+
let actualQueryItems = actualURLComponents!.queryItems
224+
XCTAssertNotNil(actualQueryItems)
225+
226+
let expectedQueryItemCount = 1
227+
let actualQueryItemCount = actualQueryItems!.count
228+
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount)
229+
230+
let actualQueryItem = actualQueryItems!.first
231+
XCTAssertNotNil(actualQueryItem!)
232+
233+
let actualQueryItemKey = actualQueryItem!.name
234+
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault
235+
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey)
236+
237+
let actualQueryItemValue = actualQueryItem!.value
238+
XCTAssertNotNil(actualQueryItemValue)
239+
240+
let expectedQueryItemValue = preferredLanguageIdentifier
241+
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!)
242+
}
243+
244+
func testThatLocaleIsNotAppendedWhenDisabledAndParametersAreNil() {
245+
// Given
246+
let path = "/path/path"
247+
248+
// When
249+
let api = WordPressComRestApi()
250+
api.appendsPreferredLanguageLocale = false
251+
let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: nil)
252+
253+
// Then
254+
XCTAssertNotNil(localeAppendedPath)
255+
let actualURL = URL(string: localeAppendedPath!, relativeTo: URL(string: WordPressComRestApi.apiBaseURLString))
256+
XCTAssertNotNil(actualURL)
257+
258+
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false)
259+
XCTAssertNotNil(actualURLComponents)
260+
261+
let expectedPath = path
262+
let actualPath = actualURLComponents!.path
263+
XCTAssertEqual(expectedPath, actualPath)
264+
265+
let actualQueryItems = actualURLComponents!.queryItems
266+
XCTAssertNil(actualQueryItems)
267+
}
202268
}

0 commit comments

Comments
 (0)