Skip to content

Commit 443e9ee

Browse files
committed
MockMvc applies StandardMultipartHttpServletRequest wrapper
This is necessary to correctly process multipart requests and resolve @RequestPart arguments and MultipartFile arguments. Closes gh-25602
1 parent 50b20c2 commit 443e9ee

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.web.context.request.async.DeferredResult;
3636
import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
3737
import org.springframework.web.context.request.async.WebAsyncUtils;
38+
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
3839
import org.springframework.web.servlet.DispatcherServlet;
3940
import org.springframework.web.servlet.HandlerExecutionChain;
4041
import org.springframework.web.servlet.ModelAndView;
@@ -67,6 +68,10 @@ public TestDispatcherServlet(WebApplicationContext webApplicationContext) {
6768
protected void service(HttpServletRequest request, HttpServletResponse response)
6869
throws ServletException, IOException {
6970

71+
if (!request.getParts().isEmpty()) {
72+
request = new StandardMultipartHttpServletRequest(request);
73+
}
74+
7075
registerAsyncResultInterceptors(request);
7176

7277
super.service(request, response);
@@ -80,8 +85,9 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
8085
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
8186
Assert.notNull(mockRequest, "Expected MockHttpServletRequest");
8287
asyncContext = (MockAsyncContext) mockRequest.getAsyncContext();
88+
String requestClassName = request.getClass().getName();
8389
Assert.notNull(asyncContext, () ->
84-
"Outer request wrapper " + request.getClass().getName() + " has an AsyncContext," +
90+
"Outer request wrapper " + requestClassName + " has an AsyncContext," +
8591
"but it is not a MockAsyncContext, while the nested " +
8692
mockRequest.getClass().getName() + " does not have an AsyncContext at all.");
8793
}

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

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
3333
import org.springframework.util.Assert;
3434
import org.springframework.util.LinkedMultiValueMap;
3535
import org.springframework.util.MultiValueMap;
36-
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
3736

3837
/**
3938
* Default builder for {@link MockMultipartHttpServletRequest}.
@@ -138,17 +137,9 @@ public Object merge(@Nullable Object parent) {
138137
*/
139138
@Override
140139
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
141-
142140
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
143-
this.files.stream().forEach(request::addFile);
141+
this.files.forEach(request::addFile);
144142
this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);
145-
146-
if (!this.parts.isEmpty()) {
147-
new StandardMultipartHttpServletRequest(request)
148-
.getMultiFileMap().values().stream().flatMap(Collection::stream)
149-
.forEach(request::addFile);
150-
}
151-
152143
return request;
153144
}
154145

spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/MultipartControllerTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -230,7 +230,7 @@ public void multipartRequestWithServletParts() throws Exception {
230230
MockPart filePart = new MockPart("file", "orig", fileContent);
231231

232232
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
233-
MockPart jsonPart = new MockPart("json", "json", json);
233+
MockPart jsonPart = new MockPart("json", json);
234234
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
235235

236236
standaloneSetup(new MultipartController()).build()

0 commit comments

Comments
 (0)