19
19
import java .net .URI ;
20
20
import java .util .List ;
21
21
import java .util .Map ;
22
+ import java .util .Set ;
22
23
23
24
import javax .xml .xpath .XPathExpressionException ;
24
25
@@ -130,6 +131,7 @@ public static RequestMatcher requestTo(URI uri) {
130
131
* @since 5.3.27
131
132
* @see #queryParam(String, Matcher...)
132
133
* @see #queryParam(String, String...)
134
+ * @see #queryParamCount(int)
133
135
*/
134
136
public static RequestMatcher queryParamList (String name , Matcher <? super List <String >> matcher ) {
135
137
return request -> {
@@ -158,6 +160,7 @@ public static RequestMatcher queryParamList(String name, Matcher<? super List<St
158
160
* parameter value
159
161
* @see #queryParamList(String, Matcher)
160
162
* @see #queryParam(String, String...)
163
+ * @see #queryParamCount(int)
161
164
*/
162
165
@ SafeVarargs
163
166
@ SuppressWarnings ("NullAway" ) // Dataflow analysis limitation
@@ -187,6 +190,7 @@ public static RequestMatcher queryParam(String name, Matcher<? super String>...
187
190
* parameter value
188
191
* @see #queryParamList(String, Matcher)
189
192
* @see #queryParam(String, Matcher...)
193
+ * @see #queryParamCount(int)
190
194
*/
191
195
@ SuppressWarnings ("NullAway" ) // Dataflow analysis limitation
192
196
public static RequestMatcher queryParam (String name , String ... expectedValues ) {
@@ -199,6 +203,25 @@ public static RequestMatcher queryParam(String name, String... expectedValues) {
199
203
};
200
204
}
201
205
206
+ /**
207
+ * Assert the number of query parameters present in the request.
208
+ * @param expectedCount the number of expected query parameters
209
+ * @since 7.0
210
+ * @see #queryParamList(String, Matcher)
211
+ * @see #queryParam(String, Matcher...)
212
+ * @see #queryParam(String, String...)
213
+ */
214
+ @ SuppressWarnings ("NullAway" ) // Dataflow analysis limitation
215
+ public static RequestMatcher queryParamCount (int expectedCount ) {
216
+ return request -> {
217
+ Set <String > parameterNames = getQueryParams (request ).keySet ();
218
+ int actualCount = parameterNames .size ();
219
+ if (expectedCount != actualCount ) {
220
+ fail ("Expected %d query parameter(s) but found %d: %s" .formatted (expectedCount , actualCount , parameterNames ));
221
+ }
222
+ };
223
+ }
224
+
202
225
private static MultiValueMap <String , String > getQueryParams (ClientHttpRequest request ) {
203
226
return UriComponentsBuilder .fromUri (request .getURI ()).build ().getQueryParams ();
204
227
}
@@ -359,7 +382,6 @@ public static XpathRequestMatchers xpath(String expression, Map<String, String>
359
382
360
383
361
384
private static void assertValueCount (String name , MultiValueMap <String , String > map , int count ) {
362
-
363
385
List <String > values = map .get (name );
364
386
String message = "Expected query param <" + name + ">" ;
365
387
if (values == null ) {
@@ -371,7 +393,6 @@ else if (count > values.size()) {
371
393
}
372
394
373
395
private static void assertValueCount (String name , HttpHeaders headers , int count ) {
374
-
375
396
List <String > values = headers .get (name );
376
397
String message = "Expected header <" + name + ">" ;
377
398
if (values == null ) {
0 commit comments