@@ -870,6 +870,83 @@ test('sendFile', async (t) => {
870870 } )
871871} )
872872
873+ test ( 'sendFile with multiple roots' , async ( t ) => {
874+ t . plan ( 4 )
875+
876+ const pluginOptions = {
877+ root : [ path . join ( __dirname , '/static' ) , path . join ( __dirname , '/static2' ) ] ,
878+ prefix : '/static'
879+ }
880+ const fastify = Fastify ( )
881+ const maxAge = Math . round ( Math . random ( ) * 10 ) * 10000
882+ fastify . register ( fastifyStatic , pluginOptions )
883+
884+ t . after ( ( ) => fastify . close ( ) )
885+
886+ fastify . get ( '/foo' , function ( _req , rep ) {
887+ rep . sendFile ( 'foo.html' )
888+ } )
889+
890+ fastify . get ( '/foo-with-options' , function ( _req , rep ) {
891+ rep . sendFile ( 'foo.html' , { maxAge } )
892+ } )
893+
894+ fastify . get ( '/bar' , function ( _req , rep ) {
895+ rep . sendFile ( 'bar.html' )
896+ } )
897+
898+ fastify . get ( '/bar-with-options' , function ( _req , rep ) {
899+ rep . sendFile ( 'bar.html' , { maxAge } )
900+ } )
901+
902+ await fastify . listen ( { port : 0 } )
903+ fastify . server . unref ( )
904+
905+ await t . test ( 'reply.sendFile() first root' , async ( t ) => {
906+ t . plan ( 4 + GENERIC_RESPONSE_CHECK_COUNT )
907+
908+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/foo' )
909+ t . assert . ok ( response . ok )
910+ t . assert . deepStrictEqual ( response . status , 200 )
911+ t . assert . deepStrictEqual ( await response . text ( ) , fooContent )
912+ t . assert . deepStrictEqual ( response . headers . get ( 'cache-control' ) , 'public, max-age=0' )
913+ genericResponseChecks ( t , response )
914+ } )
915+
916+ await t . test ( 'reply.sendFile() first root with options' , async ( t ) => {
917+ t . plan ( 4 + GENERIC_RESPONSE_CHECK_COUNT )
918+
919+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/foo-with-options' )
920+ t . assert . ok ( response . ok )
921+ t . assert . deepStrictEqual ( response . status , 200 )
922+ t . assert . deepStrictEqual ( await response . text ( ) , fooContent )
923+ t . assert . deepStrictEqual ( response . headers . get ( 'cache-control' ) , `public, max-age=${ maxAge / 1000 } ` )
924+ genericResponseChecks ( t , response )
925+ } )
926+
927+ await t . test ( 'reply.sendFile() second root' , async ( t ) => {
928+ t . plan ( 4 + GENERIC_RESPONSE_CHECK_COUNT )
929+
930+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/bar' )
931+ t . assert . ok ( response . ok )
932+ t . assert . deepStrictEqual ( response . status , 200 )
933+ t . assert . deepStrictEqual ( await response . text ( ) , barContent )
934+ t . assert . deepStrictEqual ( response . headers . get ( 'cache-control' ) , 'public, max-age=0' )
935+ genericResponseChecks ( t , response )
936+ } )
937+
938+ await t . test ( 'reply.sendFile() second root with options' , async ( t ) => {
939+ t . plan ( 4 + GENERIC_RESPONSE_CHECK_COUNT )
940+
941+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/bar-with-options' )
942+ t . assert . ok ( response . ok )
943+ t . assert . deepStrictEqual ( response . status , 200 )
944+ t . assert . deepStrictEqual ( await response . text ( ) , barContent )
945+ t . assert . deepStrictEqual ( response . headers . get ( 'cache-control' ) , `public, max-age=${ maxAge / 1000 } ` )
946+ genericResponseChecks ( t , response )
947+ } )
948+ } )
949+
873950test ( 'sendFile disabled' , async ( t ) => {
874951 t . plan ( 1 )
875952
0 commit comments