Skip to content

Commit c8aa48f

Browse files
committed
Add params(MultiValueMap) to MockMvc
Issue: SPR-13801
1 parent 5626384 commit c8aa48f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ public MockHttpServletRequestBuilder param(String name, String... values) {
151151
return this;
152152
}
153153

154+
/**
155+
* Add request parameters to the {@link MockHttpServletRequest} for example
156+
* such as when testing a form submission. If called more than once, the new
157+
* values are added.
158+
* @param params the parameters to add
159+
*/
160+
public MockHttpServletRequestBuilder params(MultiValueMap<String, String> params) {
161+
for (String name : params.keySet()) {
162+
for (String value : params.get(name)) {
163+
this.parameters.add(name, value);
164+
}
165+
}
166+
return this;
167+
}
168+
154169
/**
155170
* Add a header to the request. Values are always added.
156171
* @param name the header name

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.springframework.mock.web.MockHttpSession;
4040
import org.springframework.mock.web.MockServletContext;
4141
import org.springframework.util.FileCopyUtils;
42+
import org.springframework.util.LinkedMultiValueMap;
43+
import org.springframework.util.MultiValueMap;
4244
import org.springframework.web.servlet.FlashMap;
4345
import org.springframework.web.servlet.support.SessionFlashMapManager;
4446

@@ -252,6 +254,21 @@ public void requestParameterFromQueryNull() {
252254
assertEquals("foo", request.getQueryString());
253255
}
254256

257+
// SPR-13801
258+
259+
@Test
260+
public void requestParameterFromMultiValueMap() throws Exception {
261+
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
262+
params.add("foo", "bar");
263+
params.add("foo", "baz");
264+
this.builder = new MockHttpServletRequestBuilder(HttpMethod.POST, "/foo");
265+
this.builder.params(params);
266+
267+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
268+
269+
assertArrayEquals(new String[] {"bar", "baz"}, request.getParameterMap().get("foo"));
270+
}
271+
255272
@Test
256273
public void acceptHeader() {
257274
this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML);

0 commit comments

Comments
 (0)