Skip to content

Commit b14d189

Browse files
committed
MockRestRequestMatchers can match query params
Issue: SPR-14995
1 parent 14de29c commit b14d189

File tree

2 files changed

+103
-27
lines changed

2 files changed

+103
-27
lines changed

spring-test/src/main/java/org/springframework/test/web/client/match/MockRestRequestMatchers.java

+70-25
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@
2424

2525
import org.hamcrest.Matcher;
2626

27-
import org.springframework.http.HttpHeaders;
2827
import org.springframework.http.HttpMethod;
2928
import org.springframework.http.client.ClientHttpRequest;
3029
import org.springframework.test.util.AssertionErrors;
3130
import org.springframework.test.web.client.MockRestServiceServer;
3231
import org.springframework.test.web.client.RequestMatcher;
3332
import org.springframework.util.Assert;
33+
import org.springframework.util.MultiValueMap;
34+
import org.springframework.web.util.UriComponentsBuilder;
3435

35-
import static org.hamcrest.MatcherAssert.*;
36-
import static org.springframework.test.util.AssertionErrors.*;
36+
import static org.hamcrest.MatcherAssert.assertThat;
37+
import static org.junit.Assert.assertNotNull;
38+
import static org.springframework.test.util.AssertionErrors.assertEquals;
39+
import static org.springframework.test.util.AssertionErrors.assertTrue;
3740

