@@ -3,8 +3,9 @@ import {Readable} from "stream"
3
3
4
4
import test from "ava"
5
5
6
- import { FormData , File , fileFromPath } from "formdata-node"
6
+ import { FormData , Blob , File , fileFromPath } from "formdata-node"
7
7
8
+ import skipSync from "./__helper__/skipIterationsSync"
8
9
import readStream from "./__helper__/readStream"
9
10
import skip from "./__helper__/skipIterations"
10
11
import readLine from "./__helper__/readLine"
@@ -87,6 +88,54 @@ test("Returns the length of the FormData content", async t => {
87
88
t . is < number > ( encoder . getContentLength ( ) , expected )
88
89
} )
89
90
91
+ test ( ".values() yields headers as Uint8Array" , t => {
92
+ const fd = new FormData ( )
93
+
94
+ fd . set ( "field" , "Some value" )
95
+
96
+ const iterable = new Encoder ( fd ) . values ( )
97
+
98
+ const { value : actual } = skipSync ( iterable )
99
+
100
+ t . true ( actual instanceof Uint8Array )
101
+ } )
102
+
103
+ test ( ".valeus() yields field as Uint8Array" , t => {
104
+ const fd = new FormData ( )
105
+
106
+ fd . set ( "field" , "Some value" )
107
+
108
+ const { value : actual } = skipSync ( new Encoder ( fd ) . values ( ) , 2 )
109
+
110
+ t . true ( actual instanceof Uint8Array )
111
+ } )
112
+
113
+ test ( ".valeus() yields field's content" , t => {
114
+ const string = "Some value"
115
+ const expected = new TextEncoder ( ) . encode ( string )
116
+
117
+ const fd = new FormData ( )
118
+
119
+ fd . set ( "field" , string )
120
+
121
+ const { value : actual } = skipSync ( new Encoder ( fd ) . values ( ) , 2 )
122
+
123
+ t . true ( Buffer . from ( actual as Uint8Array ) . equals ( expected ) )
124
+ } )
125
+
126
+ test ( ".values() yields a file as is" , async t => {
127
+ const file = new File ( [ "File content" ] , "name.txt" )
128
+
129
+ const fd = new FormData ( )
130
+
131
+ fd . set ( "file" , file )
132
+
133
+ const { value : actual } = skipSync ( new Encoder ( fd ) . values ( ) , 2 )
134
+
135
+ t . true ( actual instanceof File )
136
+ t . is ( await ( actual as File ) . text ( ) , await file . text ( ) )
137
+ } )
138
+
90
139
test ( "Yields correct headers for a field" , async t => {
91
140
const fd = new FormData ( )
92
141
@@ -216,7 +265,6 @@ test("Yields every appended File", async t => {
216
265
} )
217
266
218
267
fd . append ( "file" , firstFile )
219
-
220
268
fd . append ( "file" , secondFile )
221
269
222
270
const iterable = readLine ( Readable . from ( new Encoder ( fd ) ) )
@@ -246,6 +294,22 @@ test("Yields every appended File", async t => {
246
294
t . is ( secondFileContent , await secondFile . text ( ) )
247
295
} )
248
296
297
+ test ( "Can be read through using Blob" , async t => {
298
+ const fd = new FormData ( )
299
+
300
+ fd . set ( "field" , "Some field" )
301
+ fd . set ( "file" , await fileFromPath ( "license" , { type : "text/plain" } ) )
302
+
303
+ const encoder = new Encoder ( fd )
304
+ const blob = new Blob ( [ ...encoder ] as any [ ] )
305
+
306
+ t . true (
307
+ Buffer
308
+ . from ( await blob . arrayBuffer ( ) )
309
+ . equals ( await readStream ( Readable . from ( encoder ) ) )
310
+ )
311
+ } )
312
+
249
313
test (
250
314
"Throws TypeError when the first argument is not a FormData instance" ,
251
315
t => {
0 commit comments