@@ -147,7 +147,7 @@ test("Yields correct footer for empty FormData", async t => {
147
147
t . is ( value , `--${ encoder . boundary } --` )
148
148
} )
149
149
150
- test ( "The footer ends with two crlf" , async t => {
150
+ test ( "The footer ends with double crlf" , async t => {
151
151
const actual = await readStream ( new FormDataEncoder ( new FormData ( ) ) , true )
152
152
153
153
t . true ( actual . endsWith ( "\r\n\r\n" ) )
@@ -161,36 +161,36 @@ test("Returns correct length of the empty FormData content", async t => {
161
161
} )
162
162
163
163
test ( "Returns the length of the FormData content" , async t => {
164
- const fd = new FormData ( )
164
+ const form = new FormData ( )
165
165
166
- fd . set ( "field" , "Some string" )
167
- fd . set ( "file" , new File ( [ "Some content" ] , "file.txt" ) )
166
+ form . set ( "field" , "Some string" )
167
+ form . set ( "file" , new File ( [ "Some content" ] , "file.txt" ) )
168
168
169
- const encoder = new FormDataEncoder ( fd )
169
+ const encoder = new FormDataEncoder ( form )
170
170
171
171
const expected = await readStream ( encoder ) . then ( ( { length} ) => length )
172
172
173
173
t . is < number > ( encoder . getContentLength ( ) , expected )
174
174
} )
175
175
176
176
test ( ".values() yields headers as Uint8Array" , t => {
177
- const fd = new FormData ( )
177
+ const form = new FormData ( )
178
178
179
- fd . set ( "field" , "Some value" )
179
+ form . set ( "field" , "Some value" )
180
180
181
- const iterable = new FormDataEncoder ( fd ) . values ( )
181
+ const iterable = new FormDataEncoder ( form ) . values ( )
182
182
183
183
const { value : actual } = skipSync ( iterable )
184
184
185
185
t . true ( actual instanceof Uint8Array )
186
186
} )
187
187
188
188
test ( ".valeus() yields field as Uint8Array" , t => {
189
- const fd = new FormData ( )
189
+ const form = new FormData ( )
190
190
191
- fd . set ( "field" , "Some value" )
191
+ form . set ( "field" , "Some value" )
192
192
193
- const { value : actual } = skipSync ( new FormDataEncoder ( fd ) . values ( ) , 2 )
193
+ const { value : actual } = skipSync ( new FormDataEncoder ( form ) . values ( ) , 2 )
194
194
195
195
t . true ( actual instanceof Uint8Array )
196
196
} )
@@ -199,34 +199,34 @@ test(".valeus() yields field's content", t => {
199
199
const string = "Some value"
200
200
const expected = new TextEncoder ( ) . encode ( string )
201
201
202
- const fd = new FormData ( )
202
+ const form = new FormData ( )
203
203
204
- fd . set ( "field" , string )
204
+ form . set ( "field" , string )
205
205
206
- const { value : actual } = skipSync ( new FormDataEncoder ( fd ) . values ( ) , 2 )
206
+ const { value : actual } = skipSync ( new FormDataEncoder ( form ) . values ( ) , 2 )
207
207
208
208
t . true ( Buffer . from ( actual as Uint8Array ) . equals ( expected ) )
209
209
} )
210
210
211
211
test ( ".values() yields a file as is" , async t => {
212
212
const file = new File ( [ "File content" ] , "name.txt" )
213
213
214
- const fd = new FormData ( )
214
+ const form = new FormData ( )
215
215
216
- fd . set ( "file" , file )
216
+ form . set ( "file" , file )
217
217
218
- const { value : actual } = skipSync ( new FormDataEncoder ( fd ) . values ( ) , 2 )
218
+ const { value : actual } = skipSync ( new FormDataEncoder ( form ) . values ( ) , 2 )
219
219
220
220
t . true ( actual instanceof File )
221
221
t . is ( await ( actual as File ) . text ( ) , await file . text ( ) )
222
222
} )
223
223
224
224
test ( "Yields correct headers for a field" , async t => {
225
- const fd = new FormData ( )
225
+ const form = new FormData ( )
226
226
227
- fd . set ( "field" , "Some value" )
227
+ form . set ( "field" , "Some value" )
228
228
229
- const iterable = readLine ( Readable . from ( new FormDataEncoder ( fd ) ) )
229
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form ) ) )
230
230
231
231
const { value} = await skip ( iterable , 2 )
232
232
@@ -236,25 +236,25 @@ test("Yields correct headers for a field", async t => {
236
236
test ( "Yields field's content" , async t => {
237
237
const expected = "Some value"
238
238
239
- const fd = new FormData ( )
239
+ const form = new FormData ( )
240
240
241
- fd . set ( "field" , expected )
241
+ form . set ( "field" , expected )
242
242
243
243
const {
244
244
value
245
- } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( fd ) ) ) , 4 )
245
+ } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( form ) ) ) , 4 )
246
246
247
247
t . is ( value , expected )
248
248
} )
249
249
250
250
test ( "Yields Content-Disposition header for a File" , async t => {
251
- const fd = new FormData ( )
251
+ const form = new FormData ( )
252
252
253
- fd . set ( "file" , new File ( [ "My hovercraft is full of eels" ] , "file.txt" ) )
253
+ form . set ( "file" , new File ( [ "My hovercraft is full of eels" ] , "file.txt" ) )
254
254
255
255
const {
256
256
value
257
- } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( fd ) ) ) , 2 )
257
+ } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( form ) ) ) , 2 )
258
258
259
259
t . is (
260
260
value ,
@@ -263,43 +263,81 @@ test("Yields Content-Disposition header for a File", async t => {
263
263
} )
264
264
265
265
test ( "Yields Content-Type header for a File" , async t => {
266
- const fd = new FormData ( )
266
+ const form = new FormData ( )
267
267
268
- fd . set ( "file" , new File ( [ "My hovercraft is full of eels" ] , "file.txt" , {
268
+ form . set ( "file" , new File ( [ "My hovercraft is full of eels" ] , "file.txt" , {
269
269
type : "text/plain"
270
270
} ) )
271
271
272
272
const {
273
273
value
274
- } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( fd ) ) ) , 3 )
274
+ } = await skip ( readLine ( Readable . from ( new FormDataEncoder ( form ) ) ) , 3 )
275
275
276
276
t . is ( value , "Content-Type: text/plain" )
277
277
} )
278
278
279
279
test (
280
280
"File has default Content-Type set to application/octet-stream" ,
281
281
async t => {
282
- const fd = new FormData ( )
282
+ const form = new FormData ( )
283
283
284
- fd . set ( "file" , new File ( [ "Some content" ] , "file" ) )
284
+ form . set ( "file" , new File ( [ "Some content" ] , "file" ) )
285
285
286
- const iterable = readLine ( Readable . from ( new FormDataEncoder ( fd ) ) )
286
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form ) ) )
287
287
288
288
const { value} = await skip ( iterable , 3 )
289
289
290
290
t . is ( value , "Content-Type: application/octet-stream" )
291
291
}
292
292
)
293
293
294
+ test (
295
+ "Yields Content-Length File header when enableAdditionalHeaders option is on" ,
296
+
297
+ async t => {
298
+ const form = new FormData ( )
299
+ const file = new File ( [ "Some content" ] , "file" )
300
+
301
+ form . set ( "file" , file )
302
+
303
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form , {
304
+ enableAdditionalHeaders : true
305
+ } ) ) )
306
+
307
+ const { value} = await skip ( iterable , 4 )
308
+
309
+ t . is ( value , `Content-Length: ${ file . size } ` )
310
+ }
311
+ )
312
+
313
+ test (
314
+ "Yields Content-Length header when enableAdditionalHeaders option is on" ,
315
+
316
+ async t => {
317
+ const form = new FormData ( )
318
+ const field = "Some value"
319
+
320
+ form . set ( "field" , field )
321
+
322
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form , {
323
+ enableAdditionalHeaders : true
324
+ } ) ) )
325
+
326
+ const { value} = await skip ( iterable , 3 )
327
+
328
+ t . is ( value , `Content-Length: ${ Buffer . byteLength ( field ) } ` )
329
+ }
330
+ )
331
+
294
332
test ( "Yields File's content" , async t => {
295
333
const filePath = "license"
296
- const fd = new FormData ( )
334
+ const form = new FormData ( )
297
335
298
336
const expected = await fs . readFile ( filePath , "utf-8" )
299
337
300
- fd . set ( "license" , await fileFromPath ( filePath ) )
338
+ form . set ( "license" , await fileFromPath ( filePath ) )
301
339
302
- const encoder = new FormDataEncoder ( fd )
340
+ const encoder = new FormDataEncoder ( form )
303
341
const iterable = readLine ( Readable . from ( encoder ) )
304
342
305
343
await skip ( iterable , 4 )
@@ -322,12 +360,12 @@ test("Yields File's content", async t => {
322
360
test ( "Yields every appended field" , async t => {
323
361
const expectedDisposition = "Content-Disposition: form-data; name=\"field\""
324
362
325
- const fd = new FormData ( )
363
+ const form = new FormData ( )
326
364
327
- fd . append ( "field" , "Some string" )
328
- fd . append ( "field" , "Some other string" )
365
+ form . append ( "field" , "Some string" )
366
+ form . append ( "field" , "Some other string" )
329
367
330
- const iterable = readLine ( Readable . from ( new FormDataEncoder ( fd ) ) )
368
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form ) ) )
331
369
332
370
const { value : firstFieldDisposition } = await skip ( iterable , 2 )
333
371
@@ -349,17 +387,17 @@ test("Yields every appended field", async t => {
349
387
test ( "Yields every appended File" , async t => {
350
388
const expectedDisposition = "Content-Disposition: form-data; name=\"file\""
351
389
352
- const fd = new FormData ( )
390
+ const form = new FormData ( )
353
391
354
392
const firstFile = new File ( [ "Some content" ] , "file.txt" , { type : "text/plain" } )
355
393
const secondFile = new File ( [ "Some **content**" ] , "file.md" , {
356
394
type : "text/markdown"
357
395
} )
358
396
359
- fd . append ( "file" , firstFile )
360
- fd . append ( "file" , secondFile )
397
+ form . append ( "file" , firstFile )
398
+ form . append ( "file" , secondFile )
361
399
362
- const iterable = readLine ( Readable . from ( new FormDataEncoder ( fd ) ) )
400
+ const iterable = readLine ( Readable . from ( new FormDataEncoder ( form ) ) )
363
401
364
402
const { value : firstFileDisposition } = await skip ( iterable , 2 )
365
403
@@ -387,12 +425,12 @@ test("Yields every appended File", async t => {
387
425
} )
388
426
389
427
test ( "Can be read through using Blob" , async t => {
390
- const fd = new FormData ( )
428
+ const form = new FormData ( )
391
429
392
- fd . set ( "field" , "Some field" )
393
- fd . set ( "file" , await fileFromPath ( "license" , { type : "text/plain" } ) )
430
+ form . set ( "field" , "Some field" )
431
+ form . set ( "file" , await fileFromPath ( "license" , { type : "text/plain" } ) )
394
432
395
- const encoder = new FormDataEncoder ( fd )
433
+ const encoder = new FormDataEncoder ( form )
396
434
const blob = new Blob ( [ ...encoder ] as any [ ] )
397
435
398
436
t . true (
@@ -421,6 +459,16 @@ test("Throws TypeError when given boundary is not a string", t => {
421
459
422
460
t . throws ( trap , {
423
461
instanceOf : TypeError ,
424
- message : "Expected boundary to be a string."
462
+ message : "Expected boundary argument to be a string."
463
+ } )
464
+ } )
465
+
466
+ test ( "Throws TypeError when options argument is not an object" , t => {
467
+ // @ts -expect-error
468
+ const trap = ( ) => new FormDataEncoder ( new FormData ( ) , undefined , "451" )
469
+
470
+ t . throws ( trap , {
471
+ instanceOf : TypeError ,
472
+ message : "Expected options argument to be an object."
425
473
} )
426
474
} )
0 commit comments