@@ -316,3 +316,127 @@ test('should NOT throw fileSize limitation error when throwFileSizeLimit is glob
316316 t . assert . ifError ( error )
317317 }
318318} )
319+
320+ test ( 'should throw fileSize limitation error when used alongside attachFieldsToBody and set request config' , async function ( t ) {
321+ t . plan ( 1 )
322+
323+ const fastify = Fastify ( )
324+ t . after ( ( ) => fastify . close ( ) )
325+
326+ fastify . register ( multipart , {
327+ attachFieldsToBody : true
328+ } )
329+
330+ const randomFileBuffer = Buffer . alloc ( 900_000 )
331+ crypto . randomFillSync ( randomFileBuffer )
332+
333+ fastify . post ( '/' , {
334+ config : {
335+ multipartOptions : {
336+ limits : {
337+ fileSize : 800_000
338+ }
339+ }
340+ }
341+ } , async function ( req , reply ) {
342+ t . assert . fail ( 'it should throw' )
343+
344+ reply . status ( 200 ) . send ( )
345+ } )
346+
347+ await fastify . listen ( { port : 0 } )
348+
349+ // request
350+ const form = new FormData ( )
351+ const opts = {
352+ hostname : '127.0.0.1' ,
353+ port : fastify . server . address ( ) . port ,
354+ path : '/' ,
355+ headers : form . getHeaders ( ) ,
356+ method : 'POST'
357+ }
358+
359+ const tmpFile = 'test/random-file'
360+ fs . writeFileSync ( tmpFile , randomFileBuffer )
361+
362+ const req = http . request ( opts )
363+ form . append ( 'upload' , fs . createReadStream ( tmpFile ) )
364+
365+ form . pipe ( req )
366+
367+ try {
368+ const [ res ] = await once ( req , 'response' )
369+ t . assert . equal ( res . statusCode , 413 )
370+ res . resume ( )
371+ await once ( res , 'end' )
372+
373+ fs . unlinkSync ( tmpFile )
374+ } catch ( error ) {
375+ t . assert . ifError ( error )
376+ }
377+ } )
378+
379+ test ( 'should not throw fileSize limitation error when used alongside attachFieldsToBody and set request config' , async function ( t ) {
380+ t . plan ( 4 )
381+
382+ const fastify = Fastify ( )
383+ t . after ( ( ) => fastify . close ( ) )
384+
385+ fastify . register ( multipart , {
386+ attachFieldsToBody : true
387+ } )
388+
389+ const randomFileBuffer = Buffer . alloc ( 900_000 )
390+ crypto . randomFillSync ( randomFileBuffer )
391+
392+ fastify . post ( '/' , {
393+ config : {
394+ multipartOptions : {
395+ limits : {
396+ fileSize : 1_000_000
397+ }
398+ }
399+ }
400+ } , async function ( req , reply ) {
401+ t . assert . ok ( req . isMultipart ( ) )
402+
403+ t . assert . deepStrictEqual ( Object . keys ( req . body ) , [ 'upload' ] )
404+
405+ const content = await req . body . upload . toBuffer ( )
406+
407+ t . assert . strictEqual ( content . toString ( ) , randomFileBuffer . toString ( ) )
408+
409+ reply . status ( 200 ) . send ( )
410+ } )
411+
412+ await fastify . listen ( { port : 0 } )
413+
414+ // request
415+ const form = new FormData ( )
416+ const opts = {
417+ hostname : '127.0.0.1' ,
418+ port : fastify . server . address ( ) . port ,
419+ path : '/' ,
420+ headers : form . getHeaders ( ) ,
421+ method : 'POST'
422+ }
423+
424+ const tmpFile = 'test/random-file'
425+ fs . writeFileSync ( tmpFile , randomFileBuffer )
426+
427+ const req = http . request ( opts )
428+ form . append ( 'upload' , fs . createReadStream ( tmpFile ) )
429+
430+ form . pipe ( req )
431+
432+ try {
433+ const [ res ] = await once ( req , 'response' )
434+ t . assert . equal ( res . statusCode , 200 )
435+ res . resume ( )
436+ await once ( res , 'end' )
437+
438+ fs . unlinkSync ( tmpFile )
439+ } catch ( error ) {
440+ t . assert . ifError ( error )
441+ }
442+ } )
0 commit comments