@@ -3493,8 +3493,8 @@ test('respect the .type when using with sendFile with contentType disabled', asy
3493
3493
t . assert . deepStrictEqual ( await response . text ( ) , fooContent )
3494
3494
} )
3495
3495
3496
- t . test ( 'register /static/ with custom log level' , t => {
3497
- t . plan ( 11 )
3496
+ test ( 'register /static/ with custom log level' , async t => {
3497
+ t . plan ( 9 )
3498
3498
3499
3499
const pluginOptions = {
3500
3500
root : path . join ( __dirname , '/static' ) ,
@@ -3505,163 +3505,111 @@ t.test('register /static/ with custom log level', t => {
3505
3505
logger : {
3506
3506
stream : {
3507
3507
write : ( logLine ) => {
3508
- const log = JSON . parse ( logLine )
3509
-
3510
- if ( log . reqId ) {
3511
- t . fail ( 'Not expecting any log since plugin\'s log level is set at WARN' )
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 )
3512
3511
}
3513
3512
} ,
3514
3513
} ,
3515
3514
} ,
3516
3515
} )
3517
3516
fastify . register ( fastifyStatic , pluginOptions )
3518
3517
3519
- t . teardown ( fastify . close . bind ( fastify ) )
3518
+ t . after ( ( ) => fastify . close ( ) )
3520
3519
3521
- fastify . listen ( { port : 0 } , ( err ) => {
3522
- t . error ( err )
3520
+ await fastify . listen ( { port : 0 } )
3521
+ fastify . server . unref ( )
3523
3522
3524
- fastify . server . unref ( )
3523
+ await t . test ( '/static/index.html' , async ( t ) => {
3524
+ t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3525
3525
3526
- t . test ( '/static/index.html' , ( t ) => {
3527
- t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3528
- simple . concat ( {
3529
- method : 'GET' ,
3530
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html'
3531
- } , ( err , response , body ) => {
3532
- t . error ( err )
3533
- t . equal ( response . statusCode , 200 )
3534
- t . equal ( body . toString ( ) , indexContent )
3535
- genericResponseChecks ( t , response )
3536
- } )
3537
- } )
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
+ } )
3538
3532
3539
- t . test ( '/static/index.html' , t => {
3540
- t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3541
- simple . concat ( {
3542
- method : 'HEAD' ,
3543
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html'
3544
- } , ( err , response , body ) => {
3545
- t . error ( err )
3546
- t . equal ( response . statusCode , 200 )
3547
- t . equal ( body . toString ( ) , '' )
3548
- genericResponseChecks ( t , response )
3549
- } )
3550
- } )
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
+ } )
3551
3541
3552
- t . test ( '/static/index.css' , ( t ) => {
3553
- t . plan ( 2 + GENERIC_RESPONSE_CHECK_COUNT )
3554
- simple . concat ( {
3555
- method : 'GET' ,
3556
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.css'
3557
- } , ( err , response , body ) => {
3558
- t . error ( err )
3559
- t . equal ( response . statusCode , 200 )
3560
- genericResponseChecks ( t , response )
3561
- } )
3562
- } )
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
+ } )
3563
3549
3564
- t . test ( '/static/' , ( t ) => {
3565
- t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3566
- simple . concat ( {
3567
- method : 'GET' ,
3568
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/'
3569
- } , ( err , response , body ) => {
3570
- t . error ( err )
3571
- t . equal ( response . statusCode , 200 )
3572
- t . equal ( body . toString ( ) , indexContent )
3573
- genericResponseChecks ( t , response )
3574
- } )
3575
- } )
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/' )
3576
3553
3577
- t . test ( '/static/deep/path/for/test/purpose/foo.html' , ( t ) => {
3578
- t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3579
- simple . concat ( {
3580
- method : 'GET' ,
3581
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/deep/path/for/test/purpose/foo.html'
3582
- } , ( err , response , body ) => {
3583
- t . error ( err )
3584
- t . equal ( response . statusCode , 200 )
3585
- t . equal ( body . toString ( ) , deepContent )
3586
- genericResponseChecks ( t , response )
3587
- } )
3588
- } )
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
+ } )
3589
3559
3590
- t . test ( '/static/deep/path/for/test/' , ( t ) => {
3591
- t . plan ( 3 + GENERIC_RESPONSE_CHECK_COUNT )
3592
- simple . concat ( {
3593
- method : 'GET' ,
3594
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/deep/path/for/test/'
3595
- } , ( err , response , body ) => {
3596
- t . error ( err )
3597
- t . equal ( response . statusCode , 200 )
3598
- t . equal ( body . toString ( ) , innerIndex )
3599
- genericResponseChecks ( t , response )
3600
- } )
3601
- } )
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' )
3602
3563
3603
- t . test ( '/static/this/path/for/test' , ( t ) => {
3604
- t . plan ( 2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT )
3605
- simple . concat ( {
3606
- method : 'GET' ,
3607
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/this/path/for/test' ,
3608
- followRedirect : false
3609
- } , ( err , response , body ) => {
3610
- t . error ( err )
3611
- t . equal ( response . statusCode , 404 )
3612
- genericErrorResponseChecks ( t , response )
3613
- } )
3614
- } )
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/' )
3615
3572
3616
- t . test ( '/static/this/path/doesnt/exist.html' , ( t ) => {
3617
- t . plan ( 2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT )
3618
- simple . concat ( {
3619
- method : 'GET' ,
3620
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/this/path/doesnt/exist.html' ,
3621
- followRedirect : false
3622
- } , ( err , response , body ) => {
3623
- t . error ( err )
3624
- t . equal ( response . statusCode , 404 )
3625
- genericErrorResponseChecks ( t , response )
3626
- } )
3627
- } )
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
+ } )
3628
3578
3629
- t . test ( '/static/../index.js' , ( t ) => {
3630
- t . plan ( 2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT )
3631
- simple . concat ( {
3632
- method : 'GET' ,
3633
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/../index.js' ,
3634
- followRedirect : false
3635
- } , ( err , response , body ) => {
3636
- t . error ( err )
3637
- t . equal ( response . statusCode , 403 )
3638
- genericErrorResponseChecks ( t , response )
3639
- } )
3640
- } )
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' )
3641
3582
3642
- t . test ( '304' , t => {
3643
- t . plan ( 5 + GENERIC_RESPONSE_CHECK_COUNT )
3644
- simple . concat ( {
3645
- method : 'GET' ,
3646
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html'
3647
- } , ( err , response , body ) => {
3648
- t . error ( err )
3649
- const etag = response . headers . etag
3650
- t . equal ( response . statusCode , 200 )
3651
- t . equal ( body . toString ( ) , indexContent )
3652
- genericResponseChecks ( t , response )
3653
-
3654
- simple . concat ( {
3655
- method : 'GET' ,
3656
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/static/index.html' ,
3657
- headers : {
3658
- 'if-none-match' : etag
3659
- }
3660
- } , ( err , response , body ) => {
3661
- t . error ( err )
3662
- t . equal ( response . statusCode , 304 )
3663
- } )
3664
- } )
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'
3665
3611
} )
3612
+ t . assert . ok ( ! response2 . ok )
3613
+ t . assert . deepStrictEqual ( response2 . status , 304 )
3666
3614
} )
3667
3615
} )
0 commit comments