Skip to content
Open
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
9 changes: 6 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,14 @@ res.jsonp = function jsonp(obj) {
*/

res.sendStatus = function sendStatus(statusCode) {
var body = statuses.message[statusCode] || String(statusCode)

// Emit deprecation warning for invalid status codes
if (typeof statusCode !== 'number') {
deprecate('Invalid status code: ' + String(statusCode) + '. Status code must be a number. This will throw an error in Express 6.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deprecate('Invalid status code: ' + String(statusCode) + '. Status code must be a number. This will throw an error in Express 6.');
deprecate('statusCode must be a valid number to res.sendStatus.');

I think keep it simple and only idea is just show deprecation warning.

statusCode = 500;
}
var body = statuses.message[statusCode] || String(statusCode);
this.status(statusCode);
this.type('txt');

return this.send(body);
};

Expand Down
73 changes: 68 additions & 5 deletions test/res.sendStatus.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict'

var express = require('..')
var request = require('supertest')
Expand Down Expand Up @@ -29,16 +29,79 @@ describe('res', function () {
.expect(599, '599', done);
})

it('should raise error for invalid status code', function (done) {
describe('with invalid status codes', function(){
it('should fallback to 500 for undefined status code', function(done){
var app = express()

app.use(function(req, res){
res.sendStatus(undefined)
})

request(app)
.get('/')
.expect(500, 'Internal Server Error', done)
})

it('should fallback to 500 for BigInt status code', function(done){
var app = express()

app.use(function(req, res){
res.sendStatus(200n)
})

request(app)
.get('/')
.expect(500, 'Internal Server Error', done)
})

it('should fallback to 500 for string status code', function(done){
var app = express()

app.use(function(req, res){
res.sendStatus('invalid')
})

request(app)
.get('/')
.expect(500, 'Internal Server Error', done)
})

it('should contain deprecation logic for invalid status codes', function(){
// Test that the deprecation logic is present by examining the function
var express = require('..')
var app = express()
var res = app.response

// Verify the function contains deprecation logic
var sendStatusSource = res.sendStatus.toString()
// Check that the function contains the deprecation warning
if (sendStatusSource.indexOf('deprecate') === -1) {
throw new Error('sendStatus function should contain deprecation warning')
}
if (sendStatusSource.indexOf('typeof statusCode !== \'number\'') === -1) {
throw new Error('sendStatus function should check for non-number status codes')
}
if (sendStatusSource.indexOf('statusCode = 500') === -1) {
throw new Error('sendStatus function should fallback to status 500')
}

// Check deprecation message content
if (sendStatusSource.indexOf('Express 6') === -1) {
throw new Error('Deprecation message should mention Express 6')
}
})
})

it('should not affect valid number status codes', function(done){
var app = express()

app.use(function (req, res) {
res.sendStatus(undefined).end()
app.use(function(req, res){
res.sendStatus(200)
})

request(app)
.get('/')
.expect(500, /TypeError: Invalid status code/, done)
.expect(200, 'OK', done)
})
})
})