Skip to content

Commit 0dae369

Browse files
committed
feat(response): new setting strict status codes
test: test for status in range with strictt status refactor: default strict status codes to true feat(status): set strict status codes to false docs: fix History message
1 parent 0983158 commit 0dae369

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

History.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
unreleased
22
=========================
3-
* remove:
3+
* remove:
44
- `path-is-absolute` dependency - use `path.isAbsolute` instead
55
* breaking:
6-
* `res.status()` accepts only integers, and input must be greater than 99 and less than 1000
7-
* will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range
8-
* will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs
6+
* By default `res.status()` accepts only integers, and input must be greater than 99 and less than 1000
7+
* Will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 1000.` for inputs outside this range.
8+
* Will throw a `TypeError: Invalid status code: ${code}. Status code must be an integer.` for non integer inputs.
9+
* Added a new default setting `strict status codes`, with a default value of false.
10+
* When the variable `strict status codes` is set to true, `res.status()` will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 600 in strict status codes.` for inputs outside this range.
911
1012
* change:
1113
- `res.clearCookie` will ignore user provided `maxAge` and `expires` options

lib/application.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ app.defaultConfiguration = function defaultConfiguration() {
9999
this.set('query parser', 'simple')
100100
this.set('subdomain offset', 2);
101101
this.set('trust proxy', false);
102+
this.set('strict status codes', false);
102103

103104
// trust proxy inherit back-compat
104105
Object.defineProperty(this.settings, trustProxyDefaultSymbol, {

lib/response.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ res.status = function status(code) {
7373
if (!Number.isInteger(code)) {
7474
throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);
7575
}
76+
77+
// Check if the status code is outside of strict status codes valid range
78+
if (this.app.get('strict status codes') === true && (code < 100 || code > 599)) {
79+
throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 600 in strict status codes.`);
80+
}
81+
7682
// Check if the status code is outside of Node's valid range
7783
if (code < 100 || code > 999) {
7884
throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"scripts": {
9393
"lint": "eslint .",
9494
"test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
95+
"test-watch": "npm run test -- --watch",
9596
"test-ci": "nyc --reporter=lcovonly --reporter=text npm test",
9697
"test-cov": "nyc --reporter=html --reporter=text npm test",
9798
"test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"

test/res.status.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('res', function () {
1717
.expect(200, done);
1818
});
1919

20-
describe('accept valid ranges', function() {
20+
describe('accept valid ranges', function () {
2121
// not testing w/ 100, because that has specific meaning and behavior in Node as Expect: 100-continue
2222
it('should set the response status code to 101', function (done) {
2323
var app = express()
@@ -129,6 +129,20 @@ describe('res', function () {
129129
.expect(500, /Invalid status code/, done);
130130
});
131131

132+
it('should raise error for status code above 599', function (done) {
133+
var app = express();
134+
135+
app.set('strict status codes', true);
136+
137+
app.use(function (req, res) {
138+
res.status(600).end();
139+
});
140+
141+
request(app)
142+
.get('/')
143+
.expect(500, /Status code must be greater than 99 and less than 600./, done);
144+
});
145+
132146
it('should raise error for status code above 999', function (done) {
133147
var app = express();
134148

@@ -203,4 +217,3 @@ describe('res', function () {
203217
});
204218
});
205219
});
206-

0 commit comments

Comments
 (0)