@@ -384,6 +384,54 @@ describe('Request', () => {
384
384
expect ( result ) . to . equal ( 'a=1' ) ;
385
385
} ) ;
386
386
} ) ;
387
+
388
+ it ( 'should decode empty file inputs into File instances (web FormData)' , async ( ) => {
389
+ const ogFormData = new WebFormData ( ) ;
390
+ ogFormData . append ( 'a' , 1 ) ;
391
+ // This is what happens when you construct the form data set with an empty file input:
392
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
393
+ ogFormData . append ( 'file' , new File ( [ ] , '' , { type : 'application/octet-stream' } ) ) ;
394
+ const request = new Request ( base , {
395
+ method : 'POST' ,
396
+ body : ogFormData ,
397
+ } ) ;
398
+ const clonedRequest = request . clone ( ) ;
399
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
400
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
401
+ const file = clonedFormData . get ( 'file' ) ;
402
+ expect ( file . name ) . to . equal ( "" ) ;
403
+ expect ( file . type ) . to . equal ( "application/octet-stream" ) ;
404
+ expect ( file . size ) . to . equal ( 0 ) ;
405
+ } ) ;
406
+ } ) ;
407
+
408
+ it . skip ( 'should decode empty file inputs into File instances (node FormData)' , async ( ) => {
409
+ const ogFormData = new FormData ( ) ;
410
+ ogFormData . append ( 'a' , 1 ) ;
411
+ // This is what happens when you construct the form data set with an empty file input:
412
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
413
+ ogFormData . append ( 'file' , Buffer . from ( '' ) , {
414
+ // Note: This doesn't work at the moment due to https://github.com/form-data/form-data/issues/412.
415
+ // There is a v4 released which has a fix that might handle this but I
416
+ // wasn't positive if it had breaking changes that would impact us so we
417
+ // can handle an upgrade separately.
418
+ filename : '' ,
419
+ contentType : 'application/octet-stream' ,
420
+ } ) ;
421
+ const request = new Request ( base , {
422
+ method : 'POST' ,
423
+ body : ogFormData ,
424
+ } ) ;
425
+ const clonedRequest = request . clone ( ) ;
426
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
427
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
428
+ const file = clonedFormData . get ( 'file' ) ;
429
+ expect ( file . name ) . to . equal ( "" ) ;
430
+ expect ( file . type ) . to . equal ( "application/octet-stream" ) ;
431
+ expect ( file . size ) . to . equal ( 0 ) ;
432
+ } ) ;
433
+
434
+ } ) ;
387
435
388
436
it ( 'should read formData after clone with web FormData body' , async ( ) => {
389
437
const ogFormData = new WebFormData ( ) ;
0 commit comments