@@ -236,8 +236,8 @@ test('WWW-Authenticate Realm (authenticate: {realm: "example"})', t => {
236236 authorization : basicAuthHeader ( 'user' , 'pwd' )
237237 }
238238 } , ( err , res ) => {
239- t . equal ( res . headers [ 'www-authenticate' ] , 'Basic realm="example"' )
240239 t . error ( err )
240+ t . equal ( res . headers [ 'www-authenticate' ] , 'Basic realm="example"' )
241241 t . equal ( res . statusCode , 200 )
242242 } )
243243} )
@@ -571,6 +571,26 @@ test('Invalid options (authenticate)', t => {
571571 } )
572572} )
573573
574+ test ( 'Invalid options (realm is a number)' , t => {
575+ t . plan ( 1 )
576+
577+ const fastify = Fastify ( )
578+ fastify
579+ . register ( basicAuth , { validate, authenticate : { realm : 42 } } )
580+
581+ function validate ( username , password , req , res , done ) {
582+ if ( username === 'user' && password === 'pwd' ) {
583+ done ( )
584+ } else {
585+ done ( new Error ( 'Unauthorized' ) )
586+ }
587+ }
588+
589+ fastify . ready ( function ( err ) {
590+ t . equal ( err . message , 'Basic Auth: Invalid authenticate option' )
591+ } )
592+ } )
593+
574594test ( 'Invalid options (authenticate realm)' , t => {
575595 t . plan ( 3 )
576596
@@ -604,8 +624,129 @@ test('Invalid options (authenticate realm)', t => {
604624 authorization : basicAuthHeader ( 'user' , 'pwd' )
605625 }
606626 } , ( err , res ) => {
627+ t . error ( err )
607628 t . equal ( res . headers [ 'www-authenticate' ] , 'Basic' )
629+ t . equal ( res . statusCode , 200 )
630+ } )
631+ } )
632+
633+ test ( 'Invalid options (authenticate realm = undefined)' , t => {
634+ t . plan ( 3 )
635+
636+ const fastify = Fastify ( )
637+ fastify
638+ . register ( basicAuth , { validate, authenticate : { realm : undefined } } )
639+
640+ function validate ( username , password , req , res , done ) {
641+ if ( username === 'user' && password === 'pwd' ) {
642+ done ( )
643+ } else {
644+ done ( new Error ( 'Unauthorized' ) )
645+ }
646+ }
647+
648+ fastify . after ( ( ) => {
649+ fastify . route ( {
650+ method : 'GET' ,
651+ url : '/' ,
652+ preHandler : fastify . basicAuth ,
653+ handler : ( req , reply ) => {
654+ reply . send ( { hello : 'world' } )
655+ }
656+ } )
657+ } )
658+
659+ fastify . inject ( {
660+ url : '/' ,
661+ method : 'GET' ,
662+ headers : {
663+ authorization : basicAuthHeader ( 'user' , 'pwd' )
664+ }
665+ } , ( err , res ) => {
666+ t . error ( err )
667+ t . equal ( res . headers [ 'www-authenticate' ] , 'Basic' )
668+ t . equal ( res . statusCode , 200 )
669+ } )
670+ } )
671+
672+ test ( 'WWW-Authenticate Realm dynamic realm' , t => {
673+ t . plan ( 3 )
674+
675+ const fastify = Fastify ( )
676+ const authenticate = {
677+ realm : true
678+ }
679+ fastify . register ( basicAuth , { validate, authenticate } )
680+
681+ function validate ( username , password , req , res , done ) {
682+ if ( username === 'user' && password === 'pwd' ) {
683+ done ( null , 'root' )
684+ } else {
685+ done ( new Error ( 'Unauthorized' ) )
686+ }
687+ }
688+
689+ fastify . after ( ( ) => {
690+ fastify . route ( {
691+ method : 'GET' ,
692+ url : '/' ,
693+ preHandler : fastify . basicAuth ,
694+ handler : ( req , reply ) => {
695+ reply . send ( { hello : 'world' } )
696+ }
697+ } )
698+ } )
699+
700+ fastify . inject ( {
701+ url : '/' ,
702+ method : 'GET' ,
703+ headers : {
704+ authorization : basicAuthHeader ( 'user' , 'pwd' )
705+ }
706+ } , ( err , res ) => {
707+ t . error ( err )
708+ t . equal ( res . headers [ 'www-authenticate' ] , 'Basic realm="root"' )
709+ t . equal ( res . statusCode , 200 )
710+ } )
711+ } )
712+
713+ test ( 'WWW-Authenticate Realm dynamic realm promise' , t => {
714+ t . plan ( 3 )
715+
716+ const fastify = Fastify ( )
717+ const authenticate = {
718+ realm : true
719+ }
720+ fastify . register ( basicAuth , { validate, authenticate } )
721+
722+ function validate ( username , password , req , res ) {
723+ if ( username === 'user' && password === 'pwd' ) {
724+ return Promise . resolve ( 'root' )
725+ } else {
726+ return Promise . reject ( new Error ( 'Unauthorized' ) )
727+ }
728+ }
729+
730+ fastify . after ( ( ) => {
731+ fastify . route ( {
732+ method : 'GET' ,
733+ url : '/' ,
734+ preHandler : fastify . basicAuth ,
735+ handler : ( req , reply ) => {
736+ reply . send ( { hello : 'world' } )
737+ }
738+ } )
739+ } )
740+
741+ fastify . inject ( {
742+ url : '/' ,
743+ method : 'GET' ,
744+ headers : {
745+ authorization : basicAuthHeader ( 'user' , 'pwd' )
746+ }
747+ } , ( err , res ) => {
608748 t . error ( err )
749+ t . equal ( res . headers [ 'www-authenticate' ] , 'Basic realm="root"' )
609750 t . equal ( res . statusCode , 200 )
610751 } )
611752} )
0 commit comments