|
29 | 29 |
|
30 | 30 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
31 | 31 | import static org.hamcrest.Matchers.allOf;
|
32 |
| -import static org.hamcrest.Matchers.any; |
33 | 32 | import static org.hamcrest.Matchers.anything;
|
34 | 33 | import static org.hamcrest.Matchers.contains;
|
35 | 34 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
36 | 35 | import static org.hamcrest.Matchers.containsString;
|
37 |
| -import static org.hamcrest.Matchers.either; |
38 | 36 | import static org.hamcrest.Matchers.endsWith;
|
| 37 | +import static org.hamcrest.Matchers.equalTo; |
39 | 38 | import static org.hamcrest.Matchers.everyItem;
|
40 | 39 | import static org.hamcrest.Matchers.hasItem;
|
41 | 40 | import static org.hamcrest.Matchers.hasSize;
|
42 | 41 | import static org.hamcrest.Matchers.is;
|
43 | 42 | import static org.hamcrest.Matchers.notNullValue;
|
44 |
| -import static org.hamcrest.Matchers.nullValue; |
45 | 43 | import static org.hamcrest.Matchers.startsWith;
|
46 | 44 |
|
47 | 45 | /**
|
@@ -164,65 +162,68 @@ void headerContainsWithMissingValue() {
|
164 | 162 | @Test
|
165 | 163 | void headerListMissing() {
|
166 | 164 | assertThatAssertionError()
|
167 |
| - .isThrownBy(() -> MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request)) |
| 165 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", hasSize(2)).match(this.request)) |
168 | 166 | .withMessage("Expected header <foo> to exist but was null");
|
169 | 167 | }
|
170 | 168 |
|
171 | 169 | @Test
|
172 | 170 | void headerListMatchers() throws IOException {
|
173 | 171 | this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
174 | 172 |
|
175 |
| - MockRestRequestMatchers.header("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
176 |
| - MockRestRequestMatchers.header("foo", contains(is("bar"), is("baz"))).match(this.request); |
177 |
| - MockRestRequestMatchers.header("foo", contains(is("bar"), anything())).match(this.request); |
178 |
| - MockRestRequestMatchers.header("foo", hasItem(endsWith("baz"))).match(this.request); |
179 |
| - MockRestRequestMatchers.header("foo", everyItem(startsWith("ba"))).match(this.request); |
180 |
| - MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request); |
181 |
| - |
182 |
| - // These can be a bit ambiguous when reading the test (the compiler selects the list matcher): |
183 |
| - MockRestRequestMatchers.header("foo", notNullValue()).match(this.request); |
184 |
| - MockRestRequestMatchers.header("foo", is(anything())).match(this.request); |
185 |
| - MockRestRequestMatchers.header("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
186 |
| - |
187 |
| - // These are not as ambiguous thanks to an inner matcher that is either obviously list-oriented, |
188 |
| - // string-oriented, or obviously a vararg of matchers |
189 |
| - |
190 |
| - // list matcher version |
191 |
| - MockRestRequestMatchers.header("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
192 |
| - |
193 |
| - // vararg version |
194 |
| - MockRestRequestMatchers.header("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request); |
195 |
| - MockRestRequestMatchers.header("foo", is((any(String.class)))).match(this.request); |
196 |
| - MockRestRequestMatchers.header("foo", either(is("bar")).or(is(nullValue()))).match(this.request); |
197 |
| - MockRestRequestMatchers.header("foo", is(notNullValue()), is(notNullValue())).match(this.request); |
| 173 | + MockRestRequestMatchers.headerList("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
| 174 | + MockRestRequestMatchers.headerList("foo", contains(is("bar"), is("baz"))).match(this.request); |
| 175 | + MockRestRequestMatchers.headerList("foo", contains(is("bar"), anything())).match(this.request); |
| 176 | + MockRestRequestMatchers.headerList("foo", hasItem(endsWith("baz"))).match(this.request); |
| 177 | + MockRestRequestMatchers.headerList("foo", everyItem(startsWith("ba"))).match(this.request); |
| 178 | + MockRestRequestMatchers.headerList("foo", hasSize(2)).match(this.request); |
| 179 | + |
| 180 | + MockRestRequestMatchers.headerList("foo", notNullValue()).match(this.request); |
| 181 | + MockRestRequestMatchers.headerList("foo", is(anything())).match(this.request); |
| 182 | + MockRestRequestMatchers.headerList("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
| 183 | + |
| 184 | + MockRestRequestMatchers.headerList("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
198 | 185 | }
|
199 | 186 |
|
200 | 187 | @Test
|
201 | 188 | void headerListContainsMismatch() {
|
202 | 189 | this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
203 | 190 |
|
204 | 191 | assertThatAssertionError()
|
205 |
| - .isThrownBy(() -> MockRestRequestMatchers.header("foo", contains(containsString("ba"))).match(this.request)) |
| 192 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", contains(containsString("ba"))).match(this.request)) |
206 | 193 | .withMessageContainingAll(
|
207 | 194 | "Request header [foo] values",
|
208 | 195 | "Expected: iterable containing [a string containing \"ba\"]",
|
209 | 196 | "but: not matched: \"baz\"");
|
210 | 197 |
|
211 | 198 | assertThatAssertionError()
|
212 |
| - .isThrownBy(() -> MockRestRequestMatchers.header("foo", hasItem(endsWith("ba"))).match(this.request)) |
| 199 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", hasItem(endsWith("ba"))).match(this.request)) |
213 | 200 | .withMessageContainingAll(
|
214 | 201 | "Request header [foo] values",
|
215 | 202 | "Expected: a collection containing a string ending with \"ba\"",
|
216 | 203 | "but: mismatches were: [was \"bar\", was \"baz\"]");
|
217 | 204 |
|
218 | 205 | assertThatAssertionError()
|
219 |
| - .isThrownBy(() -> MockRestRequestMatchers.header("foo", everyItem(endsWith("ar"))).match(this.request)) |
| 206 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", everyItem(endsWith("ar"))).match(this.request)) |
220 | 207 | .withMessageContainingAll(
|
221 | 208 | "Request header [foo] values",
|
222 | 209 | "Expected: every item is a string ending with \"ar\"",
|
223 | 210 | "but: an item was \"baz\"");
|
224 | 211 | }
|
225 | 212 |
|
| 213 | + @Test |
| 214 | + void headerListDoesntHideHeaderWithSingleMatcher() throws IOException { |
| 215 | + this.request.getHeaders().put("foo", List.of("bar", "baz")); |
| 216 | + |
| 217 | + MockRestRequestMatchers.header("foo", equalTo("bar")).match(this.request); |
| 218 | + |
| 219 | + assertThatAssertionError() |
| 220 | + .isThrownBy(() -> MockRestRequestMatchers.headerList("foo", equalTo("bar")).match(this.request)) |
| 221 | + .withMessageContainingAll( |
| 222 | + "Request header [foo] values", |
| 223 | + "Expected: \"bar\"", |
| 224 | + "but: was <[bar, baz]>"); |
| 225 | + } |
| 226 | + |
226 | 227 | @Test
|
227 | 228 | void headers() throws Exception {
|
228 | 229 | this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
@@ -290,65 +291,68 @@ void queryParamContainsWithMissingValue() throws Exception {
|
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