24
24
25
25
import org .hamcrest .Matcher ;
26
26
27
- import org .springframework .http .HttpHeaders ;
28
27
import org .springframework .http .HttpMethod ;
29
28
import org .springframework .http .client .ClientHttpRequest ;
30
29
import org .springframework .test .util .AssertionErrors ;
31
30
import org .springframework .test .web .client .MockRestServiceServer ;
32
31
import org .springframework .test .web .client .RequestMatcher ;
33
32
import org .springframework .util .Assert ;
33
+ import org .springframework .util .MultiValueMap ;
34
+ import org .springframework .web .util .UriComponentsBuilder ;
34
35
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 ;
37
40
38
41
/**
39
42
* Static factory methods for {@link RequestMatcher} classes. Typically used to
@@ -60,6 +63,21 @@ public void match(ClientHttpRequest request) throws AssertionError {
60
63
};
61
64
}
62
65
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
+
63
81
/**
64
82
* Assert the request URI string with the given matcher.
65
83
* @param matcher String matcher for the expected URI
@@ -91,35 +109,69 @@ public void match(ClientHttpRequest request) throws IOException, AssertionError
91
109
}
92
110
93
111
/**
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
96
114
* @return the request matcher
97
115
*/
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" );
100
118
return new RequestMatcher () {
101
119
@ 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 ());
104
122
}
105
123
};
106
124
}
107
125
108
126
/**
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.
112
128
*/
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 ) {
115
131
return new RequestMatcher () {
116
132
@ 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
+ }
119
139
}
120
140
};
121
141
}
122
142
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
+
123
175
/**
124
176
* Assert request header values with the given Hamcrest matcher.
125
177
*/
@@ -128,7 +180,7 @@ public static RequestMatcher header(final String name, final Matcher<? super Str
128
180
return new RequestMatcher () {
129
181
@ Override
130
182
public void match (ClientHttpRequest request ) {
131
- assertHeaderValueCount ( name , request .getHeaders (), matchers .length );
183
+ assertValueCount ( "header" , name , request .getHeaders (), matchers .length );
132
184
for (int i = 0 ; i < matchers .length ; i ++) {
133
185
assertThat ("Request header" , request .getHeaders ().get (name ).get (i ), matchers [i ]);
134
186
}
@@ -143,7 +195,7 @@ public static RequestMatcher header(final String name, final String... expectedV
143
195
return new RequestMatcher () {
144
196
@ Override
145
197
public void match (ClientHttpRequest request ) {
146
- assertHeaderValueCount ( name , request .getHeaders (), expectedValues .length );
198
+ assertValueCount ( "header" , name , request .getHeaders (), expectedValues .length );
147
199
for (int i = 0 ; i < expectedValues .length ; i ++) {
148
200
assertEquals ("Request header + [" + name + "]" ,
149
201
expectedValues [i ], request .getHeaders ().get (name ).get (i ));
@@ -152,13 +204,6 @@ public void match(ClientHttpRequest request) {
152
204
};
153
205
}
154
206
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
-
162
207
/**
163
208
* Access to request body matchers.
164
209
*/
0 commit comments