|
|
|
|
@ -18,21 +18,27 @@ package org.springframework.test.web.client.match;
@@ -18,21 +18,27 @@ package org.springframework.test.web.client.match;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import javax.xml.xpath.XPathExpressionException; |
|
|
|
|
|
|
|
|
|
import org.hamcrest.Matcher; |
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
|
import org.springframework.http.HttpMethod; |
|
|
|
|
import org.springframework.http.client.ClientHttpRequest; |
|
|
|
|
import org.springframework.test.util.AssertionErrors; |
|
|
|
|
import org.springframework.test.web.client.MockRestServiceServer; |
|
|
|
|
import org.springframework.test.web.client.RequestMatcher; |
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder; |
|
|
|
|
import org.springframework.web.util.UriUtils; |
|
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8; |
|
|
|
|
import static org.hamcrest.MatcherAssert.*; |
|
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
|
import static org.springframework.test.util.AssertionErrors.*; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -60,6 +66,21 @@ public abstract class MockRestRequestMatchers {
@@ -60,6 +66,21 @@ public abstract class MockRestRequestMatchers {
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert the {@link HttpMethod} of the request. |
|
|
|
|
* @param method the HTTP method |
|
|
|
|
* @return the request matcher |
|
|
|
|
*/ |
|
|
|
|
public static RequestMatcher method(final HttpMethod method) { |
|
|
|
|
Assert.notNull(method, "'method' must not be null"); |
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) throws AssertionError { |
|
|
|
|
AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod()); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert the request URI string with the given matcher. |
|
|
|
|
* @param matcher String matcher for the expected URI |
|
|
|
|
@ -91,35 +112,69 @@ public abstract class MockRestRequestMatchers {
@@ -91,35 +112,69 @@ public abstract class MockRestRequestMatchers {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert the {@link HttpMethod} of the request. |
|
|
|
|
* @param method the HTTP method |
|
|
|
|
* Expect a request to the given URI. |
|
|
|
|
* @param uri the expected URI |
|
|
|
|
* @return the request matcher |
|
|
|
|
*/ |
|
|
|
|
public static RequestMatcher method(final HttpMethod method) { |
|
|
|
|
Assert.notNull(method, "'method' must not be null"); |
|
|
|
|
public static RequestMatcher requestTo(final URI uri) { |
|
|
|
|
Assert.notNull(uri, "'uri' must not be null"); |
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) throws AssertionError { |
|
|
|
|
AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod()); |
|
|
|
|
public void match(ClientHttpRequest request) throws IOException, AssertionError { |
|
|
|
|
AssertionErrors.assertEquals("Unexpected request", uri, request.getURI()); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Expect a request to the given URI. |
|
|
|
|
* @param uri the expected URI |
|
|
|
|
* @return the request matcher |
|
|
|
|
* Assert request query parameter values with the given Hamcrest matcher. |
|
|
|
|
*/ |
|
|
|
|
public static RequestMatcher requestTo(final URI uri) { |
|
|
|
|
Assert.notNull(uri, "'uri' must not be null"); |
|
|
|
|
@SafeVarargs |
|
|
|
|
public static RequestMatcher queryParam(final String name, final Matcher<? super String>... matchers) { |
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) throws IOException, AssertionError { |
|
|
|
|
AssertionErrors.assertEquals("Unexpected request", uri, request.getURI()); |
|
|
|
|
public void match(ClientHttpRequest request) { |
|
|
|
|
MultiValueMap<String, String> params = getQueryParams(request); |
|
|
|
|
assertValueCount("query param", name, params, matchers.length); |
|
|
|
|
for (int i = 0 ; i < matchers.length; i++) { |
|
|
|
|
assertThat("Query param", params.get(name).get(i), matchers[i]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert request query parameter values. |
|
|
|
|
*/ |
|
|
|
|
public static RequestMatcher queryParam(final String name, final String... expectedValues) { |
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) { |
|
|
|
|
MultiValueMap<String, String> params = getQueryParams(request); |
|
|
|
|
assertValueCount("query param", name, params, expectedValues.length); |
|
|
|
|
for (int i = 0 ; i < expectedValues.length; i++) { |
|
|
|
|
assertEquals("Query param + [" + name + "]", expectedValues[i], params.get(name).get(i)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static MultiValueMap<String, String> getQueryParams(ClientHttpRequest request) { |
|
|
|
|
return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void assertValueCount(String valueType, final String name, |
|
|
|
|
MultiValueMap<String, String> map, int count) { |
|
|
|
|
|
|
|
|
|
List<String> values = map.get(name); |
|
|
|
|
|
|
|
|
|
String message = "Expected " + valueType + " <" + name + ">"; |
|
|
|
|
assertNotNull(message, values); |
|
|
|
|
|
|
|
|
|
assertTrue(message + " to have at least <" + count + "> values but found " + values, |
|
|
|
|
count <= values.size()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Assert request header values with the given Hamcrest matcher. |
|
|
|
|
*/ |
|
|
|
|
@ -128,7 +183,7 @@ public abstract class MockRestRequestMatchers {
@@ -128,7 +183,7 @@ public abstract class MockRestRequestMatchers {
|
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) { |
|
|
|
|
assertHeaderValueCount(name, request.getHeaders(), matchers.length); |
|
|
|
|
assertValueCount("header", name, request.getHeaders(), matchers.length); |
|
|
|
|
for (int i = 0 ; i < matchers.length; i++) { |
|
|
|
|
assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]); |
|
|
|
|
} |
|
|
|
|
@ -143,7 +198,7 @@ public abstract class MockRestRequestMatchers {
@@ -143,7 +198,7 @@ public abstract class MockRestRequestMatchers {
|
|
|
|
|
return new RequestMatcher() { |
|
|
|
|
@Override |
|
|
|
|
public void match(ClientHttpRequest request) { |
|
|
|
|
assertHeaderValueCount(name, request.getHeaders(), expectedValues.length); |
|
|
|
|
assertValueCount("header", name, request.getHeaders(), expectedValues.length); |
|
|
|
|
for (int i = 0 ; i < expectedValues.length; i++) { |
|
|
|
|
assertEquals("Request header + [" + name + "]", |
|
|
|
|
expectedValues[i], request.getHeaders().get(name).get(i)); |
|
|
|
|
@ -152,13 +207,6 @@ public abstract class MockRestRequestMatchers {
@@ -152,13 +207,6 @@ public abstract class MockRestRequestMatchers {
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) { |
|
|
|
|
List<String> actualValues = headers.get(name); |
|
|
|
|
AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null); |
|
|
|
|
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount + |
|
|
|
|
"> values but found " + actualValues, expectedCount <= actualValues.size()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Access to request body matchers. |
|
|
|
|
*/ |
|
|
|
|
|