From 411f7cec14b00c5b9460dbe3b3e3d3ce789fb643 Mon Sep 17 00:00:00 2001 From: Vinicius Date: Sun, 4 Aug 2024 00:04:14 -0300 Subject: [PATCH] feat(bearer):bearer auth token method --- lib/test.js | 12 ++++++++++++ test/supertest.js | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/test.js b/lib/test.js index 3b3b255..e764fe3 100644 --- a/lib/test.js +++ b/lib/test.js @@ -172,6 +172,18 @@ class Test extends Request { fn.call(this, errorObj || null, res); } + /* + * Adds a set Authorization Bearer + * + * @param {Bearer} Bearer Token + * Shortcut for .set('Authorization', `Bearer ${token}`) + */ + + bearer(token) { + this.set('Authorization', `Bearer ${token}`); + return this; + } + /** * Perform assertions on a response body and return an Error upon failure. * diff --git a/test/supertest.js b/test/supertest.js index 9eebf31..49be950 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -216,6 +216,23 @@ describe('request(app)', function () { }); }); + describe('.bearer(token)', function () { + it('should work the bearer token', function () { + const app = express(); + const test = request(app); + + app.get('/', function (req, res) { + if (req.headers.authorization === 'Bearer test-token') { + res.status(200).send('Authorized'); + } else { + res.status(403).send('Unauthorized'); + } + }); + + test.get('/').bearer('test-token').expect(200).expect('Authoried'); + }); + }); + describe('.end(fn)', function () { it('should close server', function (done) { const app = express();