diff --git a/index.js b/index.js index b7b8bab..b48a33d 100644 --- a/index.js +++ b/index.js @@ -130,7 +130,10 @@ async function fastifyBasicAuth (fastify, opts) { } if (err.statusCode === 401) { - reply.header('WWW-Authenticate', authenticateHeader(req)) + const header = authenticateHeader(req) + if (header) { + reply.header('WWW-Authenticate', header) + } } next(err) } else { diff --git a/test/index.test.js b/test/index.test.js index be4966e..852dd11 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -462,6 +462,54 @@ test('WWW-Authenticate (authenticate: true)', t => { }) }) +test('WWW-Authenticate (authenticate: false)', t => { + t.plan(6) + + const fastify = Fastify() + const authenticate = false + fastify.register(basicAuth, { validate, authenticate, utf8: false }) + + function validate (username, password, req, res, done) { + if (username === 'user' && password === 'pwd') { + done() + } else { + done(new Error('Unauthorized')) + } + } + + fastify.after(() => { + fastify.route({ + method: 'GET', + url: '/', + preHandler: fastify.basicAuth, + handler: (req, reply) => { + reply.send({ hello: 'world' }) + } + }) + }) + + fastify.inject({ + url: '/', + method: 'GET' + }, (err, res) => { + t.error(err) + t.equal(res.headers['www-authenticate'], undefined) + t.equal(res.statusCode, 401) + }) + + fastify.inject({ + url: '/', + method: 'GET', + headers: { + authorization: basicAuthHeader('user', 'pwd') + } + }, (err, res) => { + t.error(err) + t.equal(res.headers['www-authenticate'], undefined) + t.equal(res.statusCode, 200) + }) +}) + test('WWW-Authenticate Realm (authenticate: {realm: "example"}, utf8: false)', t => { t.plan(6)