2424
2525import org .hamcrest .Matcher ;
2626
27- import org .springframework .http .HttpHeaders ;
2827import org .springframework .http .HttpMethod ;
2928import org .springframework .http .client .ClientHttpRequest ;
3029import org .springframework .test .util .AssertionErrors ;
3130import org .springframework .test .web .client .MockRestServiceServer ;
3231import org .springframework .test .web .client .RequestMatcher ;
3332import 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 */
0 commit comments