1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2015 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.
17
17
package org .springframework .test .web .servlet .samples .standalone ;
18
18
19
19
import 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 ;
20
29
21
30
import org .junit .Test ;
22
31
23
32
import org .springframework .mock .web .MockMultipartFile ;
24
33
import org .springframework .stereotype .Controller ;
34
+ import org .springframework .test .web .servlet .MockMvc ;
25
35
import org .springframework .ui .Model ;
26
36
import org .springframework .web .bind .annotation .RequestMapping ;
27
37
import org .springframework .web .bind .annotation .RequestMethod ;
28
38
import org .springframework .web .bind .annotation .RequestParam ;
39
+ import org .springframework .web .bind .annotation .RequestPart ;
40
+ import org .springframework .web .filter .OncePerRequestFilter ;
29
41
import org .springframework .web .multipart .MultipartFile ;
30
42
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 ;
34
46
35
47
/**
36
48
* @author Rossen Stoyanchev
37
49
*/
38
50
public class FileUploadControllerTests {
39
51
52
+ private static final Charset CHARSET = Charset .forName ("UTF-8" );
53
+
54
+
40
55
@ 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" )));
46
68
}
47
69
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
+ }
48
84
85
+
86
+ @ SuppressWarnings ("unused" )
49
87
@ 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
+ }
51
99
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 );
55
103
return "redirect:/index" ;
56
104
}
105
+ }
106
+
107
+ private static class RequestWrappingFilter extends OncePerRequestFilter {
57
108
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
+ }
58
116
}
59
117
60
- }
118
+ }
0 commit comments