From 739bc4713409e1647bbff94b2c2ae104b0c9a927 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 7 Mar 2020 16:46:30 -0500 Subject: [PATCH 01/29] check status code is integer, or string integer, in range --- lib/response.js | 13 +++++++++---- test/res.status.js | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/response.js b/lib/response.js index 61ab75d247..b5a1042c9f 100644 --- a/lib/response.js +++ b/lib/response.js @@ -62,7 +62,12 @@ var charsetRegExp = /;\s*charset\s*=/; */ res.status = function status(code) { - this.statusCode = code; + // check that status code is acceptable by Node.js http module + if (!Number.isInteger(+code) || code < 100 || code > 999) { + throw new TypeError('Invalid status code'); + } + + this.statusCode = code return this; }; @@ -167,7 +172,7 @@ res.send = function send(body) { } // freshness - if (req.fresh) this.statusCode = 304; + if (req.fresh) this.status(304); // strip irrelevant headers if (204 === this.statusCode || 304 === this.statusCode) { @@ -286,7 +291,7 @@ res.jsonp = function jsonp(obj) { res.sendStatus = function sendStatus(statusCode) { var body = statuses[statusCode] || String(statusCode) - this.statusCode = statusCode; + this.status(statusCode); this.type('txt'); return this.send(body); @@ -768,7 +773,7 @@ res.redirect = function redirect(url) { }); // Respond - this.statusCode = status; + this.status(status); this.set('Content-Length', Buffer.byteLength(body)); if (this.req.method === 'HEAD') { diff --git a/test/res.status.js b/test/res.status.js index 8c173a645c..4057728e99 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -1,6 +1,8 @@ var express = require('../') - , request = require('supertest'); + , res = require('../lib/response') + , request = require('supertest') + , assert = require('assert'); describe('res', function(){ describe('.status(code)', function(){ @@ -16,5 +18,42 @@ describe('res', function(){ .expect('Created') .expect(201, done); }) + + describe('should throw', function() { + var InvalidStatusError = new TypeError('Invalid status code') + + it('if status code is < 100 || > 999', function(done) { + assert.throws(function() { + res.status(99) + }, InvalidStatusError, "for value 99") + assert.throws(function() { + res.status(1000) + }, InvalidStatusError, "for value 1000") + done() + }) + it('if status code is not an integer', function (done) { + var cases = [ + 200.1, + '200.1', + NaN, + Infinity, + -Infinity, + undefined, + null, + function () {}, + true, + false, + {}, + [], + ] + cases.forEach(function (item) { + assert.throws(function () { + res.status(item) + }, InvalidStatusError) + }) + done() + }) + + }) }) }) From 1847262849eb2e4b803ddd3b7641cb1fa9a251ea Mon Sep 17 00:00:00 2001 From: Jon Church Date: Tue, 10 Mar 2020 07:40:53 -0400 Subject: [PATCH 02/29] fix tests, update jsdoc comment for res.status --- lib/response.js | 4 +- test/res.status.js | 113 +++++++++++++++++++++++++++++++-------------- 2 files changed, 81 insertions(+), 36 deletions(-) diff --git a/lib/response.js b/lib/response.js index b5a1042c9f..3e8d0c7089 100644 --- a/lib/response.js +++ b/lib/response.js @@ -56,13 +56,13 @@ var charsetRegExp = /;\s*charset\s*=/; /** * Set status `code`. * - * @param {Number} code + * @param {Number|String} code * @return {ServerResponse} * @public */ res.status = function status(code) { - // check that status code is acceptable by Node.js http module + // check that status code is an integer within valid range if (!Number.isInteger(+code) || code < 100 || code > 999) { throw new TypeError('Invalid status code'); } diff --git a/test/res.status.js b/test/res.status.js index 4057728e99..76e257952e 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -1,8 +1,6 @@ var express = require('../') - , res = require('../lib/response') , request = require('supertest') - , assert = require('assert'); describe('res', function(){ describe('.status(code)', function(){ @@ -19,41 +17,88 @@ describe('res', function(){ .expect(201, done); }) - describe('should throw', function() { - var InvalidStatusError = new TypeError('Invalid status code') - - it('if status code is < 100 || > 999', function(done) { - assert.throws(function() { - res.status(99) - }, InvalidStatusError, "for value 99") - assert.throws(function() { - res.status(1000) - }, InvalidStatusError, "for value 1000") - done() + describe('invalid status codes', function() { + + it('should throw if status code is < 100', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(99).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + + }) + + it('should throw if status code is > 999', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(1000).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + + }) + + it('should throw if status code is undefined', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(undefined).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + }) - it('if status code is not an integer', function (done) { - var cases = [ - 200.1, - '200.1', - NaN, - Infinity, - -Infinity, - undefined, - null, - function () {}, - true, - false, - {}, - [], - ] - cases.forEach(function (item) { - assert.throws(function () { - res.status(item) - }, InvalidStatusError) - }) - done() + + it('should throw if status code is null', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(null).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + + }) + + it('should throw if status code is a float', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(200.1).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) }) + it('should throw if status code is a string float', function(done) { + var app = express(); + app.use(function(req, res){ + res.status('200.1').end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + }) + + it('should throw if status code is NaN', function(done) { + var app = express(); + app.use(function(req, res){ + res.status(NaN).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + }) }) }) }) From 964f83a92dd4632343f82cb5b0f2a4ae2625a940 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Tue, 10 Mar 2020 08:34:19 -0400 Subject: [PATCH 03/29] throw if number is string --- lib/response.js | 4 ++-- test/res.status.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/response.js b/lib/response.js index 3e8d0c7089..9f3d35170e 100644 --- a/lib/response.js +++ b/lib/response.js @@ -56,14 +56,14 @@ var charsetRegExp = /;\s*charset\s*=/; /** * Set status `code`. * - * @param {Number|String} code + * @param {Number} code * @return {ServerResponse} * @public */ res.status = function status(code) { // check that status code is an integer within valid range - if (!Number.isInteger(+code) || code < 100 || code > 999) { + if (!Number.isInteger(code) || code < 100 || code > 999) { throw new TypeError('Invalid status code'); } diff --git a/test/res.status.js b/test/res.status.js index 76e257952e..c63b3c8ab9 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -78,10 +78,10 @@ describe('res', function(){ .expect(500, /TypeError: Invalid status code/, done) }) - it('should throw if status code is a string float', function(done) { + it('should throw if status code is a string', function(done) { var app = express(); app.use(function(req, res){ - res.status('200.1').end(); + res.status('200').end(); }); request(app) From 9fa5241048201133629ff81bbe19eb9a1c87941b Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 22:16:16 -0400 Subject: [PATCH 04/29] narrow valid range to between 1xx and 5xx --- lib/response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 9f3d35170e..26335e9618 100644 --- a/lib/response.js +++ b/lib/response.js @@ -63,7 +63,7 @@ var charsetRegExp = /;\s*charset\s*=/; res.status = function status(code) { // check that status code is an integer within valid range - if (!Number.isInteger(code) || code < 100 || code > 999) { + if (!Number.isInteger(code) || code < 100 || code > 599) { throw new TypeError('Invalid status code'); } From 0761255c192329de6d1d1d65706a4e934c9f1901 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 22:16:46 -0400 Subject: [PATCH 05/29] disambiguate the error message --- lib/response.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/response.js b/lib/response.js index 26335e9618..395e06881b 100644 --- a/lib/response.js +++ b/lib/response.js @@ -62,12 +62,16 @@ var charsetRegExp = /;\s*charset\s*=/; */ res.status = function status(code) { - // check that status code is an integer within valid range - if (!Number.isInteger(code) || code < 100 || code > 599) { - throw new TypeError('Invalid status code'); + // Check if the status code is not an integer + if (!Number.isInteger(code)) { + throw new TypeError(`Invalid status code: ${code}. Status code must be an integer.`); + } + // Check if the status code is out of the valid range + if (code < 100 || code > 599) { + throw new RangeError(`Invalid status code: ${code}. Status code must be between 100 and 599.`); } - this.statusCode = code + this.statusCode = code; return this; }; From 00ecf4df470b5ad65bc2e5f55aad6542641558d5 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:17:39 -0400 Subject: [PATCH 06/29] update skipped tests, remove invalid string test --- test/res.status.js | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index 4102dc7a33..cf9a5592bb 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -9,11 +9,7 @@ var isIoJs = process.release describe('res', function () { describe('.status(code)', function () { - // This test fails in node 4.0.0 - // https://github.com/expressjs/express/pull/2237/checks - // As this will all be removed when https://github.com/expressjs/express/pull/4212 - // lands I am skipping for now and we can delete with that PR - describe.skip('when "code" is undefined', function () { + describe('when "code" is undefined', function () { it('should raise error for invalid status code', function (done) { var app = express() @@ -33,7 +29,7 @@ describe('res', function () { }) }) - describe.skip('when "code" is null', function () { + describe('when "code" is null', function () { it('should raise error for invalid status code', function (done) { var app = express() @@ -109,21 +105,7 @@ describe('res', function () { }) }) - describe('when "code" is "410"', function () { - it('should set the response status code to 410', function (done) { - var app = express() - - app.use(function (req, res) { - res.status('410').end() - }) - - request(app) - .get('/') - .expect(410, done) - }) - }) - - describe.skip('when "code" is 410.1', function () { + describe('when "code" is 410.1', function () { it('should set the response status code to 410', function (done) { var app = express() @@ -143,7 +125,7 @@ describe('res', function () { }) }) - describe.skip('when "code" is 1000', function () { + describe('when "code" is 1000', function () { it('should raise error for invalid status code', function (done) { var app = express() @@ -163,7 +145,7 @@ describe('res', function () { }) }) - describe.skip('when "code" is 99', function () { + describe('when "code" is 99', function () { it('should raise error for invalid status code', function (done) { var app = express() @@ -183,7 +165,7 @@ describe('res', function () { }) }) - describe.skip('when "code" is -401', function () { + describe('when "code" is -401', function () { it('should raise error for invalid status code', function (done) { var app = express() From a2a144848aa346bb00301fbc43facd284c717a70 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:21:53 -0400 Subject: [PATCH 07/29] remove invalid float test --- test/res.status.js | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index cf9a5592bb..95bc57caf3 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -104,25 +104,6 @@ describe('res', function () { .expect(501, done) }) }) - - describe('when "code" is 410.1', function () { - it('should set the response status code to 410', function (done) { - var app = express() - - app.use(function (req, res) { - res.status(410.1).end() - }) - - request(app) - .get('/') - .expect(410, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } - }) - }) }) describe('when "code" is 1000', function () { From d0bd8356825e024eb6dec211b54a3f5d07849bd8 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:23:54 -0400 Subject: [PATCH 08/29] fixup! remove invalid float test --- test/res.status.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/res.status.js b/test/res.status.js index 95bc57caf3..4359fe71fe 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -104,7 +104,6 @@ describe('res', function () { .expect(501, done) }) }) - }) describe('when "code" is 1000', function () { it('should raise error for invalid status code', function (done) { From 0670a3bff1c1f12d19c01fa0d009a4cc4cb13050 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:34:23 -0400 Subject: [PATCH 09/29] fix invalid range tests error assertions --- test/res.status.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index 4359fe71fe..f3a0a4d4da 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -175,19 +175,19 @@ describe('res', function () { request(app) .get('/') - .expect(500, /TypeError: Invalid status code/, done) + .expect(500, /RangeError: Invalid status code/, done) }) - it('should throw if status code is > 999', function(done) { + it('should throw if status code is > 599', function(done) { var app = express(); app.use(function(req, res){ - res.status(1000).end(); + res.status(600).end(); }); request(app) .get('/') - .expect(500, /TypeError: Invalid status code/, done) + .expect(500, /RangeError: Invalid status code/, done) }) From 8fc8c9510b99c76723114961560685e0eb504f41 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:35:53 -0400 Subject: [PATCH 10/29] remove unused deprecate function --- lib/response.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index 9d45e62e90..2171e7a918 100644 --- a/lib/response.js +++ b/lib/response.js @@ -15,7 +15,6 @@ var Buffer = require('safe-buffer').Buffer var contentDisposition = require('content-disposition'); var createError = require('http-errors') -var deprecate = require('depd')('express'); var encodeUrl = require('encodeurl'); var escapeHtml = require('escape-html'); var http = require('http'); From 063a3bb9c10355b76134e8b68b5a08da15baccfa Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Apr 2024 23:45:00 -0400 Subject: [PATCH 11/29] add test to assert on 200.00 coming through as 200 this is the behavior of node's underlying HTTP module --- test/res.status.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/res.status.js b/test/res.status.js index f3a0a4d4da..503c633b6d 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -105,6 +105,20 @@ describe('res', function () { }) }) + describe('when "code" is 200.00', function () { + it('should set the response status code to 200', function (done) { + var app = express() + + app.use(function (req, res) { + res.status(200.00).end() + }) + + request(app) + .get('/') + .expect(200, done) + }) + }) + describe('when "code" is 1000', function () { it('should raise error for invalid status code', function (done) { var app = express() From a6e4e9bc264caf1b38bc59a8f318a348bdc57504 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:25:24 -0400 Subject: [PATCH 12/29] revert back to throwing only on > 999 and < 100 --- test/res.status.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index 503c633b6d..56f5b19777 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -193,10 +193,10 @@ describe('res', function () { }) - it('should throw if status code is > 599', function(done) { + it('should throw if status code is > 999', function(done) { var app = express(); app.use(function(req, res){ - res.status(600).end(); + res.status(1000).end(); }); request(app) From cca14387081644d1cff581c002aab21c7bc7f998 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:26:42 -0400 Subject: [PATCH 13/29] update implementation for > 999 --- lib/response.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/response.js b/lib/response.js index 2171e7a918..1f018a85b9 100644 --- a/lib/response.js +++ b/lib/response.js @@ -68,9 +68,9 @@ res.status = function status(code) { if (!Number.isInteger(code)) { throw new TypeError(`Invalid status code: ${code}. Status code must be an integer.`); } - // Check if the status code is out of the valid range - if (code < 100 || code > 599) { - throw new RangeError(`Invalid status code: ${code}. Status code must be between 100 and 599.`); + // Check if the status code is outside of Node's valid range + if (code < 100 || code > 999) { + throw new RangeError(`Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.`); } this.statusCode = code; From ce181526df2de743fbd9a2c95792c4a5f01507fc Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:29:30 -0400 Subject: [PATCH 14/29] add test for 700 status code --- test/res.status.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/res.status.js b/test/res.status.js index 56f5b19777..d749bd0b48 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -105,6 +105,20 @@ describe('res', function () { }) }) + describe('when "code" is 700', function () { + it('should set the response status code to 700', function (done) { + var app = express() + + app.use(function (req, res) { + res.status(700).end() + }) + + request(app) + .get('/') + .expect(501, done) + }) + }) + describe('when "code" is 200.00', function () { it('should set the response status code to 200', function (done) { var app = express() From bade7a141bc7ec901e22c488644facd9b3c15af3 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:44:13 -0400 Subject: [PATCH 15/29] update history with change --- History.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/History.md b/History.md index 73fc46b26f..bb37e9d9cd 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,10 @@ +unreleased +========================= +* breaking: + * `res.status()` accepts only integers, and input must be greater than 99 and less than 999 + * will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range + * will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs + 5.0.0-beta.3 / 2024-03-25 ========================= From eabe2cd2886c60715e6be6957e60566bf419347a Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:53:00 -0400 Subject: [PATCH 16/29] update jsdoc --- lib/response.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/response.js b/lib/response.js index 1f018a85b9..e797c6c431 100644 --- a/lib/response.js +++ b/lib/response.js @@ -56,10 +56,15 @@ module.exports = res var schemaAndHostRegExp = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?\/\/[^\\\/\?]+/; /** - * Set status `code`. + * Set the HTTP status code for the response. * - * @param {Number} code - * @return {ServerResponse} + * This function sets the response status code and expects an integer value between 100 and 999 inclusive. + * It throws an error if the provided status code is not an integer or if it's outside the allowable range. + * + * @param {number} code - The HTTP status code to set. + * @return {ServerResponse} - Returns itself for chaining methods. + * @throws {TypeError} If `code` is not an integer. + * @throws {RangeError} If `code` is outside the range 100 to 999. * @public */ @@ -77,6 +82,21 @@ res.status = function status(code) { return this; }; + +res.status = function status(code) { + // Check if the status code is not an integer + if (!Number.isInteger(code)) { + throw new TypeError(`Invalid status code: ${code}. Status code must be an integer.`); + } + // Check if the status code is outside of Node's valid range + if (code < 100 || code > 999) { + throw new RangeError(`Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.`); + } + + this.statusCode = code; + return this; +}; + /** * Set Link header field with the given `links`. * From 1a1358298bcfb56df111d6c08be2f1f092e08280 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:54:48 -0400 Subject: [PATCH 17/29] clarify jsdoc comment --- lib/response.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/response.js b/lib/response.js index e797c6c431..c57568c12f 100644 --- a/lib/response.js +++ b/lib/response.js @@ -58,8 +58,8 @@ var schemaAndHostRegExp = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?\/\/[^\\\/\?]+/; /** * Set the HTTP status code for the response. * - * This function sets the response status code and expects an integer value between 100 and 999 inclusive. - * It throws an error if the provided status code is not an integer or if it's outside the allowable range. + * Set the response status code. Expects an integer value between 100 and 999 inclusive. + * Throws an error if the provided status code is not an integer or if it's outside the allowable range. * * @param {number} code - The HTTP status code to set. * @return {ServerResponse} - Returns itself for chaining methods. From 8374bdbc99778ca28218f2b4fd70f7aa7f8c354a Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:55:46 -0400 Subject: [PATCH 18/29] one more round of jsdoc --- lib/response.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/response.js b/lib/response.js index c57568c12f..b83a56214b 100644 --- a/lib/response.js +++ b/lib/response.js @@ -58,7 +58,7 @@ var schemaAndHostRegExp = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?\/\/[^\\\/\?]+/; /** * Set the HTTP status code for the response. * - * Set the response status code. Expects an integer value between 100 and 999 inclusive. + * Expects an integer value between 100 and 999 inclusive. * Throws an error if the provided status code is not an integer or if it's outside the allowable range. * * @param {number} code - The HTTP status code to set. From cee48e740a7479f61bf4fe40f60085cb3990439e Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 20:56:53 -0400 Subject: [PATCH 19/29] update 501 test --- test/res.status.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/res.status.js b/test/res.status.js index d749bd0b48..64cfc8304c 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -115,7 +115,7 @@ describe('res', function () { request(app) .get('/') - .expect(501, done) + .expect(700, done) }) }) From 5130879bc943c4aa01f750aeda59bf108f70f163 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 21:03:43 -0400 Subject: [PATCH 20/29] add invalid status code test for res.sendStatus --- test/res.sendStatus.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/res.sendStatus.js b/test/res.sendStatus.js index 9b1de8385c..f4a160ef17 100644 --- a/test/res.sendStatus.js +++ b/test/res.sendStatus.js @@ -28,5 +28,17 @@ describe('res', function () { .get('/') .expect(599, '599', done); }) + + it('should raise error for invalid status code', function (done) { + var app = express() + + app.use(function (req, res) { + res.status(undefined).end() + }) + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code/, done) + }) }) }) From 63192c42ba916dfe5bdde2ff9b1b5e395759db6b Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 21:34:59 -0400 Subject: [PATCH 21/29] add test describe block for valid range --- test/res.status.js | 305 ++++++++++++++++----------------------------- 1 file changed, 109 insertions(+), 196 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index 64cfc8304c..5f9797adef 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -1,280 +1,193 @@ 'use strict' - -var express = require('../') -var request = require('supertest') - -var isIoJs = process.release - ? process.release.name === 'io.js' - : ['v1.', 'v2.', 'v3.'].indexOf(process.version.slice(0, 3)) !== -1 +const express = require('../.'); +const request = require('supertest'); describe('res', function () { describe('.status(code)', function () { - describe('when "code" is undefined', function () { - it('should raise error for invalid status code', function (done) { - var app = express() - app.use(function (req, res) { - res.status(undefined).end() - }) + it('should set the status code when valid', function (done) { + var app = express(); - request(app) - .get('/') - .expect(500, /Invalid status code/, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } - }) - }) - }) + app.use(function (req, res) { + res.status(200).end(); + }); - describe('when "code" is null', function () { - it('should raise error for invalid status code', function (done) { - var app = express() + request(app) + .get('/') + .expect(200, done); + }); - app.use(function (req, res) { - res.status(null).end() - }) + describe('accept valid ranges', function() { + describe('when "code" is 100', function () { + it('should set the response status code to 201', function (done) { + var app = express() - request(app) - .get('/') - .expect(500, /Invalid status code/, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } + app.use(function (req, res) { + res.status(100).end() }) + + request(app) + .get('/') + .expect(201, done) + }) }) - }) - describe('when "code" is 201', function () { - it('should set the response status code to 201', function (done) { - var app = express() + describe('when "code" is 201', function () { + it('should set the response status code to 201', function (done) { + var app = express() - app.use(function (req, res) { - res.status(201).end() - }) + app.use(function (req, res) { + res.status(201).end() + }) - request(app) + request(app) .get('/') .expect(201, done) + }) }) - }) - describe('when "code" is 302', function () { - it('should set the response status code to 302', function (done) { - var app = express() + describe('when "code" is 302', function () { + it('should set the response status code to 302', function (done) { + var app = express() - app.use(function (req, res) { - res.status(302).end() - }) + app.use(function (req, res) { + res.status(302).end() + }) - request(app) + request(app) .get('/') .expect(302, done) + }) }) - }) - describe('when "code" is 403', function () { - it('should set the response status code to 403', function (done) { - var app = express() + describe('when "code" is 403', function () { + it('should set the response status code to 403', function (done) { + var app = express() - app.use(function (req, res) { - res.status(403).end() - }) + app.use(function (req, res) { + res.status(403).end() + }) - request(app) + request(app) .get('/') .expect(403, done) + }) }) - }) - describe('when "code" is 501', function () { - it('should set the response status code to 501', function (done) { - var app = express() + describe('when "code" is 501', function () { + it('should set the response status code to 501', function (done) { + var app = express() - app.use(function (req, res) { - res.status(501).end() - }) + app.use(function (req, res) { + res.status(501).end() + }) - request(app) + request(app) .get('/') .expect(501, done) - }) - }) - - describe('when "code" is 700', function () { - it('should set the response status code to 700', function (done) { - var app = express() - - app.use(function (req, res) { - res.status(700).end() }) - - request(app) - .get('/') - .expect(700, done) }) - }) - describe('when "code" is 200.00', function () { - it('should set the response status code to 200', function (done) { - var app = express() + describe('when "code" is 700', function () { + it('should set the response status code to 501', function (done) { + var app = express() - app.use(function (req, res) { - res.status(200.00).end() - }) + app.use(function (req, res) { + res.status(700).end() + }) - request(app) + request(app) .get('/') - .expect(200, done) - }) - }) - - describe('when "code" is 1000', function () { - it('should raise error for invalid status code', function (done) { - var app = express() - - app.use(function (req, res) { - res.status(1000).end() + .expect(501, done) }) - - request(app) - .get('/') - .expect(500, /Invalid status code/, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } - }) }) - }) - describe('when "code" is 99', function () { - it('should raise error for invalid status code', function (done) { - var app = express() + }) + describe('invalid status codes', function () { + it('should raise error for status code below 100', function (done) { + var app = express(); app.use(function (req, res) { - res.status(99).end() - }) + res.status(99).end(); + }); request(app) .get('/') - .expect(500, /Invalid status code/, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } - }) - }) - }) + .expect(500, /Invalid status code/, done); + }); - describe('when "code" is -401', function () { - it('should raise error for invalid status code', function (done) { - var app = express() + it('should raise error for status code above 999', function (done) { + var app = express(); app.use(function (req, res) { - res.status(-401).end() - }) + res.status(1000).end(); + }); request(app) .get('/') - .expect(500, /Invalid status code/, function (err) { - if (isIoJs) { - done(err ? null : new Error('expected error')) - } else { - done(err) - } - }) - }) - }) - - describe('invalid status codes', function() { + .expect(500, /Invalid status code/, done); + }); - it('should throw if status code is < 100', function(done) { + it('should raise error for non-integer status codes', function (done) { var app = express(); - app.use(function(req, res){ - res.status(99).end(); - }); - - request(app) - .get('/') - .expect(500, /RangeError: Invalid status code/, done) - - }) - it('should throw if status code is > 999', function(done) { - var app = express(); - app.use(function(req, res){ - res.status(1000).end(); + app.use(function (req, res) { + res.status(200.1).end(); }); request(app) - .get('/') - .expect(500, /RangeError: Invalid status code/, done) - - }) + .get('/') + .expect(500, /Invalid status code/, done); + }); - it('should throw if status code is undefined', function(done) { + it('should raise error for undefined status code', function (done) { var app = express(); - app.use(function(req, res){ + + app.use(function (req, res) { res.status(undefined).end(); }); request(app) - .get('/') - .expect(500, /TypeError: Invalid status code/, done) - - }) + .get('/') + .expect(500, /Invalid status code/, done); + }); - it('should throw if status code is null', function(done) { + it('should raise error for null status code', function (done) { var app = express(); - app.use(function(req, res){ + + app.use(function (req, res) { res.status(null).end(); }); request(app) - .get('/') - .expect(500, /TypeError: Invalid status code/, done) - - }) + .get('/') + .expect(500, /Invalid status code/, done); + }); - it('should throw if status code is a float', function(done) { + it('should raise error for string status code', function (done) { var app = express(); - app.use(function(req, res){ - res.status(200.1).end(); - }); - request(app) - .get('/') - .expect(500, /TypeError: Invalid status code/, done) - }) - - it('should throw if status code is a string', function(done) { - var app = express(); - app.use(function(req, res){ - res.status('200').end(); + app.use(function (req, res) { + res.status("200").end(); }); request(app) - .get('/') - .expect(500, /TypeError: Invalid status code/, done) - }) + .get('/') + .expect(500, /Invalid status code/, done); + }); - it('should throw if status code is NaN', function(done) { + it('should raise error for NaN status code', function (done) { var app = express(); - app.use(function(req, res){ + + app.use(function (req, res) { res.status(NaN).end(); }); request(app) - .get('/') - .expect(500, /TypeError: Invalid status code/, done) - }) - }) - }) -}) + .get('/') + .expect(500, /Invalid status code/, done); + }); + }); + }); +}); + From f7b7e3513ead71b86f4223f1530888f7c58d6157 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 21:38:30 -0400 Subject: [PATCH 22/29] fixup! add test describe block for valid range --- test/res.status.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/res.status.js b/test/res.status.js index 5f9797adef..a9c86029f7 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -98,11 +98,39 @@ describe('res', function () { request(app) .get('/') - .expect(501, done) + .expect(700, done) + }) + }) + + describe('when "code" is 800', function () { + it('should set the response status code to 501', function (done) { + var app = express() + + app.use(function (req, res) { + res.status(800).end() + }) + + request(app) + .get('/') + .expect(800, done) }) }) + describe('when "code" is 900', function () { + it('should set the response status code to 501', function (done) { + var app = express() + + app.use(function (req, res) { + res.status(800).end() + }) + + request(app) + .get('/') + .expect(900, done) + }) + }) }) + describe('invalid status codes', function () { it('should raise error for status code below 100', function (done) { var app = express(); From 86eb68c553572ae2ae4c1e5ec569f3e445f9d5f9 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 21:49:11 -0400 Subject: [PATCH 23/29] reduce the describe nesting --- test/res.status.js | 114 +++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 65 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index a9c86029f7..5fb702996b 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -18,116 +18,100 @@ describe('res', function () { }); describe('accept valid ranges', function() { - describe('when "code" is 100', function () { - it('should set the response status code to 201', function (done) { - var app = express() + it('should set the response status code to 100', function (done) { + var app = express() - app.use(function (req, res) { - res.status(100).end() - }) + app.use(function (req, res) { + res.status(100).end() + }) - request(app) + request(app) .get('/') - .expect(201, done) - }) + .expect(100, done) }) - describe('when "code" is 201', function () { - it('should set the response status code to 201', function (done) { - var app = express() + it('should set the response status code to 201', function (done) { + var app = express() - app.use(function (req, res) { - res.status(201).end() - }) + app.use(function (req, res) { + res.status(201).end() + }) - request(app) + request(app) .get('/') .expect(201, done) - }) }) - describe('when "code" is 302', function () { - it('should set the response status code to 302', function (done) { - var app = express() + it('should set the response status code to 302', function (done) { + var app = express() - app.use(function (req, res) { - res.status(302).end() - }) + app.use(function (req, res) { + res.status(302).end() + }) - request(app) + request(app) .get('/') .expect(302, done) - }) }) - describe('when "code" is 403', function () { - it('should set the response status code to 403', function (done) { - var app = express() + it('should set the response status code to 403', function (done) { + var app = express() - app.use(function (req, res) { - res.status(403).end() - }) + app.use(function (req, res) { + res.status(403).end() + }) - request(app) + request(app) .get('/') .expect(403, done) - }) }) - describe('when "code" is 501', function () { - it('should set the response status code to 501', function (done) { - var app = express() + it('should set the response status code to 501', function (done) { + var app = express() - app.use(function (req, res) { - res.status(501).end() - }) + app.use(function (req, res) { + res.status(501).end() + }) - request(app) + request(app) .get('/') .expect(501, done) - }) }) - describe('when "code" is 700', function () { - it('should set the response status code to 501', function (done) { - var app = express() + it('should set the response status code to 700', function (done) { + var app = express() - app.use(function (req, res) { - res.status(700).end() - }) + app.use(function (req, res) { + res.status(700).end() + }) - request(app) + request(app) .get('/') .expect(700, done) - }) }) - describe('when "code" is 800', function () { - it('should set the response status code to 501', function (done) { - var app = express() + it('should set the response status code to 800', function (done) { + var app = express() - app.use(function (req, res) { - res.status(800).end() - }) + app.use(function (req, res) { + res.status(800).end() + }) - request(app) + request(app) .get('/') .expect(800, done) - }) }) - describe('when "code" is 900', function () { - it('should set the response status code to 501', function (done) { - var app = express() + it('should set the response status code to 900', function (done) { + var app = express() - app.use(function (req, res) { - res.status(800).end() - }) + app.use(function (req, res) { + res.status(800).end() + }) - request(app) + request(app) .get('/') .expect(900, done) - }) }) }) From 6bc51368bc085d596422ae71277ccfeb5f3aee4b Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 22:05:52 -0400 Subject: [PATCH 24/29] switch to testing status 100, to avoid 100-continue behavior --- test/res.status.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/res.status.js b/test/res.status.js index 5fb702996b..1fc2d5dfb0 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -18,16 +18,17 @@ describe('res', function () { }); describe('accept valid ranges', function() { - it('should set the response status code to 100', function (done) { + // not testing w/ 100, because that has specific meaning and behavior in Node as Expect: 100-continue + it('should set the response status code to 101', function (done) { var app = express() app.use(function (req, res) { - res.status(100).end() + res.status(101).end() }) request(app) .get('/') - .expect(100, done) + .expect(101, done) }) it('should set the response status code to 201', function (done) { From bbf58badcf26e5d2feb8bccdb2ac8dd3d14617f2 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Fri, 3 May 2024 22:07:01 -0400 Subject: [PATCH 25/29] fix 900 test --- test/res.status.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/res.status.js b/test/res.status.js index 1fc2d5dfb0..59c8a57e70 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -107,7 +107,7 @@ describe('res', function () { var app = express() app.use(function (req, res) { - res.status(800).end() + res.status(900).end() }) request(app) From 64e857601795b22469b3e851a896287a428edee4 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Wed, 5 Jun 2024 09:53:22 -0400 Subject: [PATCH 26/29] stringify code in thrown RangeError message --- lib/response.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/response.js b/lib/response.js index b83a56214b..139d6fd7fc 100644 --- a/lib/response.js +++ b/lib/response.js @@ -71,11 +71,11 @@ var schemaAndHostRegExp = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:)?\/\/[^\\\/\?]+/; res.status = function status(code) { // Check if the status code is not an integer if (!Number.isInteger(code)) { - throw new TypeError(`Invalid status code: ${code}. Status code must be an integer.`); + throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`); } // Check if the status code is outside of Node's valid range if (code < 100 || code > 999) { - throw new RangeError(`Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.`); + throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`); } this.statusCode = code; From 4dbf7735492a47dc5214d4ce9642a4b244e231fb Mon Sep 17 00:00:00 2001 From: Jon Church Date: Wed, 5 Jun 2024 09:54:11 -0400 Subject: [PATCH 27/29] remove accidentally duplicated res.status method --- lib/response.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/lib/response.js b/lib/response.js index 139d6fd7fc..6ad54dbfc7 100644 --- a/lib/response.js +++ b/lib/response.js @@ -82,21 +82,6 @@ res.status = function status(code) { return this; }; - -res.status = function status(code) { - // Check if the status code is not an integer - if (!Number.isInteger(code)) { - throw new TypeError(`Invalid status code: ${code}. Status code must be an integer.`); - } - // Check if the status code is outside of Node's valid range - if (code < 100 || code > 999) { - throw new RangeError(`Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.`); - } - - this.statusCode = code; - return this; -}; - /** * Set Link header field with the given `links`. * From bf4541b4647608a6e8d7604f246be5aca1475fd8 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 8 Jun 2024 01:01:28 -0400 Subject: [PATCH 28/29] fix error range message Co-authored-by: Chris de Almeida --- History.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/History.md b/History.md index bb37e9d9cd..89d5af3ceb 100644 --- a/History.md +++ b/History.md @@ -1,7 +1,7 @@ unreleased ========================= * breaking: - * `res.status()` accepts only integers, and input must be greater than 99 and less than 999 + * `res.status()` accepts only integers, and input must be greater than 99 and less than 1000 * will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range * will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs From 794f2dead942c91baf75073915f022abf5f79d31 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Sat, 27 Jul 2024 13:15:20 -0400 Subject: [PATCH 29/29] update sendStatus invalid code test to use sendStatus --- test/res.sendStatus.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/res.sendStatus.js b/test/res.sendStatus.js index f4a160ef17..b244cf9d17 100644 --- a/test/res.sendStatus.js +++ b/test/res.sendStatus.js @@ -33,7 +33,7 @@ describe('res', function () { var app = express() app.use(function (req, res) { - res.status(undefined).end() + res.sendStatus(undefined).end() }) request(app)