@@ -439,4 +439,52 @@ describe('Request', () => {
439
439
expect ( file . lastModified ) . to . be . a ( 'number' ) ;
440
440
} ) ;
441
441
} ) ;
442
+
443
+ it ( 'should decode empty file inputs into File instances (web FormData)' , async ( ) => {
444
+ const ogFormData = new WebFormData ( ) ;
445
+ ogFormData . append ( 'a' , 1 ) ;
446
+ // This is what happens when you construct the form data set with an empty file input:
447
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
448
+ ogFormData . append ( 'file' , new File ( [ ] , '' , { type : 'application/octet-stream' } ) ) ;
449
+ const request = new Request ( base , {
450
+ method : 'POST' ,
451
+ body : ogFormData ,
452
+ } ) ;
453
+ const clonedRequest = request . clone ( ) ;
454
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
455
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
456
+ const file = clonedFormData . get ( 'file' ) ;
457
+ expect ( file . name ) . to . equal ( "" ) ;
458
+ expect ( file . type ) . to . equal ( "application/octet-stream" ) ;
459
+ expect ( file . size ) . to . equal ( 0 ) ;
460
+ } ) ;
461
+ } ) ;
462
+
463
+ it . skip ( 'should decode empty file inputs into File instances (node FormData)' , async ( ) => {
464
+ const ogFormData = new FormData ( ) ;
465
+ ogFormData . append ( 'a' , 1 ) ;
466
+ // This is what happens when you construct the form data set with an empty file input:
467
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
468
+ ogFormData . append ( 'file' , Buffer . from ( '' ) , {
469
+ // Note: This doesn't work at the moment due to https://github.com/form-data/form-data/issues/412.
470
+ // There is a v4 released which has a fix that might handle this but I
471
+ // wasn't positive if it had breaking changes that would impact us so we
472
+ // can handle an upgrade separately.
473
+ filename : '' ,
474
+ contentType : 'application/octet-stream' ,
475
+ } ) ;
476
+ const request = new Request ( base , {
477
+ method : 'POST' ,
478
+ body : ogFormData ,
479
+ } ) ;
480
+ const clonedRequest = request . clone ( ) ;
481
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
482
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
483
+ const file = clonedFormData . get ( 'file' ) ;
484
+ expect ( file . name ) . to . equal ( "" ) ;
485
+ expect ( file . type ) . to . equal ( "application/octet-stream" ) ;
486
+ expect ( file . size ) . to . equal ( 0 ) ;
487
+ } ) ;
488
+
489
+ } ) ;
442
490
} ) ;
0 commit comments