@@ -753,6 +753,51 @@ test('serving disabled', async (t) => {
753753 } )
754754} )
755755
756+ test ( 'serving disabled without root' , async ( t ) => {
757+ t . plan ( 2 )
758+
759+ const pluginOptions = {
760+ prefix : '/static/' ,
761+ serve : false
762+ }
763+ const fastify = Fastify ( )
764+ fastify . register ( fastifyStatic , pluginOptions )
765+
766+ t . after ( ( ) => fastify . close ( ) )
767+
768+ fastify . get ( '/foo/bar/r' , ( _request , reply ) => {
769+ reply . sendFile ( 'index.html' )
770+ } )
771+
772+ fastify . get ( '/foo/bar/a' , ( _request , reply ) => {
773+ reply . sendFile ( path . join ( __dirname , pluginOptions . prefix , 'index.html' ) )
774+ } )
775+
776+ t . after ( ( ) => fastify . close ( ) )
777+
778+ await fastify . listen ( { port : 0 } )
779+
780+ fastify . server . unref ( )
781+
782+ await t . test ( '/static/index.html via sendFile not found' , async ( t ) => {
783+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
784+
785+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/foo/bar/a' )
786+ t . assert . ok ( response . ok )
787+ t . assert . deepStrictEqual ( response . status , 200 )
788+ t . assert . deepStrictEqual ( await response . text ( ) , indexContent )
789+ genericResponseChecks ( t , response )
790+ } )
791+
792+ await t . test ( '/static/index.html via sendFile with relative path not found' , async ( t ) => {
793+ t . plan ( 2 )
794+
795+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/foo/bar/r' )
796+ t . assert . ok ( ! response . ok )
797+ t . assert . deepStrictEqual ( response . status , 404 )
798+ } )
799+ } )
800+
756801test ( 'sendFile' , async ( t ) => {
757802 t . plan ( 4 )
758803
@@ -1215,7 +1260,7 @@ test('maxAge option', async (t) => {
12151260} )
12161261
12171262test ( 'errors' , async ( t ) => {
1218- t . plan ( 11 )
1263+ t . plan ( 12 )
12191264
12201265 await t . test ( 'no root' , async ( t ) => {
12211266 t . plan ( 1 )
@@ -1280,6 +1325,16 @@ test('errors', async (t) => {
12801325 await t . assert . rejects ( async ( ) => await fastify . register ( fastifyStatic , pluginOptions ) )
12811326 } )
12821327
1328+ await t . test ( 'no root and serve: false' , async ( t ) => {
1329+ t . plan ( 1 )
1330+ const pluginOptions = {
1331+ serve : false ,
1332+ root : [ ]
1333+ }
1334+ const fastify = Fastify ( { logger : false } )
1335+ await t . assert . rejects ( async ( ) => await fastify . register ( fastifyStatic , pluginOptions ) )
1336+ } )
1337+
12831338 await t . test ( 'duplicate root paths are not allowed' , async ( t ) => {
12841339 t . plan ( 1 )
12851340 const pluginOptions = {
0 commit comments