@@ -106,6 +106,26 @@ test("Has contentLength property", async t => {
106106 )
107107} )
108108
109+ test (
110+ "contentLength property is undefined if there's file without known length" ,
111+
112+ t => {
113+ const form = new FormData ( )
114+
115+ form . set ( "stream" , {
116+ [ Symbol . toStringTag ] : "File" ,
117+ name : "file.txt" ,
118+ stream ( ) {
119+ return Readable . from ( [ Buffer . from ( "foo" ) ] )
120+ }
121+ } )
122+
123+ const encoder = new FormDataEncoder ( form )
124+
125+ t . is ( encoder . contentLength , undefined )
126+ }
127+ )
128+
109129test ( "contentLength property is read-only" , t => {
110130 const encoder = new FormDataEncoder ( new FormData ( ) )
111131
@@ -137,6 +157,28 @@ test("Has correct headers", async t => {
137157 } )
138158} )
139159
160+ test (
161+ "Has only Content-Type header if there's file without known length" ,
162+
163+ t => {
164+ const form = new FormData ( )
165+
166+ form . set ( "stream" , {
167+ [ Symbol . toStringTag ] : "File" ,
168+ name : "file.txt" ,
169+ stream ( ) {
170+ return Readable . from ( [ Buffer . from ( "foo" ) ] )
171+ }
172+ } )
173+
174+ const encoder = new FormDataEncoder ( form )
175+
176+ t . deepEqual ( encoder . headers , {
177+ "Content-Type" : `multipart/form-data; boundary=${ encoder . boundary } `
178+ } )
179+ }
180+ )
181+
140182test ( "Yields correct footer for empty FormData" , async t => {
141183 const encoder = new FormDataEncoder ( new FormData ( ) )
142184
@@ -157,7 +199,7 @@ test("Returns correct length of the empty FormData content", async t => {
157199 const encoder = new FormDataEncoder ( new FormData ( ) )
158200 const expected = await readStream ( encoder ) . then ( ( { length} ) => length )
159201
160- t . is < number > ( encoder . getContentLength ( ) , expected )
202+ t . is ( encoder . getContentLength ( ) , expected )
161203} )
162204
163205test ( "Returns the length of the FormData content" , async t => {
@@ -170,9 +212,29 @@ test("Returns the length of the FormData content", async t => {
170212
171213 const expected = await readStream ( encoder ) . then ( ( { length} ) => length )
172214
173- t . is < number > ( encoder . getContentLength ( ) , expected )
215+ t . is ( encoder . getContentLength ( ) , expected )
174216} )
175217
218+ test (
219+ ".getContentLength() returns undefined if there's file without known length" ,
220+
221+ t => {
222+ const form = new FormData ( )
223+
224+ form . set ( "stream" , {
225+ [ Symbol . toStringTag ] : "File" ,
226+ name : "file.txt" ,
227+ stream ( ) {
228+ return Readable . from ( [ Buffer . from ( "foo" ) ] )
229+ }
230+ } )
231+
232+ const encoder = new FormDataEncoder ( form )
233+
234+ t . is ( encoder . getContentLength ( ) , undefined )
235+ }
236+ )
237+
176238test ( ".values() yields headers as Uint8Array" , t => {
177239 const form = new FormData ( )
178240
@@ -329,6 +391,41 @@ test(
329391 }
330392)
331393
394+ test (
395+ "Does not imclude Content-Length header with enableAdditionalHeaders "
396+ + "option if entry does not have known length" ,
397+
398+ async t => {
399+ const form = new FormData ( )
400+
401+ form . set ( "stream" , {
402+ [ Symbol . toStringTag ] : "File" ,
403+ name : "file.txt" ,
404+ stream ( ) {
405+ return Readable . from ( [ Buffer . from ( "foo" ) ] )
406+ }
407+ } )
408+
409+ const encoder = new FormDataEncoder ( form , {
410+ enableAdditionalHeaders : true
411+ } )
412+
413+ const iterable = readLine ( Readable . from ( encoder ) )
414+
415+ await skip ( iterable , 1 )
416+ const headers : string [ ] = [ ]
417+ for await ( const chunk of iterable ) {
418+ if ( chunk === "" ) {
419+ break
420+ }
421+
422+ headers . push ( chunk . split ( ":" ) [ 0 ] . toLowerCase ( ) )
423+ }
424+
425+ t . false ( headers . includes ( "content-length" ) )
426+ }
427+ )
428+
332429test ( "Yields File's content" , async t => {
333430 const filePath = "license"
334431 const form = new FormData ( )
@@ -354,7 +451,7 @@ test("Yields File's content", async t => {
354451 chunks . pop ( ) // Remove trailing empty line
355452
356453 // It looks like files on Windows have different EOL when you read them, even though they were created on macOS x)
357- t . is < string > ( chunks . join ( EOL ) , expected )
454+ t . is ( chunks . join ( EOL ) , expected )
358455} )
359456
360457test ( "Yields every appended field" , async t => {
0 commit comments