@@ -235,7 +235,7 @@ public void testInvalidContentTypeMultipartRequest() throws Exception {
235
235
mai .setInvocationContext (ActionContext .getContext ());
236
236
237
237
ActionContext .getContext ().setParameters (HttpParameters .create ().build ());
238
- ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 ));
238
+ ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 , - 1 ));
239
239
240
240
interceptor .intercept (mai );
241
241
@@ -257,7 +257,7 @@ public void testNoContentMultipartRequest() throws Exception {
257
257
mai .setInvocationContext (ActionContext .getContext ());
258
258
259
259
ActionContext .getContext ().setParameters (HttpParameters .create ().build ());
260
- ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 ));
260
+ ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 , - 1 ));
261
261
262
262
interceptor .intercept (mai );
263
263
@@ -288,7 +288,7 @@ public void testSuccessUploadOfATextFileMultipartRequest() throws Exception {
288
288
mai .setInvocationContext (ActionContext .getContext ());
289
289
Map <String , Object > param = new HashMap <>();
290
290
ActionContext .getContext ().setParameters (HttpParameters .create (param ).build ());
291
- ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 ));
291
+ ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 , - 1 ));
292
292
293
293
interceptor .intercept (mai );
294
294
@@ -349,7 +349,7 @@ public void testMultipleAccept() throws Exception {
349
349
mai .setInvocationContext (ActionContext .getContext ());
350
350
Map <String , Object > param = new HashMap <String , Object >();
351
351
ActionContext .getContext ().setParameters (HttpParameters .create (param ).build ());
352
- ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 ));
352
+ ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 , - 1 ));
353
353
354
354
interceptor .setAllowedTypes ("text/html" );
355
355
interceptor .intercept (mai );
@@ -402,7 +402,7 @@ public void testUnacceptedNumberOfFiles() throws Exception {
402
402
mai .setInvocationContext (ActionContext .getContext ());
403
403
Map <String , Object > param = new HashMap <>();
404
404
ActionContext .getContext ().setParameters (HttpParameters .create (param ).build ());
405
- ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 ));
405
+ ActionContext .getContext ().put (ServletActionContext .HTTP_REQUEST , createMultipartRequest (req , 2000 , - 1 ));
406
406
407
407
interceptor .setAllowedTypes ("text/html" );
408
408
interceptor .intercept (mai );
@@ -413,6 +413,55 @@ public void testUnacceptedNumberOfFiles() throws Exception {
413
413
assertEquals ("Request exceeded allowed number of files! Max allowed files number is: 3!" , action .getActionErrors ().iterator ().next ());
414
414
}
415
415
416
+ public void testMultipartRequestMaxStringLength () throws Exception {
417
+ MockHttpServletRequest req = new MockHttpServletRequest ();
418
+ req .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
419
+ req .setMethod ("post" );
420
+ req .addHeader ("Content-type" , "multipart/form-data; boundary=---1234" );
421
+
422
+ // inspired by the unit tests for jakarta commons fileupload
423
+ String content = ("-----1234\r \n " +
424
+ "Content-Disposition: form-data; name=\" file\" ; filename=\" deleteme.txt\" \r \n " +
425
+ "Content-Type: text/html\r \n " +
426
+ "\r \n " +
427
+ "Unit test of FileUploadInterceptor" +
428
+ "\r \n " +
429
+ "-----1234\r \n " +
430
+ "Content-Disposition: form-data; name=\" normalFormField1\" \r \n " +
431
+ "\r \n " +
432
+ "it works" +
433
+ "\r \n " +
434
+ "-----1234\r \n " +
435
+ "Content-Disposition: form-data; name=\" normalFormField2\" \r \n " +
436
+ "\r \n " +
437
+ "long string should not work" +
438
+ "\r \n " +
439
+ "-----1234--\r \n " );
440
+ req .setContent (content .getBytes (StandardCharsets .US_ASCII ));
441
+
442
+ MyFileupAction action = container .inject (MyFileupAction .class );
443
+
444
+ MockActionInvocation mai = new MockActionInvocation ();
445
+ mai .setAction (action );
446
+ mai .setResultCode ("success" );
447
+ mai .setInvocationContext (ActionContext .getContext ());
448
+ Map <String , Object > param = new HashMap <>();
449
+ ActionContext .getContext ()
450
+ .withParameters (HttpParameters .create (param ).build ())
451
+ .withServletRequest (createMultipartRequest (req , -1 , 20 ));
452
+
453
+ interceptor .intercept (mai );
454
+
455
+ assertTrue (action .hasActionErrors ());
456
+
457
+ Collection <String > errors = action .getActionErrors ();
458
+ assertEquals (1 , errors .size ());
459
+ String msg = errors .iterator ().next ();
460
+ assertEquals (
461
+ "The request parameter \" normalFormField2\" was too long. Max length allowed is 20, but found 27!" ,
462
+ msg );
463
+ }
464
+
416
465
public void testMultipartRequestLocalizedError () throws Exception {
417
466
MockHttpServletRequest req = new MockHttpServletRequest ();
418
467
req .setCharacterEncoding (StandardCharsets .UTF_8 .name ());
@@ -439,7 +488,7 @@ public void testMultipartRequestLocalizedError() throws Exception {
439
488
ActionContext .getContext ()
440
489
.withParameters (HttpParameters .create (param ).build ())
441
490
.withLocale (Locale .GERMAN )
442
- .withServletRequest (createMultipartRequest (req , 10 ));
491
+ .withServletRequest (createMultipartRequest (req , 10 , - 1 ));
443
492
444
493
interceptor .intercept (mai );
445
494
@@ -472,10 +521,11 @@ private String encodeTextFile(String bondary, String endline, String name, Strin
472
521
return sb .toString ();
473
522
}
474
523
475
- private MultiPartRequestWrapper createMultipartRequest (HttpServletRequest req , int maxsize ) throws IOException {
524
+ private MultiPartRequestWrapper createMultipartRequest (HttpServletRequest req , int maxsize , int maxStringLength ) throws IOException {
476
525
JakartaMultiPartRequest jak = new JakartaMultiPartRequest ();
477
526
jak .setMaxSize (String .valueOf (maxsize ));
478
527
jak .setMaxFiles ("3" );
528
+ jak .setMaxStringLength (String .valueOf (maxStringLength ));
479
529
return new MultiPartRequestWrapper (jak , req , tempDir .getAbsolutePath (), new DefaultLocaleProvider ());
480
530
}
481
531
0 commit comments