express/test/res.sendStatus.js
Jon Church 723b5451bb
Throw on invalid status codes (#4212)
* check status code is integer, or string integer, in range

* fix tests, update jsdoc comment for res.status

* throw if number is string

* narrow valid range to between 1xx and 5xx

* disambiguate the error message

* update skipped tests, remove invalid string test

* remove invalid float test

* fixup! remove invalid float test

* fix invalid range tests error assertions

* remove unused deprecate function

* add test to assert on 200.00 coming through as 200

this is the behavior of node's underlying HTTP module

* revert back to throwing only on > 999 and < 100

* update implementation for > 999

* add test for 700 status code

* update history with change

* update jsdoc

* clarify jsdoc comment

* one more round of jsdoc

* update 501 test

* add invalid status code test for res.sendStatus

* add test describe block for valid range

* fixup! add test describe block for valid range

* reduce the describe nesting

* switch to testing status 100, to avoid 100-continue behavior

* fix 900 test

* stringify code in thrown RangeError message

* remove accidentally duplicated res.status method

* fix error range message

Co-authored-by: Chris de Almeida <ctcpip@users.noreply.github.com>

* update sendStatus invalid code test to use sendStatus

---------

Co-authored-by: Chris de Almeida <ctcpip@users.noreply.github.com>
2024-07-30 14:49:13 -07:00

45 lines
951 B
JavaScript

'use strict'
var express = require('..')
var request = require('supertest')
describe('res', function () {
describe('.sendStatus(statusCode)', function () {
it('should send the status code and message as body', function (done) {
var app = express();
app.use(function(req, res){
res.sendStatus(201);
});
request(app)
.get('/')
.expect(201, 'Created', done);
})
it('should work with unknown code', function (done) {
var app = express();
app.use(function(req, res){
res.sendStatus(599);
});
request(app)
.get('/')
.expect(599, '599', done);
})
it('should raise error for invalid status code', function (done) {
var app = express()
app.use(function (req, res) {
res.sendStatus(undefined).end()
})
request(app)
.get('/')
.expect(500, /TypeError: Invalid status code/, done)
})
})
})