Skip to content

Commit 411f7ce

Browse files
committed
feat(bearer):bearer auth token method
1 parent 2ae1c36 commit 411f7ce

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ class Test extends Request {
172172
fn.call(this, errorObj || null, res);
173173
}
174174

175+
/*
176+
* Adds a set Authorization Bearer
177+
*
178+
* @param {Bearer} Bearer Token
179+
* Shortcut for .set('Authorization', `Bearer ${token}`)
180+
*/
181+
182+
bearer(token) {
183+
this.set('Authorization', `Bearer ${token}`);
184+
return this;
185+
}
186+
175187
/**
176188
* Perform assertions on a response body and return an Error upon failure.
177189
*

test/supertest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ describe('request(app)', function () {
216216
});
217217
});
218218

219+
describe('.bearer(token)', function () {
220+
it('should work the bearer token', function () {
221+
const app = express();
222+
const test = request(app);
223+
224+
app.get('/', function (req, res) {
225+
if (req.headers.authorization === 'Bearer test-token') {
226+
res.status(200).send('Authorized');
227+
} else {
228+
res.status(403).send('Unauthorized');
229+
}
230+
});
231+
232+
test.get('/').bearer('test-token').expect(200).expect('Authoried');
233+
});
234+
});
235+
219236
describe('.end(fn)', function () {
220237
it('should close server', function (done) {
221238
const app = express();

0 commit comments

Comments
 (0)