Skip to content

Throw error if argument to res.status is null or undefined #2797

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

Closed
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ var charsetRegExp = /;\s*charset\s*=/;
*/

res.status = function status(code) {
this.statusCode = code;
if (code !== null && code !== undefined) {
this.statusCode = code;
} else {
throw new TypeError('Invalid status code.')
}
return this;
};

Expand Down
32 changes: 32 additions & 0 deletions test/res.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,37 @@ describe('res', function(){
.expect('Created')
.expect(201, done);
})

it('should throw a TypeError if code is undefined', function(done){
var app = express();
app.use(function(req, res){
res.status(undefined).end('Created');
});

request(app)
.get('/')
.expect(invalidCode)
.expect(500, done);
})

it('should throw a TypeError if code is null', function(done){
var app = express();
app.use(function(req, res){
res.status(null).end('Created');
});

request(app)
.get('/')
.expect(invalidCode)
.expect(500, done);
})

})
})

function invalidCode(res) {
if (!(res.error.text.match(/TypeError/ && res.error.text.match(/status code/)))) {
throw new Error('Expected a TypeError for invalid status code.');
}
}