@@ -27,7 +27,7 @@ test('should store file on disk, remove on response', async function (t) {
2727 fastify . post ( '/' , async function ( req , reply ) {
2828 t . assert . ok ( req . isMultipart ( ) )
2929
30- const files = await req . saveRequestFiles ( )
30+ const { files } = await req . saveRequestFiles ( )
3131
3232 t . assert . ok ( files [ 0 ] . filepath )
3333 t . assert . strictEqual ( files [ 0 ] . fieldname , 'upload' )
@@ -135,6 +135,96 @@ test('should store file on disk, remove on response error', async function (t) {
135135 await once ( ee , 'response' )
136136} )
137137
138+ test ( 'should return saved files and values from saveRequestFiles' , async function ( t ) {
139+ t . plan ( 8 )
140+
141+ const fastify = Fastify ( )
142+ t . after ( ( ) => fastify . close ( ) )
143+
144+ fastify . register ( multipart )
145+
146+ fastify . post ( '/' , async function ( req , reply ) {
147+ const { files, values } = await req . saveRequestFiles ( )
148+
149+ t . assert . ok ( Array . isArray ( files ) )
150+ t . assert . strictEqual ( files . length , 1 )
151+ t . assert . strictEqual ( files [ 0 ] . fieldname , 'upload' )
152+ t . assert . strictEqual ( values . hello . value , 'world' )
153+ t . assert . strictEqual ( values . count . value , '42' )
154+ t . assert . ok ( values . upload )
155+
156+ reply . code ( 200 ) . send ( )
157+ } )
158+
159+ await fastify . listen ( { port : 0 } )
160+
161+ const form = new FormData ( )
162+ const opts = {
163+ protocol : 'http:' ,
164+ hostname : 'localhost' ,
165+ port : fastify . server . address ( ) . port ,
166+ path : '/' ,
167+ headers : form . getHeaders ( ) ,
168+ method : 'POST'
169+ }
170+
171+ const req = http . request ( opts )
172+ form . append ( 'hello' , 'world' )
173+ form . append ( 'count' , '42' )
174+ form . append ( 'upload' , fs . createReadStream ( filePath ) )
175+
176+ form . pipe ( req )
177+
178+ const [ res ] = await once ( req , 'response' )
179+ t . assert . strictEqual ( res . statusCode , 200 )
180+ res . resume ( )
181+ await once ( res , 'end' )
182+ t . assert . ok ( 'res ended successfully' )
183+ } )
184+
185+ test ( 'should return values when saveRequestFiles receives only fields' , async function ( t ) {
186+ t . plan ( 6 )
187+
188+ const fastify = Fastify ( )
189+ t . after ( ( ) => fastify . close ( ) )
190+
191+ fastify . register ( multipart )
192+
193+ fastify . post ( '/' , async function ( req , reply ) {
194+ const { files, values } = await req . saveRequestFiles ( )
195+
196+ t . assert . ok ( Array . isArray ( files ) )
197+ t . assert . strictEqual ( files . length , 0 )
198+ t . assert . strictEqual ( values . hello . value , 'world' )
199+ t . assert . strictEqual ( values . count . value , '42' )
200+
201+ reply . code ( 200 ) . send ( )
202+ } )
203+
204+ await fastify . listen ( { port : 0 } )
205+
206+ const form = new FormData ( )
207+ const opts = {
208+ protocol : 'http:' ,
209+ hostname : 'localhost' ,
210+ port : fastify . server . address ( ) . port ,
211+ path : '/' ,
212+ headers : form . getHeaders ( ) ,
213+ method : 'POST'
214+ }
215+
216+ const req = http . request ( opts )
217+ form . append ( 'hello' , 'world' )
218+ form . append ( 'count' , '42' )
219+ form . pipe ( req )
220+
221+ const [ res ] = await once ( req , 'response' )
222+ t . assert . strictEqual ( res . statusCode , 200 )
223+ res . resume ( )
224+ await once ( res , 'end' )
225+ t . assert . ok ( 'res ended successfully' )
226+ } )
227+
138228test ( 'should throw on file limit error' , async function ( t ) {
139229 t . plan ( 4 )
140230
0 commit comments