Merge branch '5.3.x'
# Conflicts: # spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.java
This commit is contained in:
@@ -34,6 +34,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.mock.web.MockPart;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -42,8 +43,8 @@ import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequ
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
@@ -60,20 +61,30 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standal
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
* @author Jaebin Joo
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class MultipartControllerTests {
|
||||
class MultipartControllerTests {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/multipartfile", "/part"})
|
||||
public void multipartRequestWithSingleFileOrPart(String url) throws Exception {
|
||||
@ValueSource(strings = {"/multipartfile", "/multipartfile-via-put", "/part"})
|
||||
void multipartRequestWithSingleFileOrPart(String url) throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
|
||||
|
||||
MockMultipartHttpServletRequestBuilder requestBuilder = (url.endsWith("file") ?
|
||||
multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent)) :
|
||||
multipart(url).part(new MockPart("part", "orig", fileContent)));
|
||||
MockMultipartHttpServletRequestBuilder requestBuilder;
|
||||
switch (url) {
|
||||
case "/multipartfile":
|
||||
requestBuilder = multipart(url).file(new MockMultipartFile("file", "orig", null, fileContent));
|
||||
break;
|
||||
case "/multipartfile-via-put":
|
||||
requestBuilder = multipart(HttpMethod.PUT, url).file(new MockMultipartFile("file", "orig", null, fileContent));
|
||||
break;
|
||||
default:
|
||||
requestBuilder = multipart(url).part(new MockPart("part", "orig", fileContent));
|
||||
break;
|
||||
}
|
||||
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(requestBuilder.file(jsonPart))
|
||||
@@ -83,14 +94,14 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithSingleFileNotPresent() throws Exception {
|
||||
void multipartRequestWithSingleFileNotPresent() throws Exception {
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(multipart("/multipartfile"))
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileArray() throws Exception {
|
||||
void multipartRequestWithFileArray() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
@@ -106,21 +117,21 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileArrayNotPresent() throws Exception {
|
||||
void multipartRequestWithFileArrayNotPresent() throws Exception {
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(multipart("/multipartfilearray"))
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileArrayNoMultipart() throws Exception {
|
||||
void multipartRequestWithFileArrayNoMultipart() throws Exception {
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(post("/multipartfilearray"))
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileList() throws Exception {
|
||||
void multipartRequestWithFileList() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
@@ -136,21 +147,21 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileListNotPresent() throws Exception {
|
||||
void multipartRequestWithFileListNotPresent() throws Exception {
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(multipart("/multipartfilelist"))
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithFileListNoMultipart() throws Exception {
|
||||
void multipartRequestWithFileListNoMultipart() throws Exception {
|
||||
standaloneSetup(new MultipartController()).build()
|
||||
.perform(post("/multipartfilelist"))
|
||||
.andExpect(status().isFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFile() throws Exception {
|
||||
void multipartRequestWithOptionalFile() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
|
||||
@@ -165,7 +176,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFileNotPresent() throws Exception {
|
||||
void multipartRequestWithOptionalFileNotPresent() throws Exception {
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
|
||||
|
||||
@@ -177,7 +188,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFileArray() throws Exception {
|
||||
void multipartRequestWithOptionalFileArray() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
@@ -193,7 +204,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
|
||||
void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
|
||||
|
||||
@@ -205,7 +216,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFileList() throws Exception {
|
||||
void multipartRequestWithOptionalFileList() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
|
||||
@@ -221,7 +232,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
|
||||
void multipartRequestWithOptionalFileListNotPresent() throws Exception {
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
|
||||
|
||||
@@ -233,7 +244,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipartRequestWithDataBindingToFile() throws Exception {
|
||||
void multipartRequestWithDataBindingToFile() throws Exception {
|
||||
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
|
||||
MockPart filePart = new MockPart("file", "orig", fileContent);
|
||||
|
||||
@@ -244,7 +255,7 @@ public class MultipartControllerTests {
|
||||
}
|
||||
|
||||
@Test // SPR-13317
|
||||
public void multipartRequestWrapped() throws Exception {
|
||||
void multipartRequestWrapped() throws Exception {
|
||||
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
|
||||
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
|
||||
|
||||
@@ -259,7 +270,7 @@ public class MultipartControllerTests {
|
||||
@Controller
|
||||
private static class MultipartController {
|
||||
|
||||
@RequestMapping(value = "/multipartfile", method = RequestMethod.POST)
|
||||
@PostMapping("/multipartfile")
|
||||
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
|
||||
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -273,7 +284,14 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
|
||||
@PutMapping("/multipartfile-via-put")
|
||||
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
|
||||
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
return processMultipartFile(file, json, model);
|
||||
}
|
||||
|
||||
@PostMapping("/multipartfilearray")
|
||||
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
|
||||
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -289,7 +307,7 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/multipartfilelist", method = RequestMethod.POST)
|
||||
@PostMapping("/multipartfilelist")
|
||||
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
|
||||
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -305,7 +323,7 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/optionalfile", method = RequestMethod.POST)
|
||||
@PostMapping("/optionalfile")
|
||||
public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
|
||||
@RequestPart Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -317,7 +335,7 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/optionalfilearray", method = RequestMethod.POST)
|
||||
@PostMapping("/optionalfilearray")
|
||||
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
|
||||
@RequestPart Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -331,7 +349,7 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/optionalfilelist", method = RequestMethod.POST)
|
||||
@PostMapping("/optionalfilelist")
|
||||
public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>> file,
|
||||
@RequestPart Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -345,7 +363,7 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/part", method = RequestMethod.POST)
|
||||
@PostMapping("/part")
|
||||
public String processPart(@RequestPart Part part,
|
||||
@RequestPart Map<String, String> json, Model model) throws IOException {
|
||||
|
||||
@@ -358,13 +376,13 @@ public class MultipartControllerTests {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/json", method = RequestMethod.POST)
|
||||
@PostMapping("/json")
|
||||
public String processMultipart(@RequestPart Map<String, String> json, Model model) {
|
||||
model.addAttribute("json", json);
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/multipartfilebinding", method = RequestMethod.POST)
|
||||
@PostMapping("/multipartfilebinding")
|
||||
public String processMultipartFileBean(
|
||||
MultipartFileBean multipartFileBean, Model model, BindingResult bindingResult) throws IOException {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user