@@ -4,11 +4,10 @@ import {TextEncoder} from 'util';
4
4
import AbortController from 'abort-controller' ;
5
5
import chai from 'chai' ;
6
6
import FormData from 'form-data' ;
7
- import { Blob } from '@web-std/fetch' ;
8
- import { ReadableStream } from '@web-std/fetch ' ;
7
+ import { Blob , ReadableStream , Request , FormData as WebFormData } from '@web-std/fetch' ;
8
+ import { File } from '@web-std/file ' ;
9
9
10
10
import TestServer from './utils/server.js' ;
11
- import { Request } from '@web-std/fetch' ;
12
11
13
12
const { expect} = chai ;
14
13
@@ -19,6 +18,7 @@ describe('Request', () => {
19
18
before ( async ( ) => {
20
19
await local . start ( ) ;
21
20
base = `http://${ local . hostname } :${ local . port } /` ;
21
+ global . File = File ;
22
22
} ) ;
23
23
24
24
after ( async ( ) => {
@@ -384,4 +384,58 @@ describe('Request', () => {
384
384
expect ( result ) . to . equal ( 'a=1' ) ;
385
385
} ) ;
386
386
} ) ;
387
+
388
+ it ( 'should read formData after clone with web FormData body' , async ( ) => {
389
+ const ogFormData = new WebFormData ( ) ;
390
+ ogFormData . append ( 'a' , 1 ) ;
391
+ ogFormData . append ( 'b' , 2 ) ;
392
+ ogFormData . append ( 'file' , new File ( [ 'content' ] , 'file.txt' ) ) ;
393
+
394
+ const request = new Request ( base , {
395
+ method : 'POST' ,
396
+ body : ogFormData ,
397
+ } ) ;
398
+ const clonedRequest = request . clone ( ) ;
399
+
400
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
401
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
402
+ expect ( clonedFormData . get ( 'b' ) ) . to . equal ( "2" ) ;
403
+ const file = clonedFormData . get ( 'file' )
404
+ if ( typeof file !== "object" ) {
405
+ throw new Error ( "File is not an object" ) ;
406
+ }
407
+ expect ( file . name ) . to . equal ( "file.txt" ) ;
408
+ expect ( file . type ) . to . equal ( "application/octet-stream" ) ;
409
+ expect ( file . size ) . to . equal ( 7 ) ;
410
+ expect ( await file . text ( ) ) . to . equal ( "content" ) ;
411
+ expect ( file . lastModified ) . to . be . a ( 'number' ) ;
412
+ } ) ;
413
+ } ) ;
414
+
415
+ it ( 'should read formData after clone with node FormData body' , async ( ) => {
416
+ const ogFormData = new FormData ( ) ;
417
+ ogFormData . append ( 'a' , '1' ) ;
418
+ ogFormData . append ( 'b' , '2' ) ;
419
+ ogFormData . append ( 'file' , Buffer . from ( 'content' ) , { filename : "file.txt" } ) ;
420
+
421
+ const request = new Request ( base , {
422
+ method : 'POST' ,
423
+ body : ogFormData ,
424
+ } ) ;
425
+ const clonedRequest = request . clone ( ) ;
426
+
427
+ return clonedRequest . formData ( ) . then ( async clonedFormData => {
428
+ expect ( clonedFormData . get ( 'a' ) ) . to . equal ( "1" ) ;
429
+ expect ( clonedFormData . get ( 'b' ) ) . to . equal ( "2" ) ;
430
+ const file = clonedFormData . get ( 'file' )
431
+ if ( typeof file !== "object" ) {
432
+ throw new Error ( "File is not an object" ) ;
433
+ }
434
+ expect ( file . name ) . to . equal ( "file.txt" ) ;
435
+ expect ( file . type ) . to . equal ( "text/plain" ) ;
436
+ expect ( file . size ) . to . equal ( 7 ) ;
437
+ expect ( await file . text ( ) ) . to . equal ( "content" ) ;
438
+ expect ( file . lastModified ) . to . be . a ( 'number' ) ;
439
+ } ) ;
440
+ } ) ;
387
441
} ) ;
0 commit comments