@@ -3,8 +3,9 @@ import {Readable} from "stream"
33
44import test from "ava"
55
6- import { FormData , File , fileFromPath } from "formdata-node"
6+ import { FormData , Blob , File , fileFromPath } from "formdata-node"
77
8+ import skipSync from "./__helper__/skipIterationsSync"
89import readStream from "./__helper__/readStream"
910import skip from "./__helper__/skipIterations"
1011import readLine from "./__helper__/readLine"
@@ -87,6 +88,54 @@ test("Returns the length of the FormData content", async t => {
8788 t . is < number > ( encoder . getContentLength ( ) , expected )
8889} )
8990
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+
90139test ( "Yields correct headers for a field" , async t => {
91140 const fd = new FormData ( )
92141
@@ -216,7 +265,6 @@ test("Yields every appended File", async t => {
216265 } )
217266
218267 fd . append ( "file" , firstFile )
219-
220268 fd . append ( "file" , secondFile )
221269
222270 const iterable = readLine ( Readable . from ( new Encoder ( fd ) ) )
@@ -246,6 +294,22 @@ test("Yields every appended File", async t => {
246294 t . is ( secondFileContent , await secondFile . text ( ) )
247295} )
248296
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+
249313test (
250314 "Throws TypeError when the first argument is not a FormData instance" ,
251315 t => {
0 commit comments