Skip to content

Commit ec74fcd

Browse files
committed
feat(bearer):bearer token method
1 parent 2ae1c36 commit ec74fcd

File tree

2 files changed

+115
-86
lines changed

2 files changed

+115
-86
lines changed

lib/test.js

Lines changed: 93 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const { Request } = require('superagent');
1414

1515
class Test extends Request {
1616
/**
17-
* Initialize a new `Test` with the given `app`,
18-
* request `method` and `path`.
19-
*
20-
* @param {Server} app
21-
* @param {String} method
22-
* @param {String} path
23-
* @api public
24-
*/
25-
constructor (app, method, path) {
17+
* Initialize a new `Test` with the given `app`,
18+
* request `method` and `path`.
19+
*
20+
* @param {Server} app
21+
* @param {String} method
22+
* @param {String} path
23+
* @api public
24+
*/
25+
constructor(app, method, path) {
2626
super(method.toUpperCase(), path);
2727

2828
this.redirects(0);
@@ -35,13 +35,13 @@ class Test extends Request {
3535
}
3636

3737
/**
38-
* Returns a URL, extracted from a server.
39-
*
40-
* @param {Server} app
41-
* @param {String} path
42-
* @returns {String} URL address
43-
* @api private
44-
*/
38+
* Returns a URL, extracted from a server.
39+
*
40+
* @param {Server} app
41+
* @param {String} path
42+
* @returns {String} URL address
43+
* @api private
44+
*/
4545
serverAddress(app, path) {
4646
const addr = app.address();
4747

@@ -52,22 +52,22 @@ class Test extends Request {
5252
}
5353

5454
/**
55-
* Expectations:
56-
*
57-
* .expect(200)
58-
* .expect(200, fn)
59-
* .expect(200, body)
60-
* .expect('Some body')
61-
* .expect('Some body', fn)
62-
* .expect(['json array body', { key: 'val' }])
63-
* .expect('Content-Type', 'application/json')
64-
* .expect('Content-Type', 'application/json', fn)
65-
* .expect(fn)
66-
* .expect([200, 404])
67-
*
68-
* @return {Test}
69-
* @api public
70-
*/
55+
* Expectations:
56+
*
57+
* .expect(200)
58+
* .expect(200, fn)
59+
* .expect(200, body)
60+
* .expect('Some body')
61+
* .expect('Some body', fn)
62+
* .expect(['json array body', { key: 'val' }])
63+
* .expect('Content-Type', 'application/json')
64+
* .expect('Content-Type', 'application/json', fn)
65+
* .expect(fn)
66+
* .expect([200, 404])
67+
*
68+
* @return {Test}
69+
* @api public
70+
*/
7171
expect(a, b, c) {
7272
// callback
7373
if (typeof a === 'function') {
@@ -106,12 +106,12 @@ class Test extends Request {
106106
}
107107

108108
/**
109-
* Defer invoking superagent's `.end()` until
110-
* the server is listening.
111-
*
112-
* @param {Function} fn
113-
* @api public
114-
*/
109+
* Defer invoking superagent's `.end()` until
110+
* the server is listening.
111+
*
112+
* @param {Function} fn
113+
* @api public
114+
*/
115115
end(fn) {
116116
const server = this._server;
117117

@@ -129,13 +129,13 @@ class Test extends Request {
129129
}
130130

131131
/**
132-
* Perform assertions and invoke `fn(err, res)`.
133-
*
134-
* @param {?Error} resError
135-
* @param {Response} res
136-
* @param {Function} fn
137-
* @api private
138-
*/
132+
* Perform assertions and invoke `fn(err, res)`.
133+
*
134+
* @param {?Error} resError
135+
* @param {Response} res
136+
* @param {Function} fn
137+
* @api private
138+
*/
139139
assert(resError, res, fn) {
140140
let errorObj;
141141

@@ -173,13 +173,25 @@ class Test extends Request {
173173
}
174174

175175
/**
176-
* Perform assertions on a response body and return an Error upon failure.
177-
*
178-
* @param {Mixed} body
179-
* @param {Response} res
180-
* @return {?Error}
181-
* @api private
182-
*/// eslint-disable-next-line class-methods-use-this
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+
187+
/**
188+
* Perform assertions on a response body and return an Error upon failure.
189+
*
190+
* @param {Mixed} body
191+
* @param {Response} res
192+
* @return {?Error}
193+
* @api private
194+
*/// eslint-disable-next-line class-methods-use-this
183195
_assertBody(body, res) {
184196
const isRegexp = body instanceof RegExp;
185197

@@ -209,13 +221,13 @@ class Test extends Request {
209221
}
210222

211223
/**
212-
* Perform assertions on a response header and return an Error upon failure.
213-
*
214-
* @param {Object} header
215-
* @param {Response} res
216-
* @return {?Error}
217-
* @api private
218-
*/// eslint-disable-next-line class-methods-use-this
224+
* Perform assertions on a response header and return an Error upon failure.
225+
*
226+
* @param {Object} header
227+
* @param {Response} res
228+
* @return {?Error}
229+
* @api private
230+
*/// eslint-disable-next-line class-methods-use-this
219231
_assertHeader(header, res) {
220232
const field = header.name;
221233
const actual = res.header[field.toLowerCase()];
@@ -238,13 +250,13 @@ class Test extends Request {
238250
}
239251

240252
/**
241-
* Perform assertions on the response status and return an Error upon failure.
242-
*
243-
* @param {Number} status
244-
* @param {Response} res
245-
* @return {?Error}
246-
* @api private
247-
*/// eslint-disable-next-line class-methods-use-this
253+
* Perform assertions on the response status and return an Error upon failure.
254+
*
255+
* @param {Number} status
256+
* @param {Response} res
257+
* @return {?Error}
258+
* @api private
259+
*/// eslint-disable-next-line class-methods-use-this
248260
_assertStatus(status, res) {
249261
if (res.status !== status) {
250262
const a = STATUS_CODES[status];
@@ -254,13 +266,13 @@ class Test extends Request {
254266
}
255267

256268
/**
257-
* Perform assertions on the response status and return an Error upon failure.
258-
*
259-
* @param {Array<Number>} statusArray
260-
* @param {Response} res
261-
* @return {?Error}
262-
* @api private
263-
*/// eslint-disable-next-line class-methods-use-this
269+
* Perform assertions on the response status and return an Error upon failure.
270+
*
271+
* @param {Array<Number>} statusArray
272+
* @param {Response} res
273+
* @return {?Error}
274+
* @api private
275+
*/// eslint-disable-next-line class-methods-use-this
264276
_assertStatusArray(statusArray, res) {
265277
if (!statusArray.includes(res.status)) {
266278
const b = STATUS_CODES[res.status];
@@ -272,13 +284,13 @@ class Test extends Request {
272284
}
273285

274286
/**
275-
* Performs an assertion by calling a function and return an Error upon failure.
276-
*
277-
* @param {Function} fn
278-
* @param {Response} res
279-
* @return {?Error}
280-
* @api private
281-
*/// eslint-disable-next-line class-methods-use-this
287+
* Performs an assertion by calling a function and return an Error upon failure.
288+
*
289+
* @param {Function} fn
290+
* @param {Response} res
291+
* @return {?Error}
292+
* @api private
293+
*/// eslint-disable-next-line class-methods-use-this
282294
_assertFunction(fn, res) {
283295
let err;
284296
try {
@@ -301,7 +313,7 @@ class Test extends Request {
301313
function wrapAssertFn(assertFn) {
302314
const savedStack = new Error().stack.split('\n').slice(3);
303315

304-
return function(res) {
316+
return function (res) {
305317
let badStack;
306318
let err;
307319
try {

test/supertest.js

Lines changed: 22 additions & 5 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();
@@ -818,7 +835,7 @@ describe('request(app)', function () {
818835

819836
it("doesn't create false negatives on non error objects", function (done) {
820837
const handler = {
821-
get: function(target, prop, receiver) {
838+
get: function (target, prop, receiver) {
822839
throw Error('Should not be called for non Error objects');
823840
}
824841
};
@@ -1367,7 +1384,7 @@ describe('request.get(url).query(vals) works as expected', function () {
13671384
});
13681385

13691386
const describeHttp2 = (http2) ? describe : describe.skip;
1370-
describeHttp2('http2', function() {
1387+
describeHttp2('http2', function () {
13711388
// eslint-disable-next-line global-require
13721389
const proxyquire = require('proxyquire');
13731390

@@ -1386,7 +1403,7 @@ describeHttp2('http2', function() {
13861403

13871404
tests.forEach(({ title, api, mockApi }) => {
13881405
describe(title, function () {
1389-
const app = function(req, res) {
1406+
const app = function (req, res) {
13901407
res.end('hey');
13911408
};
13921409

@@ -1416,8 +1433,8 @@ describeHttp2('http2', function() {
14161433
});
14171434
});
14181435

1419-
it('should throw error if http2 is not supported', function() {
1420-
(function() {
1436+
it('should throw error if http2 is not supported', function () {
1437+
(function () {
14211438
mockApi(app, { http2: true });
14221439
}).should.throw('supertest: this version of Node.js does not support http2');
14231440
});

0 commit comments

Comments
 (0)