Skip to content

Commit e9806a9

Browse files
committed
Polish contribution and introduce test
See spring-projectsgh-28631
1 parent 9b93508 commit e9806a9

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
6767
this(HttpMethod.POST, urlTemplate, uriVariables);
6868
}
6969

70+
/**
71+
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
72+
* that also accepts an {@link HttpMethod}.
73+
* @since 5.3.22
74+
*/
7075
MockMultipartHttpServletRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... uriVariables) {
7176
super(httpMethod, urlTemplate, uriVariables);
7277
super.contentType(MediaType.MULTIPART_FORM_DATA);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public static MockHttpServletRequestBuilder request(String httpMethod, URI uri)
205205
}
206206

207207
/**
208-
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
208+
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request,
209+
* using POST as the HTTP method.
209210
* @param urlTemplate a URL template; the resulting URL will be encoded
210211
* @param uriVars zero or more URI variables
211212
* @since 5.0
@@ -215,7 +216,8 @@ public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplat
215216
}
216217

217218
/**
218-
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
219+
* Variant of {@link #multipart(String, Object...)} that also accepts an
220+
* {@link HttpMethod}.
219221
* @param httpMethod the HTTP method to use
220222
* @param urlTemplate a URL template; the resulting URL will be encoded
221223
* @param uriVars zero or more URI variables

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.junit.jupiter.params.ParameterizedTest;
3636
import org.junit.jupiter.params.provider.ValueSource;
3737

38+
import org.springframework.http.HttpMethod;
3839
import org.springframework.mock.web.MockMultipartFile;
3940
import org.springframework.mock.web.MockPart;
4041
import org.springframework.stereotype.Controller;
@@ -45,6 +46,7 @@
4546
import org.springframework.validation.BindingResult;
4647
import org.springframework.web.bind.annotation.RequestMapping;
4748
import org.springframework.web.bind.annotation.RequestMethod;
49+
import org.springframework.web.bind.annotation.PutMapping;
4850
import org.springframework.web.bind.annotation.RequestParam;
4951
import org.springframework.web.bind.annotation.RequestPart;
5052
import org.springframework.web.filter.OncePerRequestFilter;
@@ -61,20 +63,30 @@
6163
* @author Rossen Stoyanchev
6264
* @author Juergen Hoeller
6365
* @author Jaebin Joo
66+
* @author Sam Brannen
6467
*/
6568
public class MultipartControllerTests {
6669

6770
@ParameterizedTest
68-
@ValueSource(strings = {"/multipartfile", "/part"})
69-
public void multipartRequestWithSingleFileOrPart(String url) throws Exception {
71+
@ValueSource(strings = {"/multipartfile", "/multipartfile-via-put", "/part"})
72+
void multipartRequestWithSingleFileOrPart(String url) throws Exception {
7073
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
7174

7275
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
7376
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
7477

75-
MockMultipartHttpServletRequestBuilder requestBuilder = (url.endsWith("file") ?
76-
multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent)) :
77-
multipart(url).part(new MockPart("part", "orig", fileContent)));
78+
MockMultipartHttpServletRequestBuilder requestBuilder;
79+
switch (url) {
80+
case "/multipartfile":
81+
requestBuilder = multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent));
82+
break;
83+
case "/multipartfile-via-put":
84+
requestBuilder = multipart(HttpMethod.PUT, url).file(new MockMultipartFile("file", "orig", null, fileContent));
85+
break;
86+
default:
87+
requestBuilder = multipart(url).part(new MockPart("part", "orig", fileContent));
88+
break;
89+
}
7890

7991
standaloneSetup(new MultipartController()).build()
8092
.perform(requestBuilder.file(jsonPart))
@@ -275,6 +287,13 @@ public String processMultipartFile(@RequestParam(required = false) MultipartFile
275287
}
276288

277289
@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
290+
@PutMapping("/multipartfile-via-put")
291+
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
292+
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
293+
294+
return processMultipartFile(file, json, model);
295+
}
296+
278297
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
279298
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
280299

0 commit comments

Comments
 (0)