Skip to content

Commit

Permalink
feat(bearer):bearer token method
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusamc committed Jun 3, 2024
1 parent 2ae1c36 commit ec74fcd
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 86 deletions.
174 changes: 93 additions & 81 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const { Request } = require('superagent');

class Test extends Request {
/**
* Initialize a new `Test` with the given `app`,
* request `method` and `path`.
*
* @param {Server} app
* @param {String} method
* @param {String} path
* @api public
*/
constructor (app, method, path) {
* Initialize a new `Test` with the given `app`,
* request `method` and `path`.
*
* @param {Server} app
* @param {String} method
* @param {String} path
* @api public
*/
constructor(app, method, path) {
super(method.toUpperCase(), path);

this.redirects(0);
Expand All @@ -35,13 +35,13 @@ class Test extends Request {
}

/**
* Returns a URL, extracted from a server.
*
* @param {Server} app
* @param {String} path
* @returns {String} URL address
* @api private
*/
* Returns a URL, extracted from a server.
*
* @param {Server} app
* @param {String} path
* @returns {String} URL address
* @api private
*/
serverAddress(app, path) {
const addr = app.address();

Expand All @@ -52,22 +52,22 @@ class Test extends Request {
}

/**
* Expectations:
*
* .expect(200)
* .expect(200, fn)
* .expect(200, body)
* .expect('Some body')
* .expect('Some body', fn)
* .expect(['json array body', { key: 'val' }])
* .expect('Content-Type', 'application/json')
* .expect('Content-Type', 'application/json', fn)
* .expect(fn)
* .expect([200, 404])
*
* @return {Test}
* @api public
*/
* Expectations:
*
* .expect(200)
* .expect(200, fn)
* .expect(200, body)
* .expect('Some body')
* .expect('Some body', fn)
* .expect(['json array body', { key: 'val' }])
* .expect('Content-Type', 'application/json')
* .expect('Content-Type', 'application/json', fn)
* .expect(fn)
* .expect([200, 404])
*
* @return {Test}
* @api public
*/
expect(a, b, c) {
// callback
if (typeof a === 'function') {
Expand Down Expand Up @@ -106,12 +106,12 @@ class Test extends Request {
}

/**
* Defer invoking superagent's `.end()` until
* the server is listening.
*
* @param {Function} fn
* @api public
*/
* Defer invoking superagent's `.end()` until
* the server is listening.
*
* @param {Function} fn
* @api public
*/
end(fn) {
const server = this._server;

Expand All @@ -129,13 +129,13 @@ class Test extends Request {
}

/**
* Perform assertions and invoke `fn(err, res)`.
*
* @param {?Error} resError
* @param {Response} res
* @param {Function} fn
* @api private
*/
* Perform assertions and invoke `fn(err, res)`.
*
* @param {?Error} resError
* @param {Response} res
* @param {Function} fn
* @api private
*/
assert(resError, res, fn) {
let errorObj;

Expand Down Expand Up @@ -173,13 +173,25 @@ class Test extends Request {
}

/**
* Perform assertions on a response body and return an Error upon failure.
*
* @param {Mixed} body
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
* 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.
*
* @param {Mixed} body
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
_assertBody(body, res) {
const isRegexp = body instanceof RegExp;

Expand Down Expand Up @@ -209,13 +221,13 @@ class Test extends Request {
}

/**
* Perform assertions on a response header and return an Error upon failure.
*
* @param {Object} header
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
* Perform assertions on a response header and return an Error upon failure.
*
* @param {Object} header
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
_assertHeader(header, res) {
const field = header.name;
const actual = res.header[field.toLowerCase()];
Expand All @@ -238,13 +250,13 @@ class Test extends Request {
}

/**
* Perform assertions on the response status and return an Error upon failure.
*
* @param {Number} status
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
* Perform assertions on the response status and return an Error upon failure.
*
* @param {Number} status
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
_assertStatus(status, res) {
if (res.status !== status) {
const a = STATUS_CODES[status];
Expand All @@ -254,13 +266,13 @@ class Test extends Request {
}

/**
* Perform assertions on the response status and return an Error upon failure.
*
* @param {Array<Number>} statusArray
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
* Perform assertions on the response status and return an Error upon failure.
*
* @param {Array<Number>} statusArray
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
_assertStatusArray(statusArray, res) {
if (!statusArray.includes(res.status)) {
const b = STATUS_CODES[res.status];
Expand All @@ -272,13 +284,13 @@ class Test extends Request {
}

/**
* Performs an assertion by calling a function and return an Error upon failure.
*
* @param {Function} fn
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
* Performs an assertion by calling a function and return an Error upon failure.
*
* @param {Function} fn
* @param {Response} res
* @return {?Error}
* @api private
*/// eslint-disable-next-line class-methods-use-this
_assertFunction(fn, res) {
let err;
try {
Expand All @@ -301,7 +313,7 @@ class Test extends Request {
function wrapAssertFn(assertFn) {
const savedStack = new Error().stack.split('\n').slice(3);

return function(res) {
return function (res) {
let badStack;
let err;
try {
Expand Down
27 changes: 22 additions & 5 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 Expand Up @@ -818,7 +835,7 @@ describe('request(app)', function () {

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

const describeHttp2 = (http2) ? describe : describe.skip;
describeHttp2('http2', function() {
describeHttp2('http2', function () {
// eslint-disable-next-line global-require
const proxyquire = require('proxyquire');

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

tests.forEach(({ title, api, mockApi }) => {
describe(title, function () {
const app = function(req, res) {
const app = function (req, res) {
res.end('hey');
};

Expand Down Expand Up @@ -1416,8 +1433,8 @@ describeHttp2('http2', function() {
});
});

it('should throw error if http2 is not supported', function() {
(function() {
it('should throw error if http2 is not supported', function () {
(function () {
mockApi(app, { http2: true });
}).should.throw('supertest: this version of Node.js does not support http2');
});
Expand Down

0 comments on commit ec74fcd

Please sign in to comment.