@@ -4,6 +4,7 @@ const { test } = require('tap')
44const Fastify = require ( 'fastify' )
55const basicAuth = require ( './index' )
66const fastifyAuth = require ( 'fastify-auth' )
7+ const { Unauthorized } = require ( 'http-errors' )
78
89test ( 'Basic' , t => {
910 t . plan ( 2 )
@@ -384,7 +385,7 @@ test('Hook with fastify-auth- 401', t => {
384385} )
385386
386387test ( 'Missing header' , t => {
387- t . plan ( 2 )
388+ t . plan ( 3 )
388389
389390 const fastify = Fastify ( )
390391 fastify . register ( basicAuth , { validate } )
@@ -414,6 +415,11 @@ test('Missing header', t => {
414415 } , ( err , res ) => {
415416 t . error ( err )
416417 t . strictEqual ( res . statusCode , 401 )
418+ t . deepEqual ( JSON . parse ( res . payload ) , {
419+ statusCode : 401 ,
420+ error : 'Unauthorized' ,
421+ message : 'Missing or bad formatted authorization header'
422+ } )
417423 } )
418424} )
419425
@@ -456,6 +462,95 @@ test('Fastify context', t => {
456462 } )
457463} )
458464
465+ test ( 'setErrorHandler custom error and 401' , t => {
466+ t . plan ( 4 )
467+
468+ const fastify = Fastify ( )
469+ fastify
470+ . register ( fastifyAuth )
471+ . register ( basicAuth , { validate } )
472+
473+ function validate ( username , password , req , res , done ) {
474+ done ( new Error ( 'Winter is coming' ) )
475+ }
476+
477+ fastify . after ( ( ) => {
478+ fastify . addHook ( 'preHandler' , fastify . auth ( [ fastify . basicAuth ] ) )
479+ fastify . route ( {
480+ method : 'GET' ,
481+ url : '/' ,
482+ handler : ( req , reply ) => {
483+ reply . send ( { hello : 'world' } )
484+ }
485+ } )
486+ } )
487+
488+ fastify . setErrorHandler ( function ( err , req , reply ) {
489+ t . strictEqual ( err . statusCode , 401 )
490+ reply . send ( err )
491+ } )
492+
493+ fastify . inject ( {
494+ url : '/' ,
495+ method : 'GET' ,
496+ headers : {
497+ authorization : basicAuthHeader ( 'user' , 'pwdd' )
498+ }
499+ } , ( err , res ) => {
500+ t . error ( err )
501+ t . strictEqual ( res . statusCode , 401 )
502+ t . deepEqual ( JSON . parse ( res . payload ) , {
503+ error : 'Unauthorized' ,
504+ message : 'Winter is coming' ,
505+ statusCode : 401
506+ } )
507+ } )
508+ } )
509+
510+ test ( 'Missing header and custom error handler' , t => {
511+ t . plan ( 4 )
512+
513+ const fastify = Fastify ( )
514+ fastify . register ( basicAuth , { validate } )
515+
516+ function validate ( username , password , req , res , done ) {
517+ if ( username === 'user' && password === 'pwd' ) {
518+ done ( )
519+ } else {
520+ done ( new Error ( 'Unauthorized' ) )
521+ }
522+ }
523+
524+ fastify . after ( ( ) => {
525+ fastify . route ( {
526+ method : 'GET' ,
527+ url : '/' ,
528+ beforeHandler : fastify . basicAuth ,
529+ handler : ( req , reply ) => {
530+ reply . send ( { hello : 'world' } )
531+ }
532+ } )
533+ } )
534+
535+ fastify . setErrorHandler ( function ( err , req , reply ) {
536+ t . ok ( err instanceof Unauthorized )
537+ reply . send ( err )
538+ } )
539+
540+ fastify . inject ( {
541+ url : '/' ,
542+ method : 'GET'
543+ } , ( err , res ) => {
544+ t . error ( err )
545+ t . strictEqual ( res . statusCode , 401 )
546+ t . deepEqual ( JSON . parse ( res . payload ) , {
547+ statusCode : 401 ,
548+ error : 'Unauthorized' ,
549+ message : 'Missing or bad formatted authorization header'
550+ } )
551+ } )
552+ } )
553+
459554function basicAuthHeader ( username , password ) {
460555 return 'Basic ' + Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' )
461556}
0 commit comments