Skip to content

Commit

Permalink
MockMvc allows HttpMethod input for multipart request
Browse files Browse the repository at this point in the history
Closes gh-28545
  • Loading branch information
rstoyanchev committed Jun 8, 2022
1 parent 479ef3f commit 7c47b47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
@@ -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.
Expand Down Expand Up @@ -152,7 +152,7 @@ private MockHttpServletRequestBuilder initRequestBuilder(
}

// Parse the multipart request in order to adapt to Servlet Part's
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(uri);
MockMultipartHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(httpMethod, uri);

Assert.notNull(bytes, "No multipart content");
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString())
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -69,19 +69,25 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
}

/**
* Package-private constructor. Use static factory methods in
* {@link MockMvcRequestBuilders}.
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
* see {@link #with(RequestPostProcessor)} and the
* {@link RequestPostProcessor} extension point.
* @param uri the URL
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
* with a {@link URI}.
* @since 4.0.3
*/
MockMultipartHttpServletRequestBuilder(URI uri) {
super(HttpMethod.POST, uri);
super.contentType(MediaType.MULTIPART_FORM_DATA);
}

/**
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
* with a {@link URI} and an {@link HttpMethod}.
* @since 5.3.21
*/
MockMultipartHttpServletRequestBuilder(HttpMethod httpMethod, URI uri) {
super(httpMethod, uri);
super.contentType(MediaType.MULTIPART_FORM_DATA);
}


/**
* Create a new MockMultipartFile with the given content.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -215,14 +215,25 @@ public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplat
}

/**
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
* Variant of {@link #multipart(String, Object...)} with a {@link URI}.
* @param uri the URL
* @since 5.0
*/
public static MockMultipartHttpServletRequestBuilder multipart(URI uri) {
return new MockMultipartHttpServletRequestBuilder(uri);
}

/**
* Variant of {@link #multipart(String, Object...)} with a {@link URI} and
* an {@link HttpMethod}.
* @param httpMethod the HTTP method to use
* @param uri the URL
* @since 5.3.21
*/
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, URI uri) {
return new MockMultipartHttpServletRequestBuilder(httpMethod, uri);
}

/**
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
* @param urlTemplate a URL template; the resulting URL will be encoded
Expand Down
@@ -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.
Expand Down Expand Up @@ -39,8 +39,8 @@
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;
Expand Down Expand Up @@ -80,6 +80,18 @@ public void multipartRequestWithSingleFile() throws Exception {
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
Expand Down Expand Up @@ -284,10 +296,11 @@ public void multipartRequestWrapped() throws Exception {
}


@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 {

Expand All @@ -301,7 +314,14 @@ public String processMultipartFile(@RequestParam(required = false) MultipartFile
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 {

Expand All @@ -317,7 +337,7 @@ public String processMultipartFileArray(@RequestParam(required = false) Multipar
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 {

Expand All @@ -333,7 +353,7 @@ public String processMultipartFileList(@RequestParam(required = false) List<Mult
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 {

Expand All @@ -345,7 +365,7 @@ public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
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 {

Expand All @@ -359,7 +379,7 @@ public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> f
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 {

Expand All @@ -373,7 +393,7 @@ public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>
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 {

Expand All @@ -383,7 +403,7 @@ public String processPart(@RequestParam Part part,
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";
Expand Down

0 comments on commit 7c47b47

Please sign in to comment.