Skip to content

Commit e2be0e1

Browse files
authored
Merge pull request #841 from viniciusamc/master
feat(bearer):bearer token method
2 parents 200031e + 411f7ce commit e2be0e1

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
@@ -187,6 +187,18 @@ class Test extends Request {
187187
fn.call(this, errorObj || null, res);
188188
}
189189

190+
/*
191+
* Adds a set Authorization Bearer
192+
*
193+
* @param {Bearer} Bearer Token
194+
* Shortcut for .set('Authorization', `Bearer ${token}`)
195+
*/
196+
197+
bearer(token) {
198+
this.set('Authorization', `Bearer ${token}`);
199+
return this;
200+
}
201+
190202
/**
191203
* Perform assertions on a response body and return an Error upon failure.
192204
*

test/supertest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,23 @@ describe('request(app)', function () {
233233
});
234234
});
235235

236+
describe('.bearer(token)', function () {
237+
it('should work the bearer token', function () {
238+
const app = express();
239+
const test = request(app);
240+
241+
app.get('/', function (req, res) {
242+
if (req.headers.authorization === 'Bearer test-token') {
243+
res.status(200).send('Authorized');
244+
} else {
245+
res.status(403).send('Unauthorized');
246+
}
247+
});
248+
249+
test.get('/').bearer('test-token').expect(200).expect('Authoried');
250+
});
251+
});
252+
236253
describe('.end(fn)', function () {
237254
it('should close server', function (done) {
238255
const app = express();

0 commit comments

Comments
 (0)