diff --git a/lib/response.js b/lib/response.js index c9f08cd54f4..bf183e9699a 100644 --- a/lib/response.js +++ b/lib/response.js @@ -64,6 +64,14 @@ var charsetRegExp = /;\s*charset\s*=/; */ res.status = function status(code) { + + if ( + !Object.keys(http.STATUS_CODES).find( + (statuscode) => statuscode == code + ) + ) + throw new TypeError(`${code} is not a valid http status code.`); + this.statusCode = code; return this; }; diff --git a/test/res.status.js b/test/res.status.js index 8c173a645c5..8957d172c1a 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -16,5 +16,14 @@ describe('res', function(){ .expect('Created') .expect(201, done); }) + it("should throw an error internal server error for invalid status code.", function (done) { + var app = express(); + + app.use(function (req, res) { + res.status(209).end(); + }); + + request(app).get("/").expect(500, done); + }); }) })