Skip to content

Commit 7c47b47

Browse files
committed
MockMvc allows HttpMethod input for multipart request
Closes gh-28545
1 parent 479ef3f commit 7c47b47

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcHttpConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -152,7 +152,7 @@ private MockHttpServletRequestBuilder initRequestBuilder(
152152
}
153153

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

157157
Assert.notNull(bytes, "No multipart content");
158158
ReactiveHttpInputMessage inputMessage = MockServerHttpRequest.post(uri.toString())

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,19 +69,25 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
6969
}
7070

7171
/**
72-
* Package-private constructor. Use static factory methods in
73-
* {@link MockMvcRequestBuilders}.
74-
* <p>For other ways to initialize a {@code MockMultipartHttpServletRequest},
75-
* see {@link #with(RequestPostProcessor)} and the
76-
* {@link RequestPostProcessor} extension point.
77-
* @param uri the URL
72+
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
73+
* with a {@link URI}.
7874
* @since 4.0.3
7975
*/
8076
MockMultipartHttpServletRequestBuilder(URI uri) {
8177
super(HttpMethod.POST, uri);
8278
super.contentType(MediaType.MULTIPART_FORM_DATA);
8379
}
8480

81+
/**
82+
* Variant of {@link #MockMultipartHttpServletRequestBuilder(String, Object...)}
83+
* with a {@link URI} and an {@link HttpMethod}.
84+
* @since 5.3.21
85+
*/
86+
MockMultipartHttpServletRequestBuilder(HttpMethod httpMethod, URI uri) {
87+
super(httpMethod, uri);
88+
super.contentType(MediaType.MULTIPART_FORM_DATA);
89+
}
90+
8591

8692
/**
8793
* Create a new MockMultipartFile with the given content.

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -215,14 +215,25 @@ public static MockMultipartHttpServletRequestBuilder multipart(String urlTemplat
215215
}
216216

217217
/**
218-
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
218+
* Variant of {@link #multipart(String, Object...)} with a {@link URI}.
219219
* @param uri the URL
220220
* @since 5.0
221221
*/
222222
public static MockMultipartHttpServletRequestBuilder multipart(URI uri) {
223223
return new MockMultipartHttpServletRequestBuilder(uri);
224224
}
225225

226+
/**
227+
* Variant of {@link #multipart(String, Object...)} with a {@link URI} and
228+
* an {@link HttpMethod}.
229+
* @param httpMethod the HTTP method to use
230+
* @param uri the URL
231+
* @since 5.3.21
232+
*/
233+
public static MockMultipartHttpServletRequestBuilder multipart(HttpMethod httpMethod, URI uri) {
234+
return new MockMultipartHttpServletRequestBuilder(httpMethod, uri);
235+
}
236+
226237
/**
227238
* Create a {@link MockMultipartHttpServletRequestBuilder} for a multipart request.
228239
* @param urlTemplate a URL template; the resulting URL will be encoded

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,8 +39,8 @@
3939
import org.springframework.test.web.reactive.server.WebTestClient;
4040
import org.springframework.test.web.servlet.client.MockMvcWebTestClient;
4141
import org.springframework.ui.Model;
42-
import org.springframework.web.bind.annotation.RequestMapping;
43-
import org.springframework.web.bind.annotation.RequestMethod;
42+
import org.springframework.web.bind.annotation.PostMapping;
43+
import org.springframework.web.bind.annotation.PutMapping;
4444
import org.springframework.web.bind.annotation.RequestParam;
4545
import org.springframework.web.bind.annotation.RequestPart;
4646
import org.springframework.web.filter.OncePerRequestFilter;
@@ -80,6 +80,18 @@ public void multipartRequestWithSingleFile() throws Exception {
8080
MockMvcWebTestClient.resultActionsFor(exchangeResult)
8181
.andExpect(model().attribute("fileContent", fileContent))
8282
.andExpect(model().attribute("jsonContent", json));
83+
84+
// Now try the same with HTTP PUT
85+
exchangeResult = testClient.put().uri("/multipartfile-via-put")
86+
.bodyValue(bodyBuilder.build())
87+
.exchange()
88+
.expectStatus().isFound()
89+
.expectBody().isEmpty();
90+
91+
// Further assertions on the server response
92+
MockMvcWebTestClient.resultActionsFor(exchangeResult)
93+
.andExpect(model().attribute("fileContent", fileContent))
94+
.andExpect(model().attribute("jsonContent", json));
8395
}
8496

8597
@Test
@@ -284,10 +296,11 @@ public void multipartRequestWrapped() throws Exception {
284296
}
285297

286298

299+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
287300
@Controller
288301
private static class MultipartController {
289302

290-
@RequestMapping(value = "/multipartfile", method = RequestMethod.POST)
303+
@PostMapping("/multipartfile")
291304
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
292305
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
293306

@@ -301,7 +314,14 @@ public String processMultipartFile(@RequestParam(required = false) MultipartFile
301314
return "redirect:/index";
302315
}
303316

304-
@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
317+
@PutMapping("/multipartfile-via-put")
318+
public String processMultipartFileViaHttpPut(@RequestParam(required = false) MultipartFile file,
319+
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
320+
321+
return processMultipartFile(file, json, model);
322+
}
323+
324+
@PostMapping("/multipartfilearray")
305325
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
306326
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
307327

@@ -317,7 +337,7 @@ public String processMultipartFileArray(@RequestParam(required = false) Multipar
317337
return "redirect:/index";
318338
}
319339

320-
@RequestMapping(value = "/multipartfilelist", method = RequestMethod.POST)
340+
@PostMapping("/multipartfilelist")
321341
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
322342
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
323343

@@ -333,7 +353,7 @@ public String processMultipartFileList(@RequestParam(required = false) List<Mult
333353
return "redirect:/index";
334354
}
335355

336-
@RequestMapping(value = "/optionalfile", method = RequestMethod.POST)
356+
@PostMapping("/optionalfile")
337357
public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
338358
@RequestPart Map<String, String> json, Model model) throws IOException {
339359

@@ -345,7 +365,7 @@ public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
345365
return "redirect:/index";
346366
}
347367

348-
@RequestMapping(value = "/optionalfilearray", method = RequestMethod.POST)
368+
@PostMapping("/optionalfilearray")
349369
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
350370
@RequestPart Map<String, String> json, Model model) throws IOException {
351371

@@ -359,7 +379,7 @@ public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> f
359379
return "redirect:/index";
360380
}
361381

362-
@RequestMapping(value = "/optionalfilelist", method = RequestMethod.POST)
382+
@PostMapping("/optionalfilelist")
363383
public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>> file,
364384
@RequestPart Map<String, String> json, Model model) throws IOException {
365385

@@ -373,7 +393,7 @@ public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>
373393
return "redirect:/index";
374394
}
375395

376-
@RequestMapping(value = "/part", method = RequestMethod.POST)
396+
@PostMapping("/part")
377397
public String processPart(@RequestParam Part part,
378398
@RequestPart Map<String, String> json, Model model) throws IOException {
379399

@@ -383,7 +403,7 @@ public String processPart(@RequestParam Part part,
383403
return "redirect:/index";
384404
}
385405

386-
@RequestMapping(value = "/json", method = RequestMethod.POST)
406+
@PostMapping("/json")
387407
public String processMultipart(@RequestPart Map<String, String> json, Model model) {
388408
model.addAttribute("json", json);
389409
return "redirect:/index";

0 commit comments

Comments
 (0)