|
35 | 35 | import org.junit.jupiter.params.ParameterizedTest; |
36 | 36 | import org.junit.jupiter.params.provider.ValueSource; |
37 | 37 |
|
| 38 | +import org.springframework.http.HttpMethod; |
38 | 39 | import org.springframework.mock.web.MockMultipartFile; |
39 | 40 | import org.springframework.mock.web.MockPart; |
40 | 41 | import org.springframework.stereotype.Controller; |
|
45 | 46 | import org.springframework.validation.BindingResult; |
46 | 47 | import org.springframework.web.bind.annotation.RequestMapping; |
47 | 48 | import org.springframework.web.bind.annotation.RequestMethod; |
| 49 | +import org.springframework.web.bind.annotation.PutMapping; |
48 | 50 | import org.springframework.web.bind.annotation.RequestParam; |
49 | 51 | import org.springframework.web.bind.annotation.RequestPart; |
50 | 52 | import org.springframework.web.filter.OncePerRequestFilter; |
|
61 | 63 | * @author Rossen Stoyanchev |
62 | 64 | * @author Juergen Hoeller |
63 | 65 | * @author Jaebin Joo |
| 66 | + * @author Sam Brannen |
64 | 67 | */ |
65 | 68 | public class MultipartControllerTests { |
66 | 69 |
|
67 | 70 | @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 { |
70 | 73 | byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8); |
71 | 74 |
|
72 | 75 | byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8); |
73 | 76 | MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json); |
74 | 77 |
|
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 | + } |
78 | 90 |
|
79 | 91 | standaloneSetup(new MultipartController()).build() |
80 | 92 | .perform(requestBuilder.file(jsonPart)) |
@@ -275,6 +287,13 @@ public String processMultipartFile(@RequestParam(required = false) MultipartFile |
275 | 287 | } |
276 | 288 |
|
277 | 289 | @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 | + |
278 | 297 | public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file, |
279 | 298 | @RequestPart(required = false) Map<String, String> json, Model model) throws IOException { |
280 | 299 |
|
|
0 commit comments