Skip to content

Commit

Permalink
feat(bearer):bearer auth token method
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusamc committed Aug 4, 2024
1 parent 2ae1c36 commit 411f7ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 411f7ce

Please sign in to comment.