From ab247f81867e44961a72496cd22064d4712220c7 Mon Sep 17 00:00:00 2001 From: Phillip9587 Date: Wed, 12 Feb 2025 20:44:15 +0100 Subject: [PATCH] feat: add support for fetch API Request interface --- HISTORY.md | 5 +++++ index.js | 7 ++++++- test/basic-auth.js | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 2c44a01..7e5f842 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +================== + + * feat: add support for fetch API `Request` interface + 2.0.1 / 2018-09-19 ================== diff --git a/index.js b/index.js index 9106e64..c84280d 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,12 @@ function getAuthorization (req) { throw new TypeError('argument req is required to have headers property') } - return req.headers.authorization + if (req.headers.authorization) { + return req.headers.authorization + } else if (typeof req.headers.get === 'function') { + return req.headers.get('authorization') + } + return undefined } /** diff --git a/test/basic-auth.js b/test/basic-auth.js index 96877f3..2b7d89e 100644 --- a/test/basic-auth.js +++ b/test/basic-auth.js @@ -9,6 +9,16 @@ function request (authorization) { } } +function fetchRequest (authorization) { + return { + headers: { + get: function (name) { + return name === 'authorization' ? authorization : undefined + } + } + } +} + describe('auth(req)', function () { describe('arguments', function () { describe('req', function () { @@ -74,6 +84,15 @@ describe('auth(req)', function () { }) }) + describe('with valid credentials [fetch API Request]', function () { + it('should return .name and .pass', function () { + var req = fetchRequest('basic Zm9vOmJhcg==') + var creds = auth(req) + assert.strictEqual(creds.name, 'foo') + assert.strictEqual(creds.pass, 'bar') + }) + }) + describe('with empty password', function () { it('should return .name and .pass', function () { var req = request('basic Zm9vOg==')