|
28 | 28 |
|
29 | 29 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
30 | 30 | import static org.hamcrest.Matchers.allOf; |
31 | | -import static org.hamcrest.Matchers.any; |
32 | 31 | import static org.hamcrest.Matchers.anything; |
33 | 32 | import static org.hamcrest.Matchers.contains; |
34 | 33 | import static org.hamcrest.Matchers.containsInAnyOrder; |
35 | 34 | import static org.hamcrest.Matchers.containsString; |
36 | | -import static org.hamcrest.Matchers.either; |
37 | 35 | import static org.hamcrest.Matchers.endsWith; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
38 | 37 | import static org.hamcrest.Matchers.everyItem; |
39 | 38 | import static org.hamcrest.Matchers.hasItem; |
40 | 39 | import static org.hamcrest.Matchers.hasSize; |
41 | 40 | import static org.hamcrest.Matchers.is; |
42 | 41 | import static org.hamcrest.Matchers.notNullValue; |
43 | | -import static org.hamcrest.Matchers.nullValue; |
44 | 42 | import static org.hamcrest.Matchers.startsWith; |
45 | 43 |
|
46 | 44 | /** |
@@ -163,65 +161,68 @@ void headerContainsWithMissingValue() { |
163 | 161 | @Test |
164 | 162 | void headerListMissing() { |
165 | 163 | assertThatAssertionError() |
166 | | - .isThrownBy(() -> MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request)) |
| 164 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", hasSize(2)).match(this.request)) |
167 | 165 | .withMessage("Expected header <foo> to exist but was null"); |
168 | 166 | } |
169 | 167 |
|
170 | 168 | @Test |
171 | 169 | void headerListMatchers() throws IOException { |
172 | 170 | this.request.getHeaders().put("foo", List.of("bar", "baz")); |
173 | 171 |
|
174 | | - MockRestRequestMatchers.header("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
175 | | - MockRestRequestMatchers.header("foo", contains(is("bar"), is("baz"))).match(this.request); |
176 | | - MockRestRequestMatchers.header("foo", contains(is("bar"), anything())).match(this.request); |
177 | | - MockRestRequestMatchers.header("foo", hasItem(endsWith("baz"))).match(this.request); |
178 | | - MockRestRequestMatchers.header("foo", everyItem(startsWith("ba"))).match(this.request); |
179 | | - MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request); |
180 | | - |
181 | | - // These can be a bit ambiguous when reading the test (the compiler selects the list matcher): |
182 | | - MockRestRequestMatchers.header("foo", notNullValue()).match(this.request); |
183 | | - MockRestRequestMatchers.header("foo", is(anything())).match(this.request); |
184 | | - MockRestRequestMatchers.header("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
185 | | - |
186 | | - // These are not as ambiguous thanks to an inner matcher that is either obviously list-oriented, |
187 | | - // string-oriented, or obviously a vararg of matchers |
188 | | - |
189 | | - // list matcher version |
190 | | - MockRestRequestMatchers.header("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
191 | | - |
192 | | - // vararg version |
193 | | - MockRestRequestMatchers.header("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request); |
194 | | - MockRestRequestMatchers.header("foo", is((any(String.class)))).match(this.request); |
195 | | - MockRestRequestMatchers.header("foo", either(is("bar")).or(is(nullValue()))).match(this.request); |
196 | | - MockRestRequestMatchers.header("foo", is(notNullValue()), is(notNullValue())).match(this.request); |
| 172 | + MockRestRequestMatchers.headerList("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
| 173 | + MockRestRequestMatchers.headerList("foo", contains(is("bar"), is("baz"))).match(this.request); |
| 174 | + MockRestRequestMatchers.headerList("foo", contains(is("bar"), anything())).match(this.request); |
| 175 | + MockRestRequestMatchers.headerList("foo", hasItem(endsWith("baz"))).match(this.request); |
| 176 | + MockRestRequestMatchers.headerList("foo", everyItem(startsWith("ba"))).match(this.request); |
| 177 | + MockRestRequestMatchers.headerList("foo", hasSize(2)).match(this.request); |
| 178 | + |
| 179 | + MockRestRequestMatchers.headerList("foo", notNullValue()).match(this.request); |
| 180 | + MockRestRequestMatchers.headerList("foo", is(anything())).match(this.request); |
| 181 | + MockRestRequestMatchers.headerList("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
| 182 | + |
| 183 | + MockRestRequestMatchers.headerList("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
197 | 184 | } |
198 | 185 |
|
199 | 186 | @Test |
200 | 187 | void headerListContainsMismatch() { |
201 | 188 | this.request.getHeaders().put("foo", List.of("bar", "baz")); |
202 | 189 |
|
203 | 190 | assertThatAssertionError() |
204 | | - .isThrownBy(() -> MockRestRequestMatchers.header("foo", contains(containsString("ba"))).match(this.request)) |
| 191 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", contains(containsString("ba"))).match(this.request)) |
205 | 192 | .withMessageContainingAll( |
206 | 193 | "Request header [foo] values", |
207 | 194 | "Expected: iterable containing [a string containing \"ba\"]", |
208 | 195 | "but: not matched: \"baz\""); |
209 | 196 |
|
210 | 197 | assertThatAssertionError() |
211 | | - .isThrownBy(() -> MockRestRequestMatchers.header("foo", hasItem(endsWith("ba"))).match(this.request)) |
| 198 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", hasItem(endsWith("ba"))).match(this.request)) |
212 | 199 | .withMessageContainingAll( |
213 | 200 | "Request header [foo] values", |
214 | 201 | "Expected: a collection containing a string ending with \"ba\"", |
215 | 202 | "but: mismatches were: [was \"bar\", was \"baz\"]"); |
216 | 203 |
|
217 | 204 | assertThatAssertionError() |
218 | | - .isThrownBy(() -> MockRestRequestMatchers.header("foo", everyItem(endsWith("ar"))).match(this.request)) |
| 205 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", everyItem(endsWith("ar"))).match(this.request)) |
219 | 206 | .withMessageContainingAll( |
220 | 207 | "Request header [foo] values", |
221 | 208 | "Expected: every item is a string ending with \"ar\"", |
222 | 209 | "but: an item was \"baz\""); |
223 | 210 | } |
224 | 211 |
|
| 212 | + @Test |
| 213 | + void headerListDoesntHideHeaderWithSingleMatcher() throws IOException { |
| 214 | + this.request.getHeaders().put("foo", List.of("bar", "baz")); |
| 215 | + |
| 216 | + MockRestRequestMatchers.header("foo", equalTo("bar")).match(this.request); |
| 217 | + |
| 218 | + assertThatAssertionError() |
| 219 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", equalTo("bar")).match(this.request)) |
| 220 | + .withMessageContainingAll( |
| 221 | + "Request header [foo] values", |
| 222 | + "Expected: \"bar\"", |
| 223 | + "but: was <[bar, baz]>"); |
| 224 | + } |
| 225 | + |
225 | 226 | @Test |
226 | 227 | void headers() throws Exception { |
227 | 228 | this.request.getHeaders().put("foo", List.of("bar", "baz")); |
@@ -290,65 +291,68 @@ void queryParamContainsWithMissingValue() { |
290 | 291 | @Test |
291 | 292 | void queryParamListMissing() { |
292 | 293 | assertThatAssertionError() |
293 | | - .isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request)) |
| 294 | + .isThrownBy(() -> MockRestRequestMatchers.queryParamList("foo", hasSize(2)).match(this.request)) |
294 | 295 | .withMessage("Expected query param <foo> to exist but was null"); |
295 | 296 | } |
296 | 297 |
|
297 | 298 | @Test |
298 | 299 | void queryParamListMatchers() throws IOException { |
299 | 300 | this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz")); |
300 | 301 |
|
301 | | - MockRestRequestMatchers.queryParam("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
302 | | - MockRestRequestMatchers.queryParam("foo", contains(is("bar"), is("baz"))).match(this.request); |
303 | | - MockRestRequestMatchers.queryParam("foo", contains(is("bar"), anything())).match(this.request); |
304 | | - MockRestRequestMatchers.queryParam("foo", hasItem(endsWith("baz"))).match(this.request); |
305 | | - MockRestRequestMatchers.queryParam("foo", everyItem(startsWith("ba"))).match(this.request); |
306 | | - MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request); |
307 | | - |
308 | | - // These can be a bit ambiguous when reading the test (the compiler selects the list matcher): |
309 | | - MockRestRequestMatchers.queryParam("foo", notNullValue()).match(this.request); |
310 | | - MockRestRequestMatchers.queryParam("foo", is(anything())).match(this.request); |
311 | | - MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
312 | | - |
313 | | - // These are not as ambiguous thanks to an inner matcher that is either obviously list-oriented, |
314 | | - // string-oriented, or obviously a vararg of matchers |
315 | | - |
316 | | - // list matcher version |
317 | | - MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
318 | | - |
319 | | - // vararg version |
320 | | - MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request); |
321 | | - MockRestRequestMatchers.queryParam("foo", is((any(String.class)))).match(this.request); |
322 | | - MockRestRequestMatchers.queryParam("foo", either(is("bar")).or(is(nullValue()))).match(this.request); |
323 | | - MockRestRequestMatchers.queryParam("foo", is(notNullValue()), is(notNullValue())).match(this.request); |
| 302 | + MockRestRequestMatchers.queryParamList("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
| 303 | + MockRestRequestMatchers.queryParamList("foo", contains(is("bar"), is("baz"))).match(this.request); |
| 304 | + MockRestRequestMatchers.queryParamList("foo", contains(is("bar"), anything())).match(this.request); |
| 305 | + MockRestRequestMatchers.queryParamList("foo", hasItem(endsWith("baz"))).match(this.request); |
| 306 | + MockRestRequestMatchers.queryParamList("foo", everyItem(startsWith("ba"))).match(this.request); |
| 307 | + MockRestRequestMatchers.queryParamList("foo", hasSize(2)).match(this.request); |
| 308 | + |
| 309 | + MockRestRequestMatchers.queryParamList("foo", notNullValue()).match(this.request); |
| 310 | + MockRestRequestMatchers.queryParamList("foo", is(anything())).match(this.request); |
| 311 | + MockRestRequestMatchers.queryParamList("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
| 312 | + |
| 313 | + MockRestRequestMatchers.queryParamList("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
324 | 314 | } |
325 | 315 |
|
326 | 316 | @Test |
327 | 317 | void queryParamListContainsMismatch() { |
328 | 318 | this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz")); |
329 | 319 |
|
330 | 320 | assertThatAssertionError() |
331 | | - .isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", contains(containsString("ba"))).match(this.request)) |
| 321 | + .isThrownBy(() -> MockRestRequestMatchers.queryParamList("foo", contains(containsString("ba"))).match(this.request)) |
332 | 322 | .withMessageContainingAll( |
333 | 323 | "Query param [foo] values", |
334 | 324 | "Expected: iterable containing [a string containing \"ba\"]", |
335 | 325 | "but: not matched: \"baz\""); |
336 | 326 |
|
337 | 327 | assertThatAssertionError() |
338 | | - .isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasItem(endsWith("ba"))).match(this.request)) |
| 328 | + .isThrownBy(() -> MockRestRequestMatchers.queryParamList("foo", hasItem(endsWith("ba"))).match(this.request)) |
339 | 329 | .withMessageContainingAll( |
340 | 330 | "Query param [foo] values", |
341 | 331 | "Expected: a collection containing a string ending with \"ba\"", |
342 | 332 | "but: mismatches were: [was \"bar\", was \"baz\"]"); |
343 | 333 |
|
344 | 334 | assertThatAssertionError() |
345 | | - .isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", everyItem(endsWith("ar"))).match(this.request)) |
| 335 | + .isThrownBy(() -> MockRestRequestMatchers.queryParamList("foo", everyItem(endsWith("ar"))).match(this.request)) |
346 | 336 | .withMessageContainingAll( |
347 | 337 | "Query param [foo] values", |
348 | 338 | "Expected: every item is a string ending with \"ar\"", |
349 | 339 | "but: an item was \"baz\""); |
350 | 340 | } |
351 | 341 |
|
| 342 | + @Test |
| 343 | + void queryParamListDoesntHideQueryParamWithSingleMatcher() throws IOException { |
| 344 | + this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz")); |
| 345 | + |
| 346 | + MockRestRequestMatchers.queryParam("foo", equalTo("bar")).match(this.request); |
| 347 | + |
| 348 | + assertThatAssertionError() |
| 349 | + .isThrownBy(() -> MockRestRequestMatchers.queryParamList("foo", equalTo("bar")).match(this.request)) |
| 350 | + .withMessageContainingAll( |
| 351 | + "Query param [foo] values", |
| 352 | + "Expected: \"bar\"", |
| 353 | + "but: was <[bar, baz]>"); |
| 354 | + } |
| 355 | + |
352 | 356 | private static ThrowableTypeAssert<AssertionError> assertThatAssertionError() { |
353 | 357 | return assertThatExceptionOfType(AssertionError.class); |
354 | 358 | } |
|
0 commit comments