22
22
import java .util .Collections ;
23
23
import java .util .List ;
24
24
import java .util .Map ;
25
+ import java .util .Optional ;
25
26
import javax .servlet .MultipartConfigElement ;
26
27
27
28
import org .eclipse .jetty .server .Connector ;
@@ -88,7 +89,6 @@ public class RequestPartIntegrationTests {
88
89
89
90
@ BeforeClass
90
91
public static void startServer () throws Exception {
91
-
92
92
// Let server pick its own random, available port.
93
93
server = new Server (0 );
94
94
@@ -144,19 +144,18 @@ public static void stopServer() throws Exception {
144
144
145
145
@ Test
146
146
public void commonsMultipartResolver () throws Exception {
147
- testCreate (baseUrl + "/commons-resolver/test" );
147
+ testCreate (baseUrl + "/commons-resolver/test" , "Jason" );
148
+ testCreate (baseUrl + "/commons-resolver/test" , "Arjen" );
148
149
}
149
150
150
151
@ Test
151
152
public void standardMultipartResolver () throws Exception {
152
- testCreate (baseUrl + "/standard-resolver/test" );
153
+ testCreate (baseUrl + "/standard-resolver/test" , "Jason" );
154
+ testCreate (baseUrl + "/standard-resolver/test" , "Arjen" );
153
155
}
154
156
155
- // SPR-13319
156
-
157
- @ Test
157
+ @ Test // SPR-13319
158
158
public void standardMultipartResolverWithEncodedFileName () throws Exception {
159
-
160
159
byte [] boundary = MimeTypeUtils .generateMultipartBoundary ();
161
160
String boundaryText = new String (boundary , "US-ASCII" );
162
161
Map <String , String > params = Collections .singletonMap ("boundary" , boundaryText );
@@ -183,18 +182,18 @@ public void standardMultipartResolverWithEncodedFileName() throws Exception {
183
182
assertEquals (HttpStatus .OK , responseEntity .getStatusCode ());
184
183
}
185
184
186
- private void testCreate (String url ) {
185
+ private void testCreate (String url , String basename ) {
187
186
MultiValueMap <String , Object > parts = new LinkedMultiValueMap <String , Object >();
188
- parts .add ("json-data" , new HttpEntity <TestData >(new TestData ("Jason" )));
189
- parts .add ("file-data" , new ClassPathResource ("logo.jpg" , this . getClass ()));
187
+ parts .add ("json-data" , new HttpEntity <TestData >(new TestData (basename )));
188
+ parts .add ("file-data" , new ClassPathResource ("logo.jpg" , getClass ()));
190
189
parts .add ("empty-data" , new HttpEntity <byte []>(new byte [0 ])); // SPR-12860
191
190
192
191
HttpHeaders headers = new HttpHeaders ();
193
192
headers .setContentType (new MediaType ("application" , "octet-stream" , Charset .forName ("ISO-8859-1" )));
194
193
parts .add ("iso-8859-1-data" , new HttpEntity <byte []>(new byte [] {(byte ) 0xC4 }, headers )); // SPR-13096
195
194
196
195
URI location = restTemplate .postForLocation (url , parts );
197
- assertEquals ("http://localhost:8080/test/Jason /logo.jpg" , location .toString ());
196
+ assertEquals ("http://localhost:8080/test/" + basename + " /logo.jpg" , location .toString ());
198
197
}
199
198
200
199
@@ -208,6 +207,7 @@ public RequestPartTestController controller() {
208
207
}
209
208
}
210
209
210
+
211
211
@ Configuration
212
212
@ SuppressWarnings ("unused" )
213
213
static class CommonsMultipartResolverTestConfig extends RequestPartTestConfig {
@@ -218,6 +218,7 @@ public MultipartResolver multipartResolver() {
218
218
}
219
219
}
220
220
221
+
221
222
@ Configuration
222
223
@ SuppressWarnings ("unused" )
223
224
static class StandardMultipartResolverTestConfig extends RequestPartTestConfig {
@@ -228,19 +229,20 @@ public MultipartResolver multipartResolver() {
228
229
}
229
230
}
230
231
232
+
231
233
@ Controller
232
234
@ SuppressWarnings ("unused" )
233
235
private static class RequestPartTestController {
234
236
235
- @ RequestMapping (value = "/test" , method = POST , consumes = { "multipart/mixed" , "multipart/form-data" })
237
+ @ RequestMapping (value = "/test" , method = POST , consumes = {"multipart/mixed" , "multipart/form-data" })
236
238
public ResponseEntity <Object > create (@ RequestPart (name = "json-data" ) TestData testData ,
237
- @ RequestPart ("file-data" ) MultipartFile file ,
239
+ @ RequestPart ("file-data" ) Optional < MultipartFile > file ,
238
240
@ RequestPart (name = "empty-data" , required = false ) TestData emptyData ,
239
241
@ RequestPart (name = "iso-8859-1-data" ) byte [] iso88591Data ) {
240
242
241
243
Assert .assertArrayEquals (new byte []{(byte ) 0xC4 }, iso88591Data );
242
244
243
- String url = "http://localhost:8080/test/" + testData .getName () + "/" + file .getOriginalFilename ();
245
+ String url = "http://localhost:8080/test/" + testData .getName () + "/" + file .get (). getOriginalFilename ();
244
246
HttpHeaders headers = new HttpHeaders ();
245
247
headers .setLocation (URI .create (url ));
246
248
return new ResponseEntity <Object >(headers , HttpStatus .CREATED );
@@ -253,6 +255,7 @@ public ResponseEntity<Void> create(@RequestPart("file") MultipartFile multipartF
253
255
}
254
256
}
255
257
258
+
256
259
@ SuppressWarnings ("unused" )
257
260
private static class TestData {
258
261
0 commit comments