@@ -589,6 +589,102 @@ test('WWW-Authenticate Realm (authenticate: {realm: "example"}, utf8: true)', as
589589 t . assert . strictEqual ( res2 . statusCode , 200 )
590590} )
591591
592+ test ( 'WWW-Authenticate Custom Header (authenticate: {realm: "example", header: "x-custom-authenticate" }, utf8: false)' , async t => {
593+ t . plan ( 8 )
594+
595+ const fastify = Fastify ( )
596+ const authenticate = { realm : 'example' , header : 'x-custom-authenticate' }
597+ fastify . register ( basicAuth , { validate, authenticate, utf8 : false } )
598+
599+ function validate ( username , password , _req , _res , done ) {
600+ if ( username === 'user' && password === 'pwd' ) {
601+ done ( )
602+ } else {
603+ done ( new Error ( 'Unauthorized' ) )
604+ }
605+ }
606+
607+ fastify . after ( ( ) => {
608+ fastify . route ( {
609+ method : 'GET' ,
610+ url : '/' ,
611+ preHandler : fastify . basicAuth ,
612+ handler : ( _req , reply ) => {
613+ reply . send ( { hello : 'world' } )
614+ }
615+ } )
616+ } )
617+
618+ const res1 = await fastify . inject ( {
619+ url : '/' ,
620+ method : 'GET'
621+ } )
622+ t . assert . ok ( res1 . body )
623+ t . assert . strictEqual ( res1 . headers [ 'x-custom-authenticate' ] , 'Basic realm="example"' )
624+ t . assert . strictEqual ( res1 . headers [ 'www-authenticate' ] , undefined )
625+ t . assert . strictEqual ( res1 . statusCode , 401 )
626+
627+ const res2 = await fastify . inject ( {
628+ url : '/' ,
629+ method : 'GET' ,
630+ headers : {
631+ authorization : basicAuthHeader ( 'user' , 'pwd' )
632+ }
633+ } )
634+ t . assert . ok ( res2 . body )
635+ t . assert . strictEqual ( res2 . headers [ 'x-custom-authenticate' ] , undefined )
636+ t . assert . strictEqual ( res2 . headers [ 'www-authenticate' ] , undefined )
637+ t . assert . strictEqual ( res2 . statusCode , 200 )
638+ } )
639+
640+ test ( 'WWW-Authenticate Custom Header (authenticate: {realm: "example", header: "x-custom-authenticate" }, utf8: true)' , async t => {
641+ t . plan ( 8 )
642+
643+ const fastify = Fastify ( )
644+ const authenticate = { realm : 'example' , header : 'x-custom-authenticate' }
645+ fastify . register ( basicAuth , { validate, authenticate, utf8 : true } )
646+
647+ function validate ( username , password , _req , _res , done ) {
648+ if ( username === 'user' && password === 'pwd' ) {
649+ done ( )
650+ } else {
651+ done ( new Error ( 'Unauthorized' ) )
652+ }
653+ }
654+
655+ fastify . after ( ( ) => {
656+ fastify . route ( {
657+ method : 'GET' ,
658+ url : '/' ,
659+ preHandler : fastify . basicAuth ,
660+ handler : ( _req , reply ) => {
661+ reply . send ( { hello : 'world' } )
662+ }
663+ } )
664+ } )
665+
666+ const res1 = await fastify . inject ( {
667+ url : '/' ,
668+ method : 'GET'
669+ } )
670+ t . assert . ok ( res1 . body )
671+ t . assert . strictEqual ( res1 . headers [ 'x-custom-authenticate' ] , 'Basic realm="example", charset="UTF-8"' )
672+ t . assert . strictEqual ( res1 . headers [ 'www-authenticate' ] , undefined )
673+ t . assert . strictEqual ( res1 . statusCode , 401 )
674+
675+ const res2 = await fastify . inject ( {
676+ url : '/' ,
677+ method : 'GET' ,
678+ headers : {
679+ authorization : basicAuthHeader ( 'user' , 'pwd' )
680+ }
681+ } )
682+ t . assert . ok ( res2 . body )
683+ t . assert . strictEqual ( res2 . headers [ 'x-custom-authenticate' ] , undefined )
684+ t . assert . strictEqual ( res2 . headers [ 'www-authenticate' ] , undefined )
685+ t . assert . strictEqual ( res2 . statusCode , 200 )
686+ } )
687+
592688test ( 'Header option specified' , async t => {
593689 t . plan ( 2 )
594690
0 commit comments