Skip to content

Commit 8f6d3fe

Browse files
committed
Tests for non-required MultipartFile parameters
Issue: SPR-16329
1 parent 121f9e3 commit 8f6d3fe

File tree

1 file changed

+64
-17
lines changed

1 file changed

+64
-17
lines changed

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

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -71,6 +71,13 @@ public void multipartRequestWithSingleFile() throws Exception {
7171
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
7272
}
7373

74+
@Test
75+
public void multipartRequestWithSingleFileNotPresent() throws Exception {
76+
standaloneSetup(new MultipartController()).build()
77+
.perform(multipart("/multipartfile"))
78+
.andExpect(status().isFound());
79+
}
80+
7481
@Test
7582
public void multipartRequestWithFileArray() throws Exception {
7683
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
@@ -87,6 +94,20 @@ public void multipartRequestWithFileArray() throws Exception {
8794
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
8895
}
8996

97+
@Test
98+
public void multipartRequestWithFileArrayNotPresent() throws Exception {
99+
standaloneSetup(new MultipartController()).build()
100+
.perform(multipart("/multipartfilearray"))
101+
.andExpect(status().isFound());
102+
}
103+
104+
@Test
105+
public void multipartRequestWithFileArrayNoMultipart() throws Exception {
106+
standaloneSetup(new MultipartController()).build()
107+
.perform(post("/multipartfilearray"))
108+
.andExpect(status().isFound());
109+
}
110+
90111
@Test
91112
public void multipartRequestWithFileList() throws Exception {
92113
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
@@ -103,6 +124,20 @@ public void multipartRequestWithFileList() throws Exception {
103124
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
104125
}
105126

127+
@Test
128+
public void multipartRequestWithFileListNotPresent() throws Exception {
129+
standaloneSetup(new MultipartController()).build()
130+
.perform(multipart("/multipartfilelist"))
131+
.andExpect(status().isFound());
132+
}
133+
134+
@Test
135+
public void multipartRequestWithFileListNoMultipart() throws Exception {
136+
standaloneSetup(new MultipartController()).build()
137+
.perform(post("/multipartfilelist"))
138+
.andExpect(status().isFound());
139+
}
140+
106141
@Test
107142
public void multipartRequestWithOptionalFile() throws Exception {
108143
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
@@ -219,35 +254,47 @@ public void multipartRequestWrapped() throws Exception {
219254
private static class MultipartController {
220255

221256
@RequestMapping(value = "/multipartfile", method = RequestMethod.POST)
222-
public String processMultipartFile(@RequestParam MultipartFile file,
223-
@RequestPart Map<String, String> json, Model model) throws IOException {
257+
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
258+
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
224259

225-
model.addAttribute("fileContent", file.getBytes());
226-
model.addAttribute("jsonContent", json);
260+
if (file != null) {
261+
model.addAttribute("fileContent", file.getBytes());
262+
}
263+
if (json != null) {
264+
model.addAttribute("jsonContent", json);
265+
}
227266

228267
return "redirect:/index";
229268
}
230269

231270
@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
232-
public String processMultipartFileArray(@RequestParam MultipartFile[] file,
233-
@RequestPart Map<String, String> json, Model model) throws IOException {
271+
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
272+
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
234273

235-
byte[] content = file[0].getBytes();
236-
Assert.assertArrayEquals(content, file[1].getBytes());
237-
model.addAttribute("fileContent", content);
238-
model.addAttribute("jsonContent", json);
274+
if (file != null && file.length > 0) {
275+
byte[] content = file[0].getBytes();
276+
Assert.assertArrayEquals(content, file[1].getBytes());
277+
model.addAttribute("fileContent", content);
278+
}
279+
if (json != null) {
280+
model.addAttribute("jsonContent", json);
281+
}
239282

240283
return "redirect:/index";
241284
}
242285

243286
@RequestMapping(value = "/multipartfilelist", method = RequestMethod.POST)
244-
public String processMultipartFileList(@RequestParam List<MultipartFile> file,
245-
@RequestPart Map<String, String> json, Model model) throws IOException {
287+
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
288+
@RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
246289

247-
byte[] content = file.get(0).getBytes();
248-
Assert.assertArrayEquals(content, file.get(1).getBytes());
249-
model.addAttribute("fileContent", content);
250-
model.addAttribute("jsonContent", json);
290+
if (file != null && !file.isEmpty()) {
291+
byte[] content = file.get(0).getBytes();
292+
Assert.assertArrayEquals(content, file.get(1).getBytes());
293+
model.addAttribute("fileContent", content);
294+
}
295+
if (json != null) {
296+
model.addAttribute("jsonContent", json);
297+
}
251298

252299
return "redirect:/index";
253300
}

0 commit comments

Comments
 (0)