@@ -3492,3 +3492,124 @@ test('respect the .type when using with sendFile with contentType disabled', asy
3492
3492
t . assert . deepStrictEqual ( response . headers . get ( 'content-length' ) , contentLength )
3493
3493
t . assert . deepStrictEqual ( await response . text ( ) , fooContent )
3494
3494
} )
3495
+
3496
+ test ( 'register /static/ with custom log level' , async t => {
3497
+ t . plan ( 9 )
3498
+
3499
+ const pluginOptions = {
3500
+ root : path . join ( __dirname , '/static' ) ,
3501
+ prefix : '/static/' ,
3502
+ logLevel : 'warn'
3503
+ }
3504
+ const fastify = Fastify ( {
3505
+ logger : {
3506
+ stream : {
3507
+ write : ( logLine ) => {
3508
+ if ( logLine . includes ( '"msg":"incoming request"' ) ) {
3509
+ console . warn ( logLine )
3510
+ throw new Error ( 'Should never reach this point since log level is set at WARN!! Unexpected log line: ' + logLine )
3511
+ }
3512
+ } ,
3513
+ } ,
3514
+ } ,
3515
+ } )
3516
+ fastify . register ( fastifyStatic , pluginOptions )
3517
+
3518
+ t . after ( ( ) => fastify . close ( ) )
3519
+
3520
+ await fastify . listen ( { port : 0 } )
3521
+ fastify . server . unref ( )
3522
+
3523
+ await t . test ( '/static/index.html' , async ( t ) => {
3524
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3525
+
3526
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html' )
3527
+ t . assert . ok ( response . ok )
3528
+ t . assert . deepStrictEqual ( response . status , 200 )
3529
+ t . assert . deepStrictEqual ( await response . text ( ) , indexContent )
3530
+ genericResponseChecks ( t , response )
3531
+ } )
3532
+
3533
+ await t . test ( '/static/index.html' , async t => {
3534
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3535
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html' , { method : 'HEAD' } )
3536
+ t . assert . ok ( response . ok )
3537
+ t . assert . deepStrictEqual ( response . status , 200 )
3538
+ t . assert . deepStrictEqual ( await response . text ( ) , '' )
3539
+ genericResponseChecks ( t , response )
3540
+ } )
3541
+
3542
+ await t . test ( '/static/index.css' , async ( t ) => {
3543
+ t . plan ( 2 + GENERIC_RESPONSE_CHECK_COUNT )
3544
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.css' )
3545
+ t . assert . ok ( response . ok )
3546
+ t . assert . deepStrictEqual ( response . status , 200 )
3547
+ genericResponseChecks ( t , response )
3548
+ } )
3549
+
3550
+ await t . test ( '/static/' , async ( t ) => {
3551
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3552
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/' )
3553
+
3554
+ t . assert . ok ( response . ok )
3555
+ t . assert . deepStrictEqual ( response . status , 200 )
3556
+ t . assert . deepStrictEqual ( await response . text ( ) , indexContent )
3557
+ genericResponseChecks ( t , response )
3558
+ } )
3559
+
3560
+ await t . test ( '/static/deep/path/for/test/purpose/foo.html' , async ( t ) => {
3561
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3562
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/deep/path/for/test/purpose/foo.html' )
3563
+
3564
+ t . assert . ok ( response . ok )
3565
+ t . assert . deepStrictEqual ( response . status , 200 )
3566
+ t . assert . deepStrictEqual ( await response . text ( ) , deepContent )
3567
+ genericResponseChecks ( t , response )
3568
+ } )
3569
+ await t . test ( '/static/deep/path/for/test/' , async ( t ) => {
3570
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3571
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/deep/path/for/test/' )
3572
+
3573
+ t . assert . ok ( response . ok )
3574
+ t . assert . deepStrictEqual ( response . status , 200 )
3575
+ t . assert . deepStrictEqual ( await response . text ( ) , innerIndex )
3576
+ genericResponseChecks ( t , response )
3577
+ } )
3578
+
3579
+ await t . test ( '/static/this/path/for/test' , async ( t ) => {
3580
+ t . plan ( 2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT )
3581
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/this/path/for/test' )
3582
+
3583
+ t . assert . ok ( ! response . ok )
3584
+ t . assert . deepStrictEqual ( response . status , 404 )
3585
+ genericErrorResponseChecks ( t , response )
3586
+ } )
3587
+
3588
+ await t . test ( '/static/this/path/doesnt/exist.html' , async ( t ) => {
3589
+ t . plan ( 2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT )
3590
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/this/path/doesnt/exist.html' )
3591
+
3592
+ t . assert . ok ( ! response . ok )
3593
+ t . assert . deepStrictEqual ( response . status , 404 )
3594
+ genericErrorResponseChecks ( t , response )
3595
+ } )
3596
+
3597
+ await t . test ( '304' , async t => {
3598
+ t . plan ( 5 + GENERIC_RESPONSE_CHECK_COUNT )
3599
+ const response = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html' )
3600
+
3601
+ t . assert . ok ( response . ok )
3602
+ t . assert . deepStrictEqual ( response . status , 200 )
3603
+ t . assert . deepStrictEqual ( await response . text ( ) , indexContent )
3604
+ genericResponseChecks ( t , response )
3605
+
3606
+ const response2 = await fetch ( 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html' , {
3607
+ headers : {
3608
+ 'if-none-match' : response . headers . get ( 'etag' )
3609
+ } ,
3610
+ cache : 'no-cache'
3611
+ } )
3612
+ t . assert . ok ( ! response2 . ok )
3613
+ t . assert . deepStrictEqual ( response2 . status , 304 )
3614
+ } )
3615
+ } )
0 commit comments