From ab252f372c13128bcd1d8562d8ea395ef46d02c1 Mon Sep 17 00:00:00 2001 From: "Juan M. Villegas" Date: Mon, 12 Apr 2021 07:58:11 -0600 Subject: [PATCH] Allows expect to accept an array of statuses By accepting an array of expected values for status, expect can now assert a range of returned statuses codes. This is useful when we don't know the exact code but we expect a range: 200/204, redirects, etc. See: https://github.com/visionmedia/supertest/issues/389 --- lib/test.js | 26 ++++++++++++++++++++++++++ test/supertest.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/test.js b/lib/test.js index 6812255..983eb6d 100644 --- a/lib/test.js +++ b/lib/test.js @@ -101,6 +101,7 @@ function wrapAssertFn(assertFn) { * .expect('Content-Type', 'application/json') * .expect('Content-Type', 'application/json', fn) * .expect(fn) + * .expect([200, 404]) * * @return {Test} * @api public @@ -125,6 +126,12 @@ Test.prototype.expect = function(a, b, c) { return this; } + // multiple statuses + if (Array.isArray(a)) { + this._asserts.push(wrapAssertFn(this._assertStatusArray.bind(this, a))); + return this; + } + // header field if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) { this._asserts.push(wrapAssertFn(this._assertHeader.bind(this, { name: '' + a, value: b }))); @@ -297,6 +304,25 @@ Test.prototype._assertStatus = function(status, res) { } }; +/** + * Perform assertions on the response status and return an Error upon failure. + * + * @param {Array} statusArray + * @param {Response} res + * @return {?Error} + * @api private + */ + +Test.prototype._assertStatusArray = function(statusArray, res) { + var b; + var expectedList; + if (!statusArray.includes(res.status)) { + b = http.STATUS_CODES[res.status]; + expectedList = statusArray.join(', '); + return new Error('expected one of "' + expectedList + '", got ' + res.status + ' "' + b + '"'); + } +}; + /** * Performs an assertion by calling a function and return an Error upon failure. * diff --git a/test/supertest.js b/test/supertest.js index 5f43893..244b080 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -399,6 +399,38 @@ describe('request(app)', function () { }); }); + describe('.expect(statusArray)', function () { + it('should assert only status', function (done) { + const app = express(); + + app.get('/', function (req, res) { + res.send('hey'); + }); + + request(app) + .get('/') + .expect([200, 404]) + .end(done); + }); + + it('should reject if status is not in valid statuses array', function (done) { + const app = express(); + + app.get('/', function (req, res) { + res.send('hey'); + }); + + request(app) + .get('/') + .expect([500, 404]) + .end(function (err, res) { + err.message.should.equal('expected one of "500, 404", got 200 "OK"'); + shouldIncludeStackWithThisFile(err); + done(); + }); + }); + }); + describe('.expect(status, body[, fn])', function () { it('should assert the response body and status', function (done) { const app = express();