11/*
2- * Copyright 2002-2013 the original author or authors.
2+ * Copyright 2002-2015 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.
1717package org .springframework .test .web .servlet .samples .standalone ;
1818
1919import java .io .IOException ;
20+ import java .nio .charset .Charset ;
21+ import java .util .Collections ;
22+ import java .util .Map ;
23+ import javax .servlet .Filter ;
24+ import javax .servlet .FilterChain ;
25+ import javax .servlet .ServletException ;
26+ import javax .servlet .http .HttpServletRequest ;
27+ import javax .servlet .http .HttpServletRequestWrapper ;
28+ import javax .servlet .http .HttpServletResponse ;
2029
2130import org .junit .Test ;
2231
2332import org .springframework .mock .web .MockMultipartFile ;
2433import org .springframework .stereotype .Controller ;
34+ import org .springframework .test .web .servlet .MockMvc ;
2535import org .springframework .ui .Model ;
2636import org .springframework .web .bind .annotation .RequestMapping ;
2737import org .springframework .web .bind .annotation .RequestMethod ;
2838import org .springframework .web .bind .annotation .RequestParam ;
39+ import org .springframework .web .bind .annotation .RequestPart ;
40+ import org .springframework .web .filter .OncePerRequestFilter ;
2941import org .springframework .web .multipart .MultipartFile ;
3042
31- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .* ;
32- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .* ;
33- import static org .springframework .test .web .servlet .setup .MockMvcBuilders .* ;
43+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .fileUpload ;
44+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .model ;
45+ import static org .springframework .test .web .servlet .setup .MockMvcBuilders .standaloneSetup ;
3446
3547/**
3648 * @author Rossen Stoyanchev
3749 */
3850public class FileUploadControllerTests {
3951
52+ private static final Charset CHARSET = Charset .forName ("UTF-8" );
53+
54+
4055 @ Test
41- public void readString () throws Exception {
42- MockMultipartFile file = new MockMultipartFile ("file" , "orig" , null , "bar" .getBytes ());
43- standaloneSetup (new FileUploadController ()).build ()
44- .perform (fileUpload ("/fileupload" ).file (file ))
45- .andExpect (model ().attribute ("message" , "File 'orig' uploaded successfully" ));
56+ public void multipartRequest () throws Exception {
57+
58+ byte [] fileContent = "bar" .getBytes (CHARSET );
59+ MockMultipartFile filePart = new MockMultipartFile ("file" , "orig" , null , fileContent );
60+
61+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (CHARSET );
62+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
63+
64+ MockMvc mockMvc = standaloneSetup (new MultipartController ()).build ();
65+ mockMvc .perform (fileUpload ("/test" ).file (filePart ).file (jsonPart ))
66+ .andExpect (model ().attribute ("fileContent" , fileContent ))
67+ .andExpect (model ().attribute ("jsonContent" , Collections .singletonMap ("name" , "yeeeah" )));
4668 }
4769
70+ // SPR-13317
71+
72+ @ Test
73+ public void multipartRequestWrapped () throws Exception {
74+
75+ byte [] json = "{\" name\" :\" yeeeah\" }" .getBytes (CHARSET );
76+ MockMultipartFile jsonPart = new MockMultipartFile ("json" , "json" , "application/json" , json );
77+
78+ Filter filter = new RequestWrappingFilter ();
79+ MockMvc mockMvc = standaloneSetup (new MultipartController ()).addFilter (filter ).build ();
80+
81+ Map <String , String > jsonMap = Collections .singletonMap ("name" , "yeeeah" );
82+ mockMvc .perform (fileUpload ("/testJson" ).file (jsonPart )).andExpect (model ().attribute ("json" , jsonMap ));
83+ }
4884
85+
86+ @ SuppressWarnings ("unused" )
4987 @ Controller
50- private static class FileUploadController {
88+ private static class MultipartController {
89+
90+ @ RequestMapping (value = "/test" , method = RequestMethod .POST )
91+ public String processMultipart (@ RequestParam MultipartFile file ,
92+ @ RequestPart Map <String , String > json , Model model ) throws IOException {
93+
94+ model .addAttribute ("jsonContent" , json );
95+ model .addAttribute ("fileContent" , file .getBytes ());
96+
97+ return "redirect:/index" ;
98+ }
5199
52- @ RequestMapping (value = "/fileupload " , method = RequestMethod .POST )
53- public String processUpload ( @ RequestParam MultipartFile file , Model model ) throws IOException {
54- model .addAttribute ("message " , "File '" + file . getOriginalFilename () + "' uploaded successfully" );
100+ @ RequestMapping (value = "/testJson " , method = RequestMethod .POST )
101+ public String processMultipart ( @ RequestPart Map < String , String > json , Model model ) {
102+ model .addAttribute ("json " , json );
55103 return "redirect:/index" ;
56104 }
105+ }
106+
107+ private static class RequestWrappingFilter extends OncePerRequestFilter {
57108
109+ @ Override
110+ protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response ,
111+ FilterChain filterChain ) throws IOException , ServletException {
112+
113+ request = new HttpServletRequestWrapper (request );
114+ filterChain .doFilter (request , response );
115+ }
58116 }
59117
60- }
118+ }
0 commit comments