-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not add default query if it exists in the original URL (#733)
- Loading branch information
Showing
4 changed files
with
78 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 56 additions & 230 deletions
286
WordPressKitTests/WordPressComRestApiTests+Locale.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,272 +1,98 @@ | ||
import Foundation | ||
import XCTest | ||
import OHHTTPStubs | ||
|
||
import WordPressShared | ||
@testable import WordPressKit | ||
|
||
extension WordPressComRestApiTests { | ||
|
||
func testThatAppendingLocaleWorks() { | ||
// Given | ||
let path = "/path/path" | ||
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug | ||
func testAddsLocaleToURLQueryByDefault() async throws { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
let localeAppendedPath = api.buildRequestURLFor(path: path) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = path | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNotNil(actualQueryItems) | ||
|
||
let expectedQueryItemCount = 1 | ||
let actualQueryItemCount = actualQueryItems!.count | ||
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) | ||
|
||
let actualQueryItem = actualQueryItems!.first | ||
XCTAssertNotNil(actualQueryItem!) | ||
|
||
let actualQueryItemKey = actualQueryItem!.name | ||
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault | ||
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) | ||
let _ = await api.perform(.get, URLString: "/path/path") | ||
|
||
let actualQueryItemValue = actualQueryItem!.value | ||
XCTAssertNotNil(actualQueryItemValue) | ||
|
||
let expectedQueryItemValue = preferredLanguageIdentifier | ||
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) | ||
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug | ||
XCTAssertEqual(request?.url?.query, "locale=\(preferredLanguageIdentifier)") | ||
} | ||
|
||
func testThatAppendingLocaleWorksWithExistingParams() { | ||
// Given | ||
func testAddsLocaleToURLQueryByDefaultAndMaintainsInputParameters() async throws { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
let path = "/path/path" | ||
let params: [String: AnyObject] = [ | ||
"someKey": "value" as AnyObject | ||
] | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: params) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = "/path/path" | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNotNil(actualQueryItems) | ||
|
||
let expectedQueryItemCount = 1 | ||
let actualQueryItemCount = actualQueryItems!.count | ||
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) | ||
|
||
let actualQueryString = actualURLComponents?.query | ||
XCTAssertNotNil(actualQueryString) | ||
|
||
let queryStringIncludesLocale = actualQueryString!.contains(WordPressComRestApi.LocaleKeyDefault) | ||
XCTAssertTrue(queryStringIncludesLocale) | ||
} | ||
|
||
func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() { | ||
// Given | ||
let preferredLanguageIdentifier = "foo" | ||
let path = "/path/path?locale=\(preferredLanguageIdentifier)" | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
let localeAppendedPath = api.buildRequestURLFor(path: path) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = "/path/path" | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNotNil(actualQueryItems) | ||
|
||
let expectedQueryItemCount = 1 | ||
let actualQueryItemCount = actualQueryItems!.count | ||
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) | ||
|
||
let actualQueryItem = actualQueryItems!.first | ||
XCTAssertNotNil(actualQueryItem!) | ||
|
||
let actualQueryItemKey = actualQueryItem!.name | ||
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault | ||
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) | ||
|
||
let actualQueryItemValue = actualQueryItem!.value | ||
XCTAssertNotNil(actualQueryItemValue) | ||
|
||
let expectedQueryItemValue = preferredLanguageIdentifier | ||
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) | ||
} | ||
let _ = await api.perform(.get, URLString: path, parameters: params) | ||
|
||
func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() { | ||
// Given | ||
let inputPath = "/path/path" | ||
let expectedLocaleValue = "foo" | ||
let params: [String: AnyObject] = [ | ||
WordPressComRestApi.LocaleKeyDefault: expectedLocaleValue as AnyObject | ||
] | ||
|
||
// When | ||
let requestURLString = WordPressComRestApi().buildRequestURLFor(path: inputPath, parameters: params) | ||
|
||
// Then | ||
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug | ||
XCTAssertFalse(requestURLString!.contains(preferredLanguageIdentifier)) | ||
let query = try XCTUnwrap(request?.url?.query?.split(separator: "&")) | ||
XCTAssertEqual(Set(query), Set(["locale=\(preferredLanguageIdentifier)", "someKey=value"])) | ||
} | ||
|
||
func testThatLocaleIsNotAppendedWhenDisabled() { | ||
// Given | ||
let path = "/path/path" | ||
func testThatLocaleIsNotAppendedIfAlreadyIncludedInPath() async { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
api.appendsPreferredLanguageLocale = false | ||
let localeAppendedPath = api.buildRequestURLFor(path: path) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = path | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
let _ = await api.perform(.get, URLString: "/path?locale=foo") | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNil(actualQueryItems) | ||
try XCTAssertEqual(XCTUnwrap(request?.url?.query), "locale=foo") | ||
} | ||
|
||
func testThatAlternateLocaleKeyIsHonoredWhenSpecified() { | ||
// Given | ||
let path = "/path/path" | ||
let expectedKey = "foo" | ||
|
||
// When | ||
let api = WordPressComRestApi(localeKey: expectedKey) | ||
let localeAppendedPath = api.buildRequestURLFor(path: path) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = path | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNotNil(actualQueryItems) | ||
|
||
let expectedQueryItemCount = 1 | ||
let actualQueryItemCount = actualQueryItems!.count | ||
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) | ||
|
||
let actualQueryItem = actualQueryItems!.first | ||
XCTAssertNotNil(actualQueryItem!) | ||
func testThatAppendingLocaleIgnoresIfAlreadyIncludedInRequestParameters() async throws { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
let actualQueryItemKey = actualQueryItem!.name | ||
XCTAssertEqual(expectedKey, actualQueryItemKey) | ||
} | ||
|
||
func testThatAppendingLocaleWorksWhenPassingNilParameters() { | ||
// Given | ||
let path = "/path/path" | ||
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
let localeAppendedPath = WordPressComRestApi().buildRequestURLFor(path: path, parameters: nil) | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
|
||
let expectedPath = path | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
let _ = await api.perform(.get, URLString: "/path", parameters: ["locale": "foo"] as [String: AnyObject]) | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNotNil(actualQueryItems) | ||
|
||
let expectedQueryItemCount = 1 | ||
let actualQueryItemCount = actualQueryItems!.count | ||
XCTAssertEqual(expectedQueryItemCount, actualQueryItemCount) | ||
|
||
let actualQueryItem = actualQueryItems!.first | ||
XCTAssertNotNil(actualQueryItem!) | ||
|
||
let actualQueryItemKey = actualQueryItem!.name | ||
let expectedQueryItemKey = WordPressComRestApi.LocaleKeyDefault | ||
XCTAssertEqual(expectedQueryItemKey, actualQueryItemKey) | ||
|
||
let actualQueryItemValue = actualQueryItem!.value | ||
XCTAssertNotNil(actualQueryItemValue) | ||
|
||
let expectedQueryItemValue = preferredLanguageIdentifier | ||
XCTAssertEqual(expectedQueryItemValue, actualQueryItemValue!) | ||
try XCTAssertEqual(XCTUnwrap(request?.url?.query), "locale=foo") | ||
} | ||
|
||
func testThatLocaleIsNotAppendedWhenDisabledAndParametersAreNil() { | ||
// Given | ||
let path = "/path/path" | ||
func testThatLocaleIsNotAppendedWhenDisabled() async { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
// When | ||
let api = WordPressComRestApi() | ||
api.appendsPreferredLanguageLocale = false | ||
let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: nil) | ||
let _ = await api.perform(.get, URLString: "/path") | ||
|
||
// Then | ||
XCTAssertNotNil(localeAppendedPath) | ||
let actualURL = URL(string: localeAppendedPath!, relativeTo: api.baseURL) | ||
XCTAssertNotNil(actualURL) | ||
XCTAssertNotNil(request?.url) | ||
XCTAssertNil(request?.url?.query) | ||
} | ||
|
||
let actualURLComponents = URLComponents(url: actualURL!, resolvingAgainstBaseURL: false) | ||
XCTAssertNotNil(actualURLComponents) | ||
func testThatAlternateLocaleKeyIsHonoredWhenSpecified() async { | ||
var request: URLRequest? | ||
stub(condition: { _ in true }, response: { | ||
request = $0 | ||
return HTTPStubsResponse(error: URLError(.networkConnectionLost)) | ||
}) | ||
|
||
let expectedPath = path | ||
let actualPath = actualURLComponents!.path | ||
XCTAssertEqual(expectedPath, actualPath) | ||
let api = WordPressComRestApi(localeKey: "foo") | ||
|
||
let actualQueryItems = actualURLComponents!.queryItems | ||
XCTAssertNil(actualQueryItems) | ||
let preferredLanguageIdentifier = WordPressComLanguageDatabase().deviceLanguage.slug | ||
let _ = await api.perform(.get, URLString: "/path/path") | ||
XCTAssertEqual(request?.url?.query, "foo=\(preferredLanguageIdentifier)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters