diff --git a/lib/response.js b/lib/response.js index 7a2f0ecce56..5a07b137099 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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.'); + statusCode = 500; + } + var body = statuses.message[statusCode] || String(statusCode); this.status(statusCode); this.type('txt'); - return this.send(body); }; diff --git a/test/res.sendStatus.js b/test/res.sendStatus.js index b244cf9d173..280ab883fa6 100644 --- a/test/res.sendStatus.js +++ b/test/res.sendStatus.js @@ -1,4 +1,4 @@ -'use strict' +'use strict' var express = require('..') var request = require('supertest') @@ -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) }) }) })