19
19
import java .io .IOException ;
20
20
import java .nio .charset .StandardCharsets ;
21
21
import java .util .Collections ;
22
+ import java .util .List ;
22
23
import java .util .Map ;
24
+ import java .util .Optional ;
23
25
import javax .servlet .Filter ;
24
26
import javax .servlet .FilterChain ;
25
27
import javax .servlet .ServletException ;
28
30
import javax .servlet .http .HttpServletResponse ;
29
31
import javax .servlet .http .Part ;
30
32
33
+ import org .junit .Assert ;
31
34
import org .junit .Test ;
32
35
33
36
import org .springframework .http .MediaType ;
49
52
50
53
/**
51
54
* @author Rossen Stoyanchev
52
- * @since 5.0
55
+ * @author Juergen Hoeller
53
56
*/
54
57
public class MultipartControllerTests {
55
58
56
59
@ Test
57
- public void multipartRequest () throws Exception {
60
+ public void multipartRequestWithSingleFile () throws Exception {
58
61
byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
59
62
MockMultipartFile filePart = new MockMultipartFile ("file" , "orig" , null , fileContent );
60
63
61
64
byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
62
65
MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
63
66
64
67
standaloneSetup (new MultipartController ()).build ()
65
- .perform (multipart ("/test- multipartfile" ).file (filePart ).file (jsonPart ))
68
+ .perform (multipart ("/multipartfile" ).file (filePart ).file (jsonPart ))
66
69
.andExpect (status ().isFound ())
67
70
.andExpect (model ().attribute ("fileContent" , fileContent ))
68
71
.andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
69
72
}
70
73
74
+ @ Test
75
+ public void multipartRequestWithFileArray () throws Exception {
76
+ byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
77
+ MockMultipartFile filePart1 = new MockMultipartFile ("file" , "orig" , null , fileContent );
78
+ MockMultipartFile filePart2 = new MockMultipartFile ("file" , "orig" , null , fileContent );
79
+
80
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
81
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
82
+
83
+ standaloneSetup (new MultipartController ()).build ()
84
+ .perform (multipart ("/multipartfilearray" ).file (filePart1 ).file (filePart2 ).file (jsonPart ))
85
+ .andExpect (status ().isFound ())
86
+ .andExpect (model ().attribute ("fileContent" , fileContent ))
87
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
88
+ }
89
+
90
+ @ Test
91
+ public void multipartRequestWithFileList () throws Exception {
92
+ byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
93
+ MockMultipartFile filePart1 = new MockMultipartFile ("file" , "orig" , null , fileContent );
94
+ MockMultipartFile filePart2 = new MockMultipartFile ("file" , "orig" , null , fileContent );
95
+
96
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
97
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
98
+
99
+ standaloneSetup (new MultipartController ()).build ()
100
+ .perform (multipart ("/multipartfilelist" ).file (filePart1 ).file (filePart2 ).file (jsonPart ))
101
+ .andExpect (status ().isFound ())
102
+ .andExpect (model ().attribute ("fileContent" , fileContent ))
103
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
104
+ }
105
+
106
+ @ Test
107
+ public void multipartRequestWithOptionalFile () throws Exception {
108
+ byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
109
+ MockMultipartFile filePart = new MockMultipartFile ("file" , "orig" , null , fileContent );
110
+
111
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
112
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
113
+
114
+ standaloneSetup (new MultipartController ()).build ()
115
+ .perform (multipart ("/optionalfile" ).file (filePart ).file (jsonPart ))
116
+ .andExpect (status ().isFound ())
117
+ .andExpect (model ().attribute ("fileContent" , fileContent ))
118
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
119
+ }
120
+
121
+ @ Test
122
+ public void multipartRequestWithOptionalFileNotPresent () throws Exception {
123
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
124
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
125
+
126
+ standaloneSetup (new MultipartController ()).build ()
127
+ .perform (multipart ("/optionalfile" ).file (jsonPart ))
128
+ .andExpect (status ().isFound ())
129
+ .andExpect (model ().attributeDoesNotExist ("fileContent" ))
130
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
131
+ }
132
+
133
+ @ Test
134
+ public void multipartRequestWithOptionalFileArray () throws Exception {
135
+ byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
136
+ MockMultipartFile filePart1 = new MockMultipartFile ("file" , "orig" , null , fileContent );
137
+ MockMultipartFile filePart2 = new MockMultipartFile ("file" , "orig" , null , fileContent );
138
+
139
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
140
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
141
+
142
+ standaloneSetup (new MultipartController ()).build ()
143
+ .perform (multipart ("/optionalfilearray" ).file (filePart1 ).file (filePart2 ).file (jsonPart ))
144
+ .andExpect (status ().isFound ())
145
+ .andExpect (model ().attribute ("fileContent" , fileContent ))
146
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
147
+ }
148
+
149
+ @ Test
150
+ public void multipartRequestWithOptionalFileArrayNotPresent () throws Exception {
151
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
152
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
153
+
154
+ standaloneSetup (new MultipartController ()).build ()
155
+ .perform (multipart ("/optionalfilearray" ).file (jsonPart ))
156
+ .andExpect (status ().isFound ())
157
+ .andExpect (model ().attributeDoesNotExist ("fileContent" ))
158
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
159
+ }
160
+
161
+ @ Test
162
+ public void multipartRequestWithOptionalFileList () throws Exception {
163
+ byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
164
+ MockMultipartFile filePart1 = new MockMultipartFile ("file" , "orig" , null , fileContent );
165
+ MockMultipartFile filePart2 = new MockMultipartFile ("file" , "orig" , null , fileContent );
166
+
167
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
168
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
169
+
170
+ standaloneSetup (new MultipartController ()).build ()
171
+ .perform (multipart ("/optionalfilelist" ).file (filePart1 ).file (filePart2 ).file (jsonPart ))
172
+ .andExpect (status ().isFound ())
173
+ .andExpect (model ().attribute ("fileContent" , fileContent ))
174
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
175
+ }
176
+
177
+ @ Test
178
+ public void multipartRequestWithOptionalFileListNotPresent () throws Exception {
179
+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (StandardCharsets .UTF_8 );
180
+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
181
+
182
+ standaloneSetup (new MultipartController ()).build ()
183
+ .perform (multipart ("/optionalfilelist" ).file (jsonPart ))
184
+ .andExpect (status ().isFound ())
185
+ .andExpect (model ().attributeDoesNotExist ("fileContent" ))
186
+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
187
+ }
188
+
71
189
@ Test
72
190
public void multipartRequestWithServletParts () throws Exception {
73
191
byte [] fileContent = "bar" .getBytes (StandardCharsets .UTF_8 );
@@ -78,7 +196,7 @@ public void multipartRequestWithServletParts() throws Exception {
78
196
jsonPart .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
79
197
80
198
standaloneSetup (new MultipartController ()).build ()
81
- .perform (multipart ("/test- multipartfile" ).part (filePart ).part (jsonPart ))
199
+ .perform (multipart ("/multipartfile" ).part (filePart ).part (jsonPart ))
82
200
.andExpect (status ().isFound ())
83
201
.andExpect (model ().attribute ("fileContent" , fileContent ))
84
202
.andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
@@ -93,34 +211,98 @@ public void multipartRequestWrapped() throws Exception {
93
211
MockMvc mockMvc = standaloneSetup (new MultipartController ()).addFilter (filter ).build ();
94
212
95
213
Map <String , String > jsonMap = Collections .singletonMap ("name" , "yeeeah" );
96
- mockMvc .perform (multipart ("/test- json" ).file (jsonPart )).andExpect (model ().attribute ("json" , jsonMap ));
214
+ mockMvc .perform (multipart ("/json" ).file (jsonPart )).andExpect (model ().attribute ("json" , jsonMap ));
97
215
}
98
216
99
217
100
218
@ Controller
101
219
private static class MultipartController {
102
220
103
- @ RequestMapping (value = "/test- multipartfile" , method = RequestMethod .POST )
221
+ @ RequestMapping (value = "/multipartfile" , method = RequestMethod .POST )
104
222
public String processMultipartFile (@ RequestParam MultipartFile file ,
105
223
@ RequestPart Map <String , String > json , Model model ) throws IOException {
106
224
107
- model .addAttribute ("jsonContent" , json );
108
225
model .addAttribute ("fileContent" , file .getBytes ());
226
+ model .addAttribute ("jsonContent" , json );
109
227
110
228
return "redirect:/index" ;
111
229
}
112
230
113
- @ RequestMapping (value = "/test-part " , method = RequestMethod .POST )
114
- public String processPart (@ RequestParam Part part ,
231
+ @ RequestMapping (value = "/multipartfilearray " , method = RequestMethod .POST )
232
+ public String processMultipartFileArray (@ RequestParam MultipartFile [] file ,
115
233
@ RequestPart Map <String , String > json , Model model ) throws IOException {
116
234
235
+ byte [] content = file [0 ].getBytes ();
236
+ Assert .assertArrayEquals (content , file [1 ].getBytes ());
237
+ model .addAttribute ("fileContent" , content );
117
238
model .addAttribute ("jsonContent" , json );
239
+
240
+ return "redirect:/index" ;
241
+ }
242
+
243
+ @ RequestMapping (value = "/multipartfilelist" , method = RequestMethod .POST )
244
+ public String processMultipartFileList (@ RequestParam List <MultipartFile > file ,
245
+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
246
+
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 );
251
+
252
+ return "redirect:/index" ;
253
+ }
254
+
255
+ @ RequestMapping (value = "/optionalfile" , method = RequestMethod .POST )
256
+ public String processOptionalFile (@ RequestParam Optional <MultipartFile > file ,
257
+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
258
+
259
+ if (file .isPresent ()) {
260
+ model .addAttribute ("fileContent" , file .get ().getBytes ());
261
+ }
262
+ model .addAttribute ("jsonContent" , json );
263
+
264
+ return "redirect:/index" ;
265
+ }
266
+
267
+ @ RequestMapping (value = "/optionalfilearray" , method = RequestMethod .POST )
268
+ public String processOptionalFileArray (@ RequestParam Optional <MultipartFile []> file ,
269
+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
270
+
271
+ if (file .isPresent ()) {
272
+ byte [] content = file .get ()[0 ].getBytes ();
273
+ Assert .assertArrayEquals (content , file .get ()[1 ].getBytes ());
274
+ model .addAttribute ("fileContent" , content );
275
+ }
276
+ model .addAttribute ("jsonContent" , json );
277
+
278
+ return "redirect:/index" ;
279
+ }
280
+
281
+ @ RequestMapping (value = "/optionalfilelist" , method = RequestMethod .POST )
282
+ public String processOptionalFileList (@ RequestParam Optional <List <MultipartFile >> file ,
283
+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
284
+
285
+ if (file .isPresent ()) {
286
+ byte [] content = file .get ().get (0 ).getBytes ();
287
+ Assert .assertArrayEquals (content , file .get ().get (1 ).getBytes ());
288
+ model .addAttribute ("fileContent" , content );
289
+ }
290
+ model .addAttribute ("jsonContent" , json );
291
+
292
+ return "redirect:/index" ;
293
+ }
294
+
295
+ @ RequestMapping (value = "/part" , method = RequestMethod .POST )
296
+ public String processPart (@ RequestParam Part part ,
297
+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
298
+
118
299
model .addAttribute ("fileContent" , part .getInputStream ());
300
+ model .addAttribute ("jsonContent" , json );
119
301
120
302
return "redirect:/index" ;
121
303
}
122
304
123
- @ RequestMapping (value = "/test- json" , method = RequestMethod .POST )
305
+ @ RequestMapping (value = "/json" , method = RequestMethod .POST )
124
306
public String processMultipart (@ RequestPart Map <String , String > json , Model model ) {
125
307
model .addAttribute ("json" , json );
126
308
return "redirect:/index" ;
@@ -139,4 +321,4 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
139
321
}
140
322
}
141
323
142
- }
324
+ }
0 commit comments