Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bearer):bearer token method #841

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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