3434import org .springframework .http .MediaType ;
3535import org .springframework .http .client .MultipartBodyBuilder ;
3636import org .springframework .stereotype .Controller ;
37- import org .springframework .test .web .reactive .server .EntityExchangeResult ;
3837import org .springframework .test .web .reactive .server .WebTestClient ;
3938import org .springframework .test .web .servlet .client .MockMvcWebTestClient ;
40- import org .springframework .ui .Model ;
4139import org .springframework .web .bind .annotation .PostMapping ;
4240import org .springframework .web .bind .annotation .PutMapping ;
4341import org .springframework .web .bind .annotation .RequestParam ;
4644import org .springframework .web .multipart .MultipartFile ;
4745
4846import static org .assertj .core .api .Assertions .assertThat ;
49- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .model ;
5047
5148/**
5249 * {@link MockMvcWebTestClient} equivalent of the MockMvc
@@ -69,28 +66,18 @@ public void multipartRequestWithSingleFile() throws Exception {
6966 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
7067 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
7168
72- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfile" )
69+ testClient .post ().uri ("/multipartfile" )
7370 .bodyValue (bodyBuilder .build ())
7471 .exchange ()
7572 .expectStatus ().isFound ()
7673 .expectBody ().isEmpty ();
7774
78- // Further assertions on the server response
79- MockMvcWebTestClient .resultActionsFor (exchangeResult )
80- .andExpect (model ().attribute ("fileContent" , fileContent ))
81- .andExpect (model ().attribute ("jsonContent" , json ));
82-
8375 // Now try the same with HTTP PUT
84- exchangeResult = testClient .put ().uri ("/multipartfile-via-put" )
76+ testClient .put ().uri ("/multipartfile-via-put" )
8577 .bodyValue (bodyBuilder .build ())
8678 .exchange ()
8779 .expectStatus ().isFound ()
8880 .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 ));
9481 }
9582
9683 @ Test
@@ -110,16 +97,11 @@ public void multipartRequestWithFileArray() throws Exception {
11097 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
11198 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
11299
113- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfilearray" )
100+ testClient .post ().uri ("/multipartfilearray" )
114101 .bodyValue (bodyBuilder .build ())
115102 .exchange ()
116103 .expectStatus ().isFound ()
117104 .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 ));
123105 }
124106
125107 @ Test
@@ -138,16 +120,11 @@ public void multipartRequestWithOptionalFile() throws Exception {
138120 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
139121 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
140122
141- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfile" )
123+ testClient .post ().uri ("/optionalfile" )
142124 .bodyValue (bodyBuilder .build ())
143125 .exchange ()
144126 .expectStatus ().isFound ()
145127 .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 ));
151128 }
152129
153130 @ Test
@@ -157,16 +134,11 @@ public void multipartRequestWithOptionalFileNotPresent() throws Exception {
157134 MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
158135 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
159136
160- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfile" )
137+ testClient .post ().uri ("/optionalfile" )
161138 .bodyValue (bodyBuilder .build ())
162139 .exchange ()
163140 .expectStatus ().isFound ()
164141 .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 ));
170142 }
171143
172144 @ Test
@@ -179,16 +151,11 @@ public void multipartRequestWithOptionalFileArray() throws Exception {
179151 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
180152 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
181153
182- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilearray" )
154+ testClient .post ().uri ("/optionalfilearray" )
183155 .bodyValue (bodyBuilder .build ())
184156 .exchange ()
185157 .expectStatus ().isFound ()
186158 .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 ));
192159 }
193160
194161 @ Test
@@ -198,16 +165,11 @@ public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
198165 MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
199166 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
200167
201- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilearray" )
168+ testClient .post ().uri ("/optionalfilearray" )
202169 .bodyValue (bodyBuilder .build ())
203170 .exchange ()
204171 .expectStatus ().isFound ()
205172 .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 ));
211173 }
212174
213175 @ Test
@@ -220,16 +182,11 @@ public void multipartRequestWithOptionalFileList() throws Exception {
220182 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
221183 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
222184
223- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilelist" )
185+ testClient .post ().uri ("/optionalfilelist" )
224186 .bodyValue (bodyBuilder .build ())
225187 .exchange ()
226188 .expectStatus ().isFound ()
227189 .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 ));
233190 }
234191
235192 @ Test
@@ -239,16 +196,11 @@ public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
239196 MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
240197 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
241198
242- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/optionalfilelist" )
199+ testClient .post ().uri ("/optionalfilelist" )
243200 .bodyValue (bodyBuilder .build ())
244201 .exchange ()
245202 .expectStatus ().isFound ()
246203 .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 ));
252204 }
253205
254206 @ Test
@@ -260,16 +212,11 @@ public void multipartRequestWithServletParts() throws Exception {
260212 bodyBuilder .part ("file" , fileContent ).filename ("orig" );
261213 bodyBuilder .part ("json" , json , MediaType .APPLICATION_JSON );
262214
263- EntityExchangeResult < Void > exchangeResult = testClient .post ().uri ("/multipartfile" )
215+ testClient .post ().uri ("/multipartfile" )
264216 .bodyValue (bodyBuilder .build ())
265217 .exchange ()
266218 .expectStatus ().isFound ()
267219 .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 ));
273220 }
274221
275222 @ Test
@@ -283,15 +230,11 @@ public void multipartRequestWrapped() throws Exception {
283230 .filter (new RequestWrappingFilter ())
284231 .build ();
285232
286- EntityExchangeResult < Void > exchangeResult = client .post ().uri ("/multipartfile" )
233+ client .post ().uri ("/multipartfile" )
287234 .bodyValue (bodyBuilder .build ())
288235 .exchange ()
289236 .expectStatus ().isFound ()
290237 .expectBody ().isEmpty ();
291-
292- // Further assertions on the server response
293- MockMvcWebTestClient .resultActionsFor (exchangeResult )
294- .andExpect (model ().attribute ("jsonContent" , json ));
295238 }
296239
297240
@@ -301,110 +244,78 @@ private static class MultipartController {
301244
302245 @ PostMapping ("/multipartfile" )
303246 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 ) {
312248
313249 return "redirect:/index" ;
314250 }
315251
316252 @ PutMapping ("/multipartfile-via-put" )
317253 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 ) {
319255
320- return processMultipartFile (file , json , model );
256+ return processMultipartFile (file , json );
321257 }
322258
323259 @ PostMapping ("/multipartfilearray" )
324260 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 {
326262
327263 if (file != null && file .length > 0 ) {
328264 byte [] content = file [0 ].getBytes ();
329265 assertThat (file [1 ].getBytes ()).isEqualTo (content );
330- model .addAttribute ("fileContent" , content );
331- }
332- if (json != null ) {
333- model .addAttribute ("jsonContent" , json );
334266 }
335-
336267 return "redirect:/index" ;
337268 }
338269
339270 @ PostMapping ("/multipartfilelist" )
340271 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 {
342273
343274 if (file != null && !file .isEmpty ()) {
344275 byte [] content = file .get (0 ).getBytes ();
345276 assertThat (file .get (1 ).getBytes ()).isEqualTo (content );
346- model .addAttribute ("fileContent" , content );
347- }
348- if (json != null ) {
349- model .addAttribute ("jsonContent" , json );
350277 }
351-
352278 return "redirect:/index" ;
353279 }
354280
355281 @ 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 ) {
363284
364285 return "redirect:/index" ;
365286 }
366287
367288 @ 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 {
370292
371293 if (file .isPresent ()) {
372294 byte [] content = file .get ()[0 ].getBytes ();
373295 assertThat (file .get ()[1 ].getBytes ()).isEqualTo (content );
374- model .addAttribute ("fileContent" , content );
375296 }
376- model .addAttribute ("jsonContent" , json );
377-
378297 return "redirect:/index" ;
379298 }
380299
381300 @ 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 {
384304
385305 if (file .isPresent ()) {
386306 byte [] content = file .get ().get (0 ).getBytes ();
387307 assertThat (file .get ().get (1 ).getBytes ()).isEqualTo (content );
388- model .addAttribute ("fileContent" , content );
389308 }
390- model .addAttribute ("jsonContent" , json );
391-
392309 return "redirect:/index" ;
393310 }
394311
395312 @ 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 ) {
402314 return "redirect:/index" ;
403315 }
404316
405317 @ 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 ) {
408319 return "redirect:/index" ;
409320 }
410321 }
0 commit comments