MockMvc allows HttpMethod input for multipart request

Closes gh-28545
This commit is contained in:
rstoyanchev
2022-06-06 17:04:07 +01:00
parent 479ef3f3fd
commit 7c47b470ff
4 changed files with 59 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,8 +39,8 @@ import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
import org.springframework.ui.Model;
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;
@@ -80,6 +80,18 @@ public class MultipartControllerTests {
MockMvcWebTestClient.resultActionsFor(exchangeResult)
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", json));
// Now try the same with HTTP PUT
exchangeResult = testClient.put().uri("/multipartfile-via-put")
.bodyValue(bodyBuilder.build())
.exchange()
.expectStatus().isFound()
.expectBody().isEmpty();
// Further assertions on the server response
MockMvcWebTestClient.resultActionsFor(exchangeResult)
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", json));
}
@Test
@@ -284,10 +296,11 @@ public class MultipartControllerTests {
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@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 {
@@ -301,7 +314,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 {
@@ -317,7 +337,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 {
@@ -333,7 +353,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 {
@@ -345,7 +365,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 {
@@ -359,7 +379,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 {
@@ -373,7 +393,7 @@ public class MultipartControllerTests {
return "redirect:/index";
}
@RequestMapping(value = "/part", method = RequestMethod.POST)
@PostMapping("/part")
public String processPart(@RequestParam Part part,
@RequestPart Map<String, String> json, Model model) throws IOException {
@@ -383,7 +403,7 @@ 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";