Skip to content

Commit f5a28b0

Browse files
committed
feat(response): new setting strict status codes
test: test for status in range with strictt status
1 parent 0983158 commit f5a28b0

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

History.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
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 value of the variable `strict status codes` is set to true, `res.status()` accepts only integers, and input must be greater than 99 and less than 600.
11+
* Will throw a `RangeError: Invalid status code: ${code}. Status code must be greater than 99 and less than 600 in strict status codes.`
912
1013
* change:
1114
- `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 Node's 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.`);

test/res.status.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ describe('res', function () {
7979
.expect(501, done)
8080
})
8181

82+
it('should set the response status code to 404 with strict status code', function (done) {
83+
var app = express()
84+
85+
app.set('strict status codes', true)
86+
87+
app.use(function (req, res) {
88+
res.status(404).end()
89+
})
90+
91+
request(app)
92+
.get('/')
93+
.expect(404, done)
94+
})
95+
8296
it('should set the response status code to 700', function (done) {
8397
var app = express()
8498

@@ -129,6 +143,20 @@ describe('res', function () {
129143
.expect(500, /Invalid status code/, done);
130144
});
131145

146+
it('should raise error for status code above 599 with strict status code', function (done) {
147+
var app = express();
148+
149+
app.set('strict status codes', true);
150+
151+
app.use(function (req, res) {
152+
res.status(600).end();
153+
});
154+
155+
request(app)
156+
.get('/')
157+
.expect(500, /Status code must be greater than 99 and less than 600 in strict status codes./, done);
158+
});
159+
132160
it('should raise error for status code above 999', function (done) {
133161
var app = express();
134162

0 commit comments

Comments
 (0)