Merge pull request #1263
This commit is contained in:
commit
a8d834b797
|
@ -18,21 +18,27 @@ package org.springframework.test.web.client.match;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
|
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.client.ClientHttpRequest;
|
import org.springframework.http.client.ClientHttpRequest;
|
||||||
import org.springframework.test.util.AssertionErrors;
|
import org.springframework.test.util.AssertionErrors;
|
||||||
import org.springframework.test.web.client.MockRestServiceServer;
|
import org.springframework.test.web.client.MockRestServiceServer;
|
||||||
import org.springframework.test.web.client.RequestMatcher;
|
import org.springframework.test.web.client.RequestMatcher;
|
||||||
import org.springframework.util.Assert;
|
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.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.springframework.test.util.AssertionErrors.*;
|
import static org.springframework.test.util.AssertionErrors.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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.
|
* Assert the request URI string with the given matcher.
|
||||||
* @param matcher String matcher for the expected URI
|
* @param matcher String matcher for the expected URI
|
||||||
|
@ -90,21 +111,6 @@ 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());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expect a request to the given URI.
|
* Expect a request to the given URI.
|
||||||
* @param uri the expected URI
|
* @param uri the expected URI
|
||||||
|
@ -120,6 +126,55 @@ public abstract class MockRestRequestMatchers {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert request query parameter values with the given Hamcrest matcher.
|
||||||
|
*/
|
||||||
|
@SafeVarargs
|
||||||
|
public static RequestMatcher queryParam(final String name, final Matcher<? super String>... matchers) {
|
||||||
|
return new RequestMatcher() {
|
||||||
|
@Override
|
||||||
|
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.
|
* Assert request header values with the given Hamcrest matcher.
|
||||||
*/
|
*/
|
||||||
|
@ -128,7 +183,7 @@ public abstract class MockRestRequestMatchers {
|
||||||
return new RequestMatcher() {
|
return new RequestMatcher() {
|
||||||
@Override
|
@Override
|
||||||
public void match(ClientHttpRequest request) {
|
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++) {
|
for (int i = 0 ; i < matchers.length; i++) {
|
||||||
assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]);
|
assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]);
|
||||||
}
|
}
|
||||||
|
@ -143,7 +198,7 @@ public abstract class MockRestRequestMatchers {
|
||||||
return new RequestMatcher() {
|
return new RequestMatcher() {
|
||||||
@Override
|
@Override
|
||||||
public void match(ClientHttpRequest request) {
|
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++) {
|
for (int i = 0 ; i < expectedValues.length; i++) {
|
||||||
assertEquals("Request header + [" + name + "]",
|
assertEquals("Request header + [" + name + "]",
|
||||||
expectedValues[i], request.getHeaders().get(name).get(i));
|
expectedValues[i], request.getHeaders().get(name).get(i));
|
||||||
|
@ -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.
|
* Access to request body matchers.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -18,13 +18,14 @@ package org.springframework.test.web.client.match;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link MockRestRequestMatchers}.
|
* Unit tests for {@link MockRestRequestMatchers}.
|
||||||
|
@ -124,9 +125,39 @@ public class MockRestRequestMatchersTests {
|
||||||
|
|
||||||
@Test(expected = AssertionError.class)
|
@Test(expected = AssertionError.class)
|
||||||
public void headersWithMissingValue() throws Exception {
|
public void headersWithMissingValue() throws Exception {
|
||||||
this.request.getHeaders().put("foo", Arrays.asList("bar"));
|
this.request.getHeaders().put("foo", Collections.singletonList("bar"));
|
||||||
|
|
||||||
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void queryParam() throws Exception {
|
||||||
|
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
|
||||||
|
MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void queryParamMissing() throws Exception {
|
||||||
|
this.request.setURI(new URI("http://foo.com/a"));
|
||||||
|
MockRestRequestMatchers.queryParam("foo", "bar").match(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void queryParamMissingValue() throws Exception {
|
||||||
|
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
|
||||||
|
MockRestRequestMatchers.queryParam("foo", "bad").match(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void queryParamContains() throws Exception {
|
||||||
|
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
|
||||||
|
MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = AssertionError.class)
|
||||||
|
public void queryParamContainsWithMissingValue() throws Exception {
|
||||||
|
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
|
||||||
|
MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue