|
1 | 1 | import Foundation |
2 | 2 | import XCTest |
| 3 | +import OHHTTPStubs |
3 | 4 |
|
4 | 5 | import WordPressShared |
5 | 6 | @testable import WordPressKit |
6 | 7 |
|
7 | 8 | extension WordPressComRestApiTests { |
8 | 9 |
|
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 | + }) |
13 | 16 |
|
14 | | - // When |
15 | 17 | 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") |
43 | 19 |
|
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)") |
49 | 22 | } |
50 | 23 |
|
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 | + |
53 | 31 | let path = "/path/path" |
54 | 32 | let params: [String: AnyObject] = [ |
55 | 33 | "someKey": "value" as AnyObject |
56 | 34 | ] |
57 | 35 |
|
58 | | - // When |
59 | 36 | 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) |
129 | 38 |
|
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 |
142 | 39 | 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"])) |
144 | 42 | } |
145 | 43 |
|
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 | + }) |
149 | 50 |
|
150 | | - // When |
151 | 51 | 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") |
166 | 53 |
|
167 | | - let actualQueryItems = actualURLComponents!.queryItems |
168 | | - XCTAssertNil(actualQueryItems) |
| 54 | + try XCTAssertEqual(XCTUnwrap(request?.url?.query), "locale=foo") |
169 | 55 | } |
170 | 56 |
|
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 | + }) |
201 | 63 |
|
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 |
212 | 64 | 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]) |
226 | 66 |
|
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") |
246 | 68 | } |
247 | 69 |
|
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 | + }) |
251 | 76 |
|
252 | | - // When |
253 | 77 | let api = WordPressComRestApi() |
254 | 78 | api.appendsPreferredLanguageLocale = false |
255 | | - let localeAppendedPath = api.buildRequestURLFor(path: path, parameters: nil) |
| 79 | + let _ = await api.perform(.get, URLString: "/path") |
256 | 80 |
|
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 | + } |
261 | 84 |
|
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 | + }) |
264 | 91 |
|
265 | | - let expectedPath = path |
266 | | - let actualPath = actualURLComponents!.path |
267 | | - XCTAssertEqual(expectedPath, actualPath) |
| 92 | + let api = WordPressComRestApi(localeKey: "foo") |
268 | 93 |
|
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)") |
271 | 97 | } |
272 | 98 | } |
0 commit comments