Introduce queryParamCount() in MockRestRequestMatchers
Closes gh-34703
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.test.web.client.match;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.xpath.XPathExpressionException;
|
||||
|
||||
@@ -130,6 +131,7 @@ public abstract class MockRestRequestMatchers {
|
||||
* @since 5.3.27
|
||||
* @see #queryParam(String, Matcher...)
|
||||
* @see #queryParam(String, String...)
|
||||
* @see #queryParamCount(int)
|
||||
*/
|
||||
public static RequestMatcher queryParamList(String name, Matcher<? super List<String>> matcher) {
|
||||
return request -> {
|
||||
@@ -158,6 +160,7 @@ public abstract class MockRestRequestMatchers {
|
||||
* parameter value
|
||||
* @see #queryParamList(String, Matcher)
|
||||
* @see #queryParam(String, String...)
|
||||
* @see #queryParamCount(int)
|
||||
*/
|
||||
@SafeVarargs
|
||||
@SuppressWarnings("NullAway") // Dataflow analysis limitation
|
||||
@@ -187,6 +190,7 @@ public abstract class MockRestRequestMatchers {
|
||||
* parameter value
|
||||
* @see #queryParamList(String, Matcher)
|
||||
* @see #queryParam(String, Matcher...)
|
||||
* @see #queryParamCount(int)
|
||||
*/
|
||||
@SuppressWarnings("NullAway") // Dataflow analysis limitation
|
||||
public static RequestMatcher queryParam(String name, String... expectedValues) {
|
||||
@@ -199,6 +203,25 @@ public abstract class MockRestRequestMatchers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the number of query parameters present in the request.
|
||||
* @param expectedCount the number of expected query parameters
|
||||
* @since 7.0
|
||||
* @see #queryParamList(String, Matcher)
|
||||
* @see #queryParam(String, Matcher...)
|
||||
* @see #queryParam(String, String...)
|
||||
*/
|
||||
@SuppressWarnings("NullAway") // Dataflow analysis limitation
|
||||
public static RequestMatcher queryParamCount(int expectedCount) {
|
||||
return request -> {
|
||||
Set<String> parameterNames = getQueryParams(request).keySet();
|
||||
int actualCount = parameterNames.size();
|
||||
if (expectedCount != actualCount) {
|
||||
fail("Expected %d query parameter(s) but found %d: %s".formatted(expectedCount, actualCount, parameterNames));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static MultiValueMap<String, String> getQueryParams(ClientHttpRequest request) {
|
||||
return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams();
|
||||
}
|
||||
@@ -359,7 +382,6 @@ public abstract class MockRestRequestMatchers {
|
||||
|
||||
|
||||
private static void assertValueCount(String name, MultiValueMap<String, String> map, int count) {
|
||||
|
||||
List<String> values = map.get(name);
|
||||
String message = "Expected query param <" + name + ">";
|
||||
if (values == null) {
|
||||
@@ -371,7 +393,6 @@ public abstract class MockRestRequestMatchers {
|
||||
}
|
||||
|
||||
private static void assertValueCount(String name, HttpHeaders headers, int count) {
|
||||
|
||||
List<String> values = headers.get(name);
|
||||
String message = "Expected header <" + name + ">";
|
||||
if (values == null) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -287,7 +287,6 @@ class MockRestRequestMatchersTests {
|
||||
.withMessageContaining("was \"bar\"");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void queryParamListMissing() {
|
||||
assertThatAssertionError()
|
||||
@@ -353,6 +352,34 @@ class MockRestRequestMatchersTests {
|
||||
"but: was <[bar, baz]>");
|
||||
}
|
||||
|
||||
@Test // gh-34703
|
||||
void queryParamCount() throws Exception {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a"));
|
||||
MockRestRequestMatchers.queryParamCount(0).match(this.request);
|
||||
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?"));
|
||||
MockRestRequestMatchers.queryParamCount(0).match(this.request);
|
||||
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=1"));
|
||||
MockRestRequestMatchers.queryParamCount(1).match(this.request);
|
||||
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=1&foo=2"));
|
||||
MockRestRequestMatchers.queryParamCount(1).match(this.request);
|
||||
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=1&baz=2"));
|
||||
MockRestRequestMatchers.queryParamCount(2).match(this.request);
|
||||
}
|
||||
|
||||
@Test // gh-34703
|
||||
void queryParamCountMismatch() {
|
||||
this.request.setURI(URI.create("http://www.foo.example/a?foo=1&baz=2"));
|
||||
|
||||
assertThatAssertionError()
|
||||
.isThrownBy(() -> MockRestRequestMatchers.queryParamCount(1).match(this.request))
|
||||
.withMessage("Expected 1 query parameter(s) but found 2: [foo, baz]");
|
||||
}
|
||||
|
||||
|
||||
private static ThrowableTypeAssert<AssertionError> assertThatAssertionError() {
|
||||
return assertThatExceptionOfType(AssertionError.class);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user