Revise queryParam() and header() support in MockRestRequestMatchers
See gh-29953 See gh-29964
This commit is contained in:
@@ -18,25 +18,23 @@ package org.springframework.test.web.client.match;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.assertj.core.api.AssertionsForClassTypes;
|
||||
import org.assertj.core.api.ThrowableTypeAssert;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.any;
|
||||
import static org.hamcrest.Matchers.anything;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.either;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.everyItem;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
@@ -52,6 +50,7 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
* @author Craig Walls
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @author Simon Baslé
|
||||
*/
|
||||
class MockRestRequestMatchersTests {
|
||||
|
||||
@@ -76,9 +75,8 @@ class MockRestRequestMatchersTests {
|
||||
void requestToNoMatch() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/bar"));
|
||||
|
||||
assertThatThrownBy(
|
||||
() -> MockRestRequestMatchers.requestTo("http://www.foo.example/wrong").match(this.request))
|
||||
.isInstanceOf(AssertionError.class);
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.requestTo("http://www.foo.example/wrong").match(this.request));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -99,14 +97,14 @@ class MockRestRequestMatchersTests {
|
||||
void methodNoMatch() {
|
||||
this.request.setMethod(HttpMethod.POST);
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("expected:<GET> but was:<POST>");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request))
|
||||
.withMessageContaining("expected:<GET> but was:<POST>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void header() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
}
|
||||
@@ -117,62 +115,62 @@ class MockRestRequestMatchersTests {
|
||||
MockRestRequestMatchers.headerDoesNotExist("").match(this.request);
|
||||
MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request);
|
||||
|
||||
List<String> values = Arrays.asList("bar", "baz");
|
||||
List<String> values = List.of("bar", "baz");
|
||||
this.request.getHeaders().put("foo", values);
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessage("Expected header <foo> not to exist, but it exists with values: " + values);
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request))
|
||||
.withMessage("Expected header <foo> not to exist, but it exists with values: " + values);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerMissing() {
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("was null");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
|
||||
.withMessageContaining("was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerMissingValue() {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("expected:<bad> but was:<bar>");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request))
|
||||
.withMessageContaining("expected:<bad> but was:<bar>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerContains() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
MockRestRequestMatchers.header("foo", containsString("ba")).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerContainsWithMissingHeader() {
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("but was null");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request))
|
||||
.withMessageContaining("but was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerContainsWithMissingValue() {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("was \"bar\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request))
|
||||
.withMessageContaining("was \"bar\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerListMissing() {
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessage("No header values for header [foo]");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request))
|
||||
.withMessage("Expected header <foo> to exist but was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerListMatchers() throws IOException {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
MockRestRequestMatchers.header("foo", containsInAnyOrder(endsWith("baz"), endsWith("bar"))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", contains(is("bar"), is("baz"))).match(this.request);
|
||||
@@ -181,66 +179,71 @@ class MockRestRequestMatchersTests {
|
||||
MockRestRequestMatchers.header("foo", everyItem(startsWith("ba"))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", hasSize(2)).match(this.request);
|
||||
|
||||
//these can be a bit ambiguous when reading the test (the compiler selects the list matcher):
|
||||
// These can be a bit ambiguous when reading the test (the compiler selects the list matcher):
|
||||
MockRestRequestMatchers.header("foo", notNullValue()).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", is(anything())).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", allOf(notNullValue(), notNullValue())).match(this.request);
|
||||
|
||||
//these are not as ambiguous thanks to an inner matcher that is either obviously list-oriented,
|
||||
//string-oriented or obviously a vararg of matchers
|
||||
//list matcher version
|
||||
// These are not as ambiguous thanks to an inner matcher that is either obviously list-oriented,
|
||||
// string-oriented, or obviously a vararg of matchers
|
||||
|
||||
// list matcher version
|
||||
MockRestRequestMatchers.header("foo", allOf(notNullValue(), hasSize(2))).match(this.request);
|
||||
//vararg version
|
||||
|
||||
// vararg version
|
||||
MockRestRequestMatchers.header("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", is((any(String.class)))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", CoreMatchers.either(is("bar")).or(is(nullValue()))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", either(is("bar")).or(is(nullValue()))).match(this.request);
|
||||
MockRestRequestMatchers.header("foo", is(notNullValue()), is(notNullValue())).match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerListContainsMismatch() {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.header("foo", contains(containsString("ba"))).match(this.request))
|
||||
.withMessage("Request header values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: iterable containing [a string containing \"ba\"]" + System.lineSeparator()
|
||||
+ " but: not matched: \"baz\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", contains(containsString("ba"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Request header [foo] values",
|
||||
"Expected: iterable containing [a string containing \"ba\"]",
|
||||
"but: not matched: \"baz\"");
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.header("foo", hasItem(endsWith("ba"))).match(this.request))
|
||||
.withMessage("Request header values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: a collection containing a string ending with \"ba\"" + System.lineSeparator()
|
||||
+ " but: mismatches were: [was \"bar\", was \"baz\"]");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", hasItem(endsWith("ba"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Request header [foo] values",
|
||||
"Expected: a collection containing a string ending with \"ba\"",
|
||||
"but: mismatches were: [was \"bar\", was \"baz\"]");
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.header("foo", everyItem(endsWith("ar"))).match(this.request))
|
||||
.withMessage("Request header values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: every item is a string ending with \"ar\"" + System.lineSeparator()
|
||||
+ " but: an item was \"baz\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", everyItem(endsWith("ar"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Request header [foo] values",
|
||||
"Expected: every item is a string ending with \"ar\"",
|
||||
"but: an item was \"baz\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headers() throws Exception {
|
||||
this.request.getHeaders().put("foo", Arrays.asList("bar", "baz"));
|
||||
this.request.getHeaders().put("foo", List.of("bar", "baz"));
|
||||
|
||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
void headersWithMissingHeader() {
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("but was null");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))
|
||||
.withMessageContaining("but was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void headersWithMissingValue() {
|
||||
this.request.getHeaders().put("foo", Collections.singletonList("bar"));
|
||||
this.request.getHeaders().put("foo", List.of("bar"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("to have at least <2> values");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request))
|
||||
.withMessageContaining("to have at least <2> values");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -254,18 +257,18 @@ class MockRestRequestMatchersTests {
|
||||
void queryParamMissing() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("but was null");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request))
|
||||
.withMessageContaining("but was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryParamMissingValue() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("expected:<bad> but was:<bar>");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request))
|
||||
.withMessageContaining("expected:<bad> but was:<bar>");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -279,17 +282,17 @@ class MockRestRequestMatchersTests {
|
||||
void queryParamContainsWithMissingValue() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
|
||||
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessageContaining("was \"bar\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request))
|
||||
.withMessageContaining("was \"bar\"");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void queryParamListMissing() {
|
||||
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request))
|
||||
.isInstanceOf(AssertionError.class)
|
||||
.hasMessage("No queryParam [foo]");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request))
|
||||
.withMessage("Expected query param <foo> to exist but was null");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -303,19 +306,21 @@ class MockRestRequestMatchersTests {
|
||||
MockRestRequestMatchers.queryParam("foo", everyItem(startsWith("ba"))).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", hasSize(2)).match(this.request);
|
||||
|
||||
//these can be a bit ambiguous when reading the test (the compiler selects the list matcher):
|
||||
// These can be a bit ambiguous when reading the test (the compiler selects the list matcher):
|
||||
MockRestRequestMatchers.queryParam("foo", notNullValue()).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", is(anything())).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), notNullValue())).match(this.request);
|
||||
|
||||
//these are not as ambiguous thanks to an inner matcher that is either obviously list-oriented,
|
||||
//string-oriented or obviously a vararg of matchers
|
||||
//list matcher version
|
||||
// These are not as ambiguous thanks to an inner matcher that is either obviously list-oriented,
|
||||
// string-oriented, or obviously a vararg of matchers
|
||||
|
||||
// list matcher version
|
||||
MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), hasSize(2))).match(this.request);
|
||||
//vararg version
|
||||
|
||||
// vararg version
|
||||
MockRestRequestMatchers.queryParam("foo", allOf(notNullValue(), endsWith("ar"))).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", is((any(String.class)))).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", CoreMatchers.either(is("bar")).or(is(nullValue()))).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", either(is("bar")).or(is(nullValue()))).match(this.request);
|
||||
MockRestRequestMatchers.queryParam("foo", is(notNullValue()), is(notNullValue())).match(this.request);
|
||||
}
|
||||
|
||||
@@ -323,23 +328,30 @@ class MockRestRequestMatchersTests {
|
||||
void queryParamListContainsMismatch() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=bar&foo=baz"));
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.queryParam("foo", contains(containsString("ba"))).match(this.request))
|
||||
.withMessage("Request queryParam values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: iterable containing [a string containing \"ba\"]" + System.lineSeparator()
|
||||
+ " but: not matched: \"baz\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", contains(containsString("ba"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Query param [foo] values",
|
||||
"Expected: iterable containing [a string containing \"ba\"]",
|
||||
"but: not matched: \"baz\"");
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.queryParam("foo", hasItem(endsWith("ba"))).match(this.request))
|
||||
.withMessage("Request queryParam values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: a collection containing a string ending with \"ba\"" + System.lineSeparator()
|
||||
+ " but: mismatches were: [was \"bar\", was \"baz\"]");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", hasItem(endsWith("ba"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Query param [foo] values",
|
||||
"Expected: a collection containing a string ending with \"ba\"",
|
||||
"but: mismatches were: [was \"bar\", was \"baz\"]");
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers
|
||||
.queryParam("foo", everyItem(endsWith("ar"))).match(this.request))
|
||||
.withMessage("Request queryParam values for [foo]" + System.lineSeparator()
|
||||
+ "Expected: every item is a string ending with \"ar\"" + System.lineSeparator()
|
||||
+ " but: an item was \"baz\"");
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParam("foo", everyItem(endsWith("ar"))).match(this.request))
|
||||
.withMessageContainingAll(
|
||||
"Query param [foo] values",
|
||||
"Expected: every item is a string ending with \"ar\"",
|
||||
"but: an item was \"baz\"");
|
||||
}
|
||||
|
||||
private static ThrowableTypeAssert<AssertionError> assertThatAssertionError() {
|
||||
return AssertionsForClassTypes.assertThatExceptionOfType(AssertionError.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user