|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.test.web.client.match;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.net.URI;
|
20 | 21 | import java.util.Arrays;
|
21 | 22 | import java.util.Collections;
|
22 | 23 | import java.util.List;
|
23 | 24 |
|
| 25 | +import org.hamcrest.CoreMatchers; |
| 26 | +import org.hamcrest.Matchers; |
24 | 27 | import org.junit.jupiter.api.Test;
|
25 | 28 |
|
26 | 29 | import org.springframework.http.HttpMethod;
|
27 | 30 | import org.springframework.mock.http.client.MockClientHttpRequest;
|
28 | 31 |
|
| 32 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
29 | 33 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
| 34 | +import static org.hamcrest.Matchers.allOf; |
| 35 | +import static org.hamcrest.Matchers.any; |
| 36 | +import static org.hamcrest.Matchers.anything; |
| 37 | +import static org.hamcrest.Matchers.contains; |
| 38 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
30 | 39 | import static org.hamcrest.Matchers.containsString;
|
| 40 | +import static org.hamcrest.Matchers.endsWith; |
| 41 | +import static org.hamcrest.Matchers.everyItem; |
| 42 | +import static org.hamcrest.Matchers.hasItem; |
| 43 | +import static org.hamcrest.Matchers.hasSize; |
| 44 | +import static org.hamcrest.Matchers.is; |
| 45 | +import static org.hamcrest.Matchers.notNullValue; |
| 46 | +import static org.hamcrest.Matchers.nullValue; |
| 47 | +import static org.hamcrest.Matchers.startsWith; |
31 | 48 |
|
32 | 49 | /**
|
33 | 50 | * Unit tests for {@link MockRestRequestMatchers}.
|
@@ -146,6 +163,63 @@ void headerContainsWithMissingValue() {
|
146 | 163 | .hasMessageContaining("was \"bar\"");
|
147 | 164 | }
|
148 | 165 |
|
| 166 | + @Test |
| 167 | + void headerListMissing() { |
| 168 | + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request)) |
| 169 | + .isInstanceOf(AssertionError.class) |
| 170 | + .hasMessage("No header values for header [foo]"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void headerListMatchers() throws IOException { |
| 175 | + this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); |
| 176 | + |
| 177 | + MockRestRequestMatchers.header("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
| 178 | + MockRestRequestMatchers.header("foo", contains(is("bar"), is("baz"))).match(this.request); |
| 179 | + MockRestRequestMatchers.header("foo", contains(is("bar"), Matchers.anything())).match(this.request); |
| 180 | + MockRestRequestMatchers.header("foo", hasItem(endsWith("baz"))).match(this.request); |
| 181 | + MockRestRequestMatchers.header("foo", everyItem(startsWith("ba"))).match(this.request); |
| 182 | + MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request); |
| 183 | + |
| 184 | + //these can be a bit ambiguous when reading the test (the compiler selects the list matcher): |
| 185 | + MockRestRequestMatchers.header("foo", notNullValue()).match(this.request); |
| 186 | + MockRestRequestMatchers.header("foo", is(anything())).match(this.request); |
| 187 | + MockRestRequestMatchers.header("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
| 188 | + |
| 189 | + //these are not as ambiguous thanks to an inner matcher that is either obviously list-oriented, |
| 190 | + //string-oriented or obviously a vararg of matchers |
| 191 | + //list matcher version |
| 192 | + MockRestRequestMatchers.header("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
| 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", CoreMatchers.either(is("bar")).or(is(nullValue()))).match(this.request); |
| 197 | + MockRestRequestMatchers.header("foo", is(notNullValue()), is(notNullValue())).match(this.request); |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void headerListContainsMismatch() { |
| 202 | + this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); |
| 203 | + |
| 204 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 205 | + .header("foo", contains(containsString("ba"))).match(this.request)) |
| 206 | + .withMessage("Request header values for [foo]\n" |
| 207 | + + "Expected: iterable containing [a string containing \"ba\"]\n" |
| 208 | + + " but: not matched: \"baz\""); |
| 209 | + |
| 210 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 211 | + .header("foo", hasItem(endsWith("ba"))).match(this.request)) |
| 212 | + .withMessage("Request header values for [foo]\n" |
| 213 | + + "Expected: a collection containing a string ending with \"ba\"\n" |
| 214 | + + " but: mismatches were: [was \"bar\", was \"baz\"]"); |
| 215 | + |
| 216 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 217 | + .header("foo", everyItem(endsWith("ar"))).match(this.request)) |
| 218 | + .withMessage("Request header values for [foo]\n" |
| 219 | + + "Expected: every item is a string ending with \"ar\"\n" |
| 220 | + + " but: an item was \"baz\""); |
| 221 | + } |
| 222 | + |
149 | 223 | @Test
|
150 | 224 | void headers() throws Exception {
|
151 | 225 | this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
@@ -210,4 +284,62 @@ void queryParamContainsWithMissingValue() {
|
210 | 284 | .hasMessageContaining("was \"bar\"");
|
211 | 285 | }
|
212 | 286 |
|
| 287 | + |
| 288 | + @Test |
| 289 | + void queryParamListMissing() { |
| 290 | + assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request)) |
| 291 | + .isInstanceOf(AssertionError.class) |
| 292 | + .hasMessage("No queryParam [foo]"); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + void queryParamListMatchers() throws IOException { |
| 297 | + this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz")); |
| 298 | + |
| 299 | + MockRestRequestMatchers.queryParam("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request); |
| 300 | + MockRestRequestMatchers.queryParam("foo", contains(is("bar"), is("baz"))).match(this.request); |
| 301 | + MockRestRequestMatchers.queryParam("foo", contains(is("bar"), Matchers.anything())).match(this.request); |
| 302 | + MockRestRequestMatchers.queryParam("foo", hasItem(endsWith("baz"))).match(this.request); |
| 303 | + MockRestRequestMatchers.queryParam("foo", everyItem(startsWith("ba"))).match(this.request); |
| 304 | + MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request); |
| 305 | + |
| 306 | + //these can be a bit ambiguous when reading the test (the compiler selects the list matcher): |
| 307 | + MockRestRequestMatchers.queryParam("foo", notNullValue()).match(this.request); |
| 308 | + MockRestRequestMatchers.queryParam("foo", is(anything())).match(this.request); |
| 309 | + MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), notNullValue())).match(this.request); |
| 310 | + |
| 311 | + //these are not as ambiguous thanks to an inner matcher that is either obviously list-oriented, |
| 312 | + //string-oriented or obviously a vararg of matchers |
| 313 | + //list matcher version |
| 314 | + MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), hasSize(2))).match(this.request); |
| 315 | + //vararg version |
| 316 | + MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request); |
| 317 | + MockRestRequestMatchers.queryParam("foo", is((any(String.class)))).match(this.request); |
| 318 | + MockRestRequestMatchers.queryParam("foo", CoreMatchers.either(is("bar")).or(is(nullValue()))).match(this.request); |
| 319 | + MockRestRequestMatchers.queryParam("foo", is(notNullValue()), is(notNullValue())).match(this.request); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + void queryParamListContainsMismatch() { |
| 324 | + this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz")); |
| 325 | + |
| 326 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 327 | + .queryParam("foo", contains(containsString("ba"))).match(this.request)) |
| 328 | + .withMessage("Request queryParam values for [foo]\n" |
| 329 | + + "Expected: iterable containing [a string containing \"ba\"]\n" |
| 330 | + + " but: not matched: \"baz\""); |
| 331 | + |
| 332 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 333 | + .queryParam("foo", hasItem(endsWith("ba"))).match(this.request)) |
| 334 | + .withMessage("Request queryParam values for [foo]\n" |
| 335 | + + "Expected: a collection containing a string ending with \"ba\"\n" |
| 336 | + + " but: mismatches were: [was \"bar\", was \"baz\"]"); |
| 337 | + |
| 338 | + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers |
| 339 | + .queryParam("foo", everyItem(endsWith("ar"))).match(this.request)) |
| 340 | + .withMessage("Request queryParam values for [foo]\n" |
| 341 | + + "Expected: every item is a string ending with \"ar\"\n" |
| 342 | + + " but: an item was \"baz\""); |
| 343 | + } |
| 344 | + |
213 | 345 | }
|
0 commit comments