3841
/**
3942
* Static factory methods for {@link RequestMatcher} classes. Typically used to
@@ -60,6 +63,21 @@ public void match(ClientHttpRequest request) throws AssertionError {
6063
};
6164
}
6265

66+
/**
67+
* Assert the {@link HttpMethod} of the request.
68+
* @param method the HTTP method
69+
* @return the request matcher
70+
*/
71+
public static RequestMatcher method(final HttpMethod method) {
72+
Assert.notNull(method, "'method' must not be null");
73+
return new RequestMatcher() {
74+
@Override
75+
public void match(ClientHttpRequest request) throws AssertionError {
76+
AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod());
77+
}
78+
};
79+
}
80+
6381
/**
6482
* Assert the request URI string with the given matcher.
6583
* @param matcher String matcher for the expected URI
@@ -91,35 +109,69 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
91109
}
92110

93111
/**
94-
* Assert the {@link HttpMethod} of the request.
95-
* @param method the HTTP method
112+
* Expect a request to the given URI.
113+
* @param uri the expected URI
96114
* @return the request matcher
97115
*/
98-
public static RequestMatcher method(final HttpMethod method) {
99-
Assert.notNull(method, "'method' must not be null");
116+
public static RequestMatcher requestTo(final URI uri) {
117+
Assert.notNull(uri, "'uri' must not be null");
100118
return new RequestMatcher() {
101119
@Override
102-
public void match(ClientHttpRequest request) throws AssertionError {
103-
AssertionErrors.assertEquals("Unexpected HttpMethod", method, request.getMethod());
120+
public void match(ClientHttpRequest request) throws IOException, AssertionError {
121+
AssertionErrors.assertEquals("Unexpected request", uri, request.getURI());
104122
}
105123
};
106124
}
107125

108126
/**
109-
* Expect a request to the given URI.
110-
* @param uri the expected URI
111-
* @return the request matcher
127+
* Assert request query parameter values with the given Hamcrest matcher.
112128
*/
113-
public static RequestMatcher requestTo(final URI uri) {
114-
Assert.notNull(uri, "'uri' must not be null");
129+
@SafeVarargs
130+
public static RequestMatcher queryParam(final String name, final Matcher<? super String>... matchers) {
115131
return new RequestMatcher() {
116132
@Override
117-
public void match(ClientHttpRequest request) throws IOException, AssertionError {
118-
AssertionErrors.assertEquals("Unexpected request", uri, request.getURI());
133+
public void match(ClientHttpRequest request) {
134+
MultiValueMap<String, String> params = getQueryParams(request);
135+
assertValueCount("query param", name, params, matchers.length);
136+
for (int i = 0 ; i < matchers.length; i++) {
137+
assertThat("Query param", params.get(name).get(i), matchers[i]);
138+
}
119139
}
120140
};
121141
}
122142

143+
/**
144+
* Assert request query parameter values.
145+
*/
146+
public static RequestMatcher queryParam(final String name, final String... expectedValues) {
147+
return new RequestMatcher() {
148+
@Override
149+
public void match(ClientHttpRequest request) {
150+
MultiValueMap<String, String> params = getQueryParams(request);
151+
assertValueCount("query param", name, params, expectedValues.length);
152+
for (int i = 0 ; i < expectedValues.length; i++) {
153+
assertEquals("Query param + [" + name + "]", expectedValues[i], params.get(name).get(i));
154+
}
155+
}
156+
};
157+
}
158+
159+
private static MultiValueMap<String, String> getQueryParams(ClientHttpRequest request) {
160+
return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams();
161+
}
162+
163+
private static void assertValueCount(String valueType, final String name,
164+
MultiValueMap<String, String> map, int count) {
165+
166+
List<String> values = map.get(name);
167+
168+
String message = "Expected " + valueType + " <" + name + ">";
169+
assertNotNull(message, values);
170+
171+
assertTrue(message + " to have at least <" + count + "> values but found " + values,
172+
count <= values.size());
173+
}
174+
123175
/**
124176
* Assert request header values with the given Hamcrest matcher.
125177
*/
@@ -128,7 +180,7 @@ public static RequestMatcher header(final String name, final Matcher<? super Str
128180
return new RequestMatcher() {
129181
@Override
130182
public void match(ClientHttpRequest request) {
131-
assertHeaderValueCount(name, request.getHeaders(), matchers.length);
183+
assertValueCount("header", name, request.getHeaders(), matchers.length);
132184
for (int i = 0 ; i < matchers.length; i++) {
133185
assertThat("Request header", request.getHeaders().get(name).get(i), matchers[i]);
134186
}
@@ -143,7 +195,7 @@ public static RequestMatcher header(final String name, final String... expectedV
143195
return new RequestMatcher() {
144196
@Override
145197
public void match(ClientHttpRequest request) {
146-
assertHeaderValueCount(name, request.getHeaders(), expectedValues.length);
198+
assertValueCount("header", name, request.getHeaders(), expectedValues.length);
147199
for (int i = 0 ; i < expectedValues.length; i++) {
148200
assertEquals("Request header + [" + name + "]",
149201
expectedValues[i], request.getHeaders().get(name).get(i));
@@ -152,13 +204,6 @@ public void match(ClientHttpRequest request) {
152204
};
153205
}
154206

155-
private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) {
156-
List<String> actualValues = headers.get(name);
157-
AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null);
158-
AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount +
159-
"> values but found " + actualValues, expectedCount <= actualValues.size());
160-
}
161-
162207
/**
163208
* Access to request body matchers.
164209
*/

spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.net.URI;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122

2223
import org.junit.Test;
2324

@@ -124,9 +125,39 @@ public void headersWithMissingHeader() throws Exception {
124125

125126
@Test(expected = AssertionError.class)
126127
public void headersWithMissingValue() throws Exception {
127-
this.request.getHeaders().put("foo", Arrays.asList("bar"));
128+
this.request.getHeaders().put("foo", Collections.singletonList("bar"));
128129

129130
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
130131
}
131132

133+
@Test
134+
public void queryParam() throws Exception {
135+
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
136+
MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request);
137+
}
138+
139+
@Test(expected = AssertionError.class)
140+
public void queryParamMissing() throws Exception {
141+
this.request.setURI(new URI("http://foo.com/a"));
142+
MockRestRequestMatchers.queryParam("foo", "bar").match(this.request);
143+
}
144+
145+
@Test(expected = AssertionError.class)
146+
public void queryParamMissingValue() throws Exception {
147+
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
148+
MockRestRequestMatchers.queryParam("foo", "bad").match(this.request);
149+
}
150+
151+
@Test
152+
public void queryParamContains() throws Exception {
153+
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
154+
MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request);
155+
}
156+
157+
@Test(expected = AssertionError.class)
158+
public void queryParamContainsWithMissingValue() throws Exception {
159+
this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz"));
160+
MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request);
161+
}
162+
132163
}

0 commit comments

Comments
 (0)