34
34
import org .springframework .http .MediaType ;
35
35
import org .springframework .http .client .MultipartBodyBuilder ;
36
36
import org .springframework .stereotype .Controller ;
37
- import org .springframework .test .web .reactive .server .EntityExchangeResult ;
38
37
import org .springframework .test .web .reactive .server .WebTestClient ;
39
38
import org .springframework .test .web .servlet .client .MockMvcWebTestClient ;
40
- import org .springframework .ui .Model ;
41
39
import org .springframework .web .bind .annotation .PostMapping ;
42
40
import org .springframework .web .bind .annotation .PutMapping ;
43
41
import org .springframework .web .bind .annotation .RequestParam ;
46
44
import org .springframework .web .multipart .MultipartFile ;
47
45
48
46
import static org .assertj .core .api .Assertions .assertThat ;
49
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .model ;
50
47
51
48
/**
52
49
* {@link MockMvcWebTestClient} equivalent of the MockMvc
@@ -69,28 +66,18 @@ public void multipartRequestWithSingleFile() throws Exception {
69
66
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
70
67
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
71
68
72
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfile" )
69
+ testClient .post ().uri ("/multipartfile" )
73
70
.bodyValue (bodyBuilder .build ())
74
71
.exchange ()
75
72
.expectStatus ().isFound ()
76
73
.expectBody ().isEmpty ();
77
74
78
- // Further assertions on the server response
79
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
80
- .andExpect (model ().attribute ("fileContent" , fileContent ))
81
- .andExpect (model ().attribute ("jsonContent" , json ));
82
-
83
75
// Now try the same with HTTP PUT
84
- exchangeResult = testClient .put ().uri ("/multipartfile-via-put" )
76
+ testClient .put ().uri ("/multipartfile-via-put" )
85
77
.bodyValue (bodyBuilder .build ())
86
78
.exchange ()
87
79
.expectStatus ().isFound ()
88
80
.expectBody ().isEmpty ();
89
-
90
- // Further assertions on the server response
91
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
92
- .andExpect (model ().attribute ("fileContent" , fileContent ))
93
- .andExpect (model ().attribute ("jsonContent" , json ));
94
81
}
95
82
96
83
@ Test
@@ -110,16 +97,11 @@ public void multipartRequestWithFileArray() throws Exception {
110
97
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
111
98
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
112
99
113
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfilearray" )
100
+ testClient .post ().uri ("/multipartfilearray" )
114
101
.bodyValue (bodyBuilder .build ())
115
102
.exchange ()
116
103
.expectStatus ().isFound ()
117
104
.expectBody ().isEmpty ();
118
-
119
- // Further assertions on the server response
120
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
121
- .andExpect (model ().attribute ("fileContent" , fileContent ))
122
- .andExpect (model ().attribute ("jsonContent" , json ));
123
105
}
124
106
125
107
@ Test
@@ -138,16 +120,11 @@ public void multipartRequestWithOptionalFile() throws Exception {
138
120
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
139
121
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
140
122
141
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfile" )
123
+ testClient .post ().uri ("/optionalfile" )
142
124
.bodyValue (bodyBuilder .build ())
143
125
.exchange ()
144
126
.expectStatus ().isFound ()
145
127
.expectBody ().isEmpty ();
146
-
147
- // Further assertions on the server response
148
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
149
- .andExpect (model ().attribute ("fileContent" , fileContent ))
150
- .andExpect (model ().attribute ("jsonContent" , json ));
151
128
}
152
129
153
130
@ Test
@@ -157,16 +134,11 @@ public void multipartRequestWithOptionalFileNotPresent() throws Exception {
157
134
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
158
135
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
159
136
160
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfile" )
137
+ testClient .post ().uri ("/optionalfile" )
161
138
.bodyValue (bodyBuilder .build ())
162
139
.exchange ()
163
140
.expectStatus ().isFound ()
164
141
.expectBody ().isEmpty ();
165
-
166
- // Further assertions on the server response
167
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
168
- .andExpect (model ().attributeDoesNotExist ("fileContent" ))
169
- .andExpect (model ().attribute ("jsonContent" , json ));
170
142
}
171
143
172
144
@ Test
@@ -179,16 +151,11 @@ public void multipartRequestWithOptionalFileArray() throws Exception {
179
151
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
180
152
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
181
153
182
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilearray" )
154
+ testClient .post ().uri ("/optionalfilearray" )
183
155
.bodyValue (bodyBuilder .build ())
184
156
.exchange ()
185
157
.expectStatus ().isFound ()
186
158
.expectBody ().isEmpty ();
187
-
188
- // Further assertions on the server response
189
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
190
- .andExpect (model ().attribute ("fileContent" , fileContent ))
191
- .andExpect (model ().attribute ("jsonContent" , json ));
192
159
}
193
160
194
161
@ Test
@@ -198,16 +165,11 @@ public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
198
165
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
199
166
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
200
167
201
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilearray" )
168
+ testClient .post ().uri ("/optionalfilearray" )
202
169
.bodyValue (bodyBuilder .build ())
203
170
.exchange ()
204
171
.expectStatus ().isFound ()
205
172
.expectBody ().isEmpty ();
206
-
207
- // Further assertions on the server response
208
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
209
- .andExpect (model ().attributeDoesNotExist ("fileContent" ))
210
- .andExpect (model ().attribute ("jsonContent" , json ));
211
173
}
212
174
213
175
@ Test
@@ -220,16 +182,11 @@ public void multipartRequestWithOptionalFileList() throws Exception {
220
182
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
221
183
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
222
184
223
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilelist" )
185
+ testClient .post ().uri ("/optionalfilelist" )
224
186
.bodyValue (bodyBuilder .build ())
225
187
.exchange ()
226
188
.expectStatus ().isFound ()
227
189
.expectBody ().isEmpty ();
228
-
229
- // Further assertions on the server response
230
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
231
- .andExpect (model ().attribute ("fileContent" , fileContent ))
232
- .andExpect (model ().attribute ("jsonContent" , json ));
233
190
}
234
191
235
192
@ Test
@@ -239,16 +196,11 @@ public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
239
196
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
240
197
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
241
198
242
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilelist" )
199
+ testClient .post ().uri ("/optionalfilelist" )
243
200
.bodyValue (bodyBuilder .build ())
244
201
.exchange ()
245
202
.expectStatus ().isFound ()
246
203
.expectBody ().isEmpty ();
247
-
248
- // Further assertions on the server response
249
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
250
- .andExpect (model ().attributeDoesNotExist ("fileContent" ))
251
- .andExpect (model ().attribute ("jsonContent" , json ));
252
204
}
253
205
254
206
@ Test
@@ -260,16 +212,11 @@ public void multipartRequestWithServletParts() throws Exception {
260
212
bodyBuilder .part ("file" , fileContent ).filename ("orig" );
261
213
bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
262
214
263
- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfile" )
215
+ testClient .post ().uri ("/multipartfile" )
264
216
.bodyValue (bodyBuilder .build ())
265
217
.exchange ()
266
218
.expectStatus ().isFound ()
267
219
.expectBody ().isEmpty ();
268
-
269
- // Further assertions on the server response
270
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
271
- .andExpect (model ().attribute ("fileContent" , fileContent ))
272
- .andExpect (model ().attribute ("jsonContent" , json ));
273
220
}
274
221
275
222
@ Test
@@ -283,15 +230,11 @@ public void multipartRequestWrapped() throws Exception {
283
230
.filter (new RequestWrappingFilter ())
284
231
.build ();
285
232
286
- EntityExchangeResult < Void > exchangeResult = client .post ().uri ("/multipartfile" )
233
+ client .post ().uri ("/multipartfile" )
287
234
.bodyValue (bodyBuilder .build ())
288
235
.exchange ()
289
236
.expectStatus ().isFound ()
290
237
.expectBody ().isEmpty ();
291
-
292
- // Further assertions on the server response
293
- MockMvcWebTestClient .resultActionsFor (exchangeResult )
294
- .andExpect (model ().attribute ("jsonContent" , json ));
295
238
}
296
239
297
240
@@ -301,110 +244,78 @@ private static class MultipartController {
301
244
302
245
@ PostMapping ("/multipartfile" )
303
246
public String processMultipartFile (@ RequestParam (required = false ) MultipartFile file ,
304
- @ RequestPart (required = false ) Map <String , String > json , Model model ) throws IOException {
305
-
306
- if (file != null ) {
307
- model .addAttribute ("fileContent" , file .getBytes ());
308
- }
309
- if (json != null ) {
310
- model .addAttribute ("jsonContent" , json );
311
- }
247
+ @ RequestPart (required = false ) Map <String , String > json ) {
312
248
313
249
return "redirect:/index" ;
314
250
}
315
251
316
252
@ PutMapping ("/multipartfile-via-put" )
317
253
public String processMultipartFileViaHttpPut (@ RequestParam (required = false ) MultipartFile file ,
318
- @ RequestPart (required = false ) Map <String , String > json , Model model ) throws IOException {
254
+ @ RequestPart (required = false ) Map <String , String > json ) {
319
255
320
- return processMultipartFile (file , json , model );
256
+ return processMultipartFile (file , json );
321
257
}
322
258
323
259
@ PostMapping ("/multipartfilearray" )
324
260
public String processMultipartFileArray (@ RequestParam (required = false ) MultipartFile [] file ,
325
- @ RequestPart (required = false ) Map <String , String > json , Model model ) throws IOException {
261
+ @ RequestPart (required = false ) Map <String , String > json ) throws IOException {
326
262
327
263
if (file != null && file .length > 0 ) {
328
264
byte [] content = file [0 ].getBytes ();
329
265
assertThat (file [1 ].getBytes ()).isEqualTo (content );
330
- model .addAttribute ("fileContent" , content );
331
- }
332
- if (json != null ) {
333
- model .addAttribute ("jsonContent" , json );
334
266
}
335
-
336
267
return "redirect:/index" ;
337
268
}
338
269
339
270
@ PostMapping ("/multipartfilelist" )
340
271
public String processMultipartFileList (@ RequestParam (required = false ) List <MultipartFile > file ,
341
- @ RequestPart (required = false ) Map <String , String > json , Model model ) throws IOException {
272
+ @ RequestPart (required = false ) Map <String , String > json ) throws IOException {
342
273
343
274
if (file != null && !file .isEmpty ()) {
344
275
byte [] content = file .get (0 ).getBytes ();
345
276
assertThat (file .get (1 ).getBytes ()).isEqualTo (content );
346
- model .addAttribute ("fileContent" , content );
347
- }
348
- if (json != null ) {
349
- model .addAttribute ("jsonContent" , json );
350
277
}
351
-
352
278
return "redirect:/index" ;
353
279
}
354
280
355
281
@ PostMapping ("/optionalfile" )
356
- public String processOptionalFile (@ RequestParam Optional <MultipartFile > file ,
357
- @ RequestPart Map <String , String > json , Model model ) throws IOException {
358
-
359
- if (file .isPresent ()) {
360
- model .addAttribute ("fileContent" , file .get ().getBytes ());
361
- }
362
- model .addAttribute ("jsonContent" , json );
282
+ public String processOptionalFile (
283
+ @ RequestParam Optional <MultipartFile > file , @ RequestPart Map <String , String > json ) {
363
284
364
285
return "redirect:/index" ;
365
286
}
366
287
367
288
@ PostMapping ("/optionalfilearray" )
368
- public String processOptionalFileArray (@ RequestParam Optional <MultipartFile []> file ,
369
- @ RequestPart Map <String , String > json , Model model ) throws IOException {
289
+ public String processOptionalFileArray (
290
+ @ RequestParam Optional <MultipartFile []> file , @ RequestPart Map <String , String > json )
291
+ throws IOException {
370
292
371
293
if (file .isPresent ()) {
372
294
byte [] content = file .get ()[0 ].getBytes ();
373
295
assertThat (file .get ()[1 ].getBytes ()).isEqualTo (content );
374
- model .addAttribute ("fileContent" , content );
375
296
}
376
- model .addAttribute ("jsonContent" , json );
377
-
378
297
return "redirect:/index" ;
379
298
}
380
299
381
300
@ PostMapping ("/optionalfilelist" )
382
- public String processOptionalFileList (@ RequestParam Optional <List <MultipartFile >> file ,
383
- @ RequestPart Map <String , String > json , Model model ) throws IOException {
301
+ public String processOptionalFileList (
302
+ @ RequestParam Optional <List <MultipartFile >> file , @ RequestPart Map <String , String > json )
303
+ throws IOException {
384
304
385
305
if (file .isPresent ()) {
386
306
byte [] content = file .get ().get (0 ).getBytes ();
387
307
assertThat (file .get ().get (1 ).getBytes ()).isEqualTo (content );
388
- model .addAttribute ("fileContent" , content );
389
308
}
390
- model .addAttribute ("jsonContent" , json );
391
-
392
309
return "redirect:/index" ;
393
310
}
394
311
395
312
@ PostMapping ("/part" )
396
- public String processPart (@ RequestParam Part part ,
397
- @ RequestPart Map <String , String > json , Model model ) throws IOException {
398
-
399
- model .addAttribute ("fileContent" , part .getInputStream ());
400
- model .addAttribute ("jsonContent" , json );
401
-
313
+ public String processPart (@ RequestParam Part part , @ RequestPart Map <String , String > json ) {
402
314
return "redirect:/index" ;
403
315
}
404
316
405
317
@ PostMapping ("/json" )
406
- public String processMultipart (@ RequestPart Map <String , String > json , Model model ) {
407
- model .addAttribute ("json" , json );
318
+ public String processMultipart (@ RequestPart Map <String , String > json ) {
408
319
return "redirect:/index" ;
409
320
}
410
321
}
0 commit comments