File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -399,6 +399,41 @@ test(
399
399
}
400
400
)
401
401
402
+ test (
403
+ "Does not imclude Content-Length header with enableAdditionalHeaders "
404
+ + "option if entry does not have known length" ,
405
+
406
+ async t => {
407
+ const form = new FormData ( )
408
+
409
+ form . set ( "stream" , {
410
+ [ Symbol . toStringTag ] : "File" ,
411
+ name : "file.txt" ,
412
+ stream ( ) {
413
+ return Readable . from ( [ Buffer . from ( "foo" ) ] )
414
+ }
415
+ } )
416
+
417
+ const encoder = new FormDataEncoder ( form , {
418
+ enableAdditionalHeaders : true
419
+ } )
420
+
421
+ const iterable = readLine ( Readable . from ( encoder ) )
422
+
423
+ await skip ( iterable , 1 )
424
+ const headers : string [ ] = [ ]
425
+ for await ( const chunk of iterable ) {
426
+ if ( chunk === "" ) {
427
+ break
428
+ }
429
+
430
+ headers . push ( chunk . split ( ":" ) [ 0 ] . toLowerCase ( ) )
431
+ }
432
+
433
+ t . false ( headers . includes ( "content-length" ) )
434
+ }
435
+ )
436
+
402
437
test ( "Yields File's content" , async t => {
403
438
const filePath = "license"
404
439
const form = new FormData ( )
Original file line number Diff line number Diff line change
1
+ /* eslint-disable no-restricted-globals */
1
2
import type { LowercaseObjectKeys } from "./util/LowercaseObjectKeys.js"
2
3
import { createBoundary } from "./util/createBoundary.js"
3
4
import { normalizeValue } from "./util/normalizeValue.js"
@@ -207,7 +208,11 @@ export class FormDataEncoder {
207
208
}
208
209
209
210
const size = isFile ( value ) ? value . size : value . byteLength
210
- if ( this . #options. enableAdditionalHeaders === true && size != null ) {
211
+ if (
212
+ this . #options. enableAdditionalHeaders === true
213
+ && size != null
214
+ && ! isNaN ( size )
215
+ ) {
211
216
header += `${ this . #CRLF} Content-Length: ${
212
217
isFile ( value ) ? value . size : value . byteLength
213
218
} `
@@ -230,7 +235,7 @@ export class FormDataEncoder {
230
235
const size = isFile ( value ) ? value . size : value . byteLength
231
236
232
237
// Return `undefined` if encountered part without known size
233
- if ( size == null || Number . isNaN ( size ) ) {
238
+ if ( size == null || isNaN ( size ) ) {
234
239
return undefined
235
240
}
236
241
You can’t perform that action at this time.
0 commit comments