1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -71,6 +71,13 @@ public void multipartRequestWithSingleFile() throws Exception {
71
71
.andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
72
72
}
73
73
74
+ @ Test
75
+ public void multipartRequestWithSingleFileNotPresent () throws Exception {
76
+ standaloneSetup (new MultipartController ()).build ()
77
+ .perform (multipart ("/multipartfile" ))
78
+ .andExpect (status ().isFound ());
79
+ }
80
+
74
81
@ Test
75
82
public void multipartRequestWithFileArray () throws Exception {
76
83
byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
@@ -87,6 +94,20 @@ public void multipartRequestWithFileArray() throws Exception {
87
94
.andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
88
95
}
89
96
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
+
90
111
@ Test
91
112
public void multipartRequestWithFileList () throws Exception {
92
113
byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
@@ -103,6 +124,20 @@ public void multipartRequestWithFileList() throws Exception {
103
124
.andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
104
125
}
105
126
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
+
106
141
@ Test
107
142
public void multipartRequestWithOptionalFile () throws Exception {
108
143
byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
@@ -219,35 +254,47 @@ public void multipartRequestWrapped() throws Exception {
219
254
private static class MultipartController {
220
255
221
256
@ 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 {
224
259
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
+ }
227
266
228
267
return "redirect:/index" ;
229
268
}
230
269
231
270
@ 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 {
234
273
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
+ }
239
282
240
283
return "redirect:/index" ;
241
284
}
242
285
243
286
@ 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 {
246
289
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
+ }
251
298
252
299
return "redirect:/index" ;
253
300
}
0 commit comments