Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle invalid status codes #3143

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ var charsetRegExp = /;\s*charset\s*=/;
* @public
*/

res.status = function status(code) {
this.statusCode = code;
res.status = function status(statusCode) {
// check that status code is valid
if (typeof statusCode !== 'number' || statusCode < 100 || statusCode > 999) {
throw new TypeError('Invalid status code.');
}

this.statusCode = statusCode;
return this;
};

Expand Down Expand Up @@ -110,7 +115,7 @@ res.send = function send(body) {
// support res.send(status, body)
if (arguments.length === 2) {
deprecate('res.send(status, body): Use res.status(status).send(body) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
chunk = arguments[1];
}

Expand Down Expand Up @@ -169,7 +174,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) {
Expand Down Expand Up @@ -208,7 +213,7 @@ res.json = function json(obj) {
// support res.json(status, obj)
if (arguments.length === 2) {
deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
val = arguments[1];
}

Expand Down Expand Up @@ -244,7 +249,7 @@ res.jsonp = function jsonp(obj) {
// support res.jsonp(status, obj)
if (arguments.length === 2) {
deprecate('res.jsonp(status, obj): Use res.status(status).jsonp(obj) instead');
this.statusCode = arguments[0];
this.status(arguments[0]);
val = arguments[1];
}

Expand Down Expand Up @@ -304,11 +309,10 @@ res.jsonp = function jsonp(obj) {
*/

res.sendStatus = function sendStatus(statusCode) {
var body = statusCodes[statusCode] || String(statusCode);

this.statusCode = statusCode;
this.status(statusCode);
this.type('txt');

var body = statusCodes[statusCode] || String(statusCode);
return this.send(body);
};

Expand Down Expand Up @@ -788,7 +792,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') {
Expand Down
24 changes: 24 additions & 0 deletions test/res.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,29 @@ describe('res', function(){
.expect('Created')
.expect(201, done);
})

it('should throw a TypeError if not a number', function(done) {
var app = express();

app.use(function(req, res){
res.status('foo').end();
});

request(app)
.get('/')
.expect(500, /Invalid status code/, done);
})

it('should throw a TypeError if invalid number', function(done){
var app = express();

app.use(function(req, res){
res.status(10000).end();
});

request(app)
.get('/')
.expect(500, /Invalid status code/, done);
})
})
})