Compare commits

...

2 Commits

Author SHA1 Message Date
Sebastian Beltran
3981e29545 fix(tests): correct spelling of 'throw' in error messages 2025-03-18 09:26:05 -05:00
Sebastian Beltran
f636741bf8 feat(res.redirect): add validation for url and status arguments 2025-03-18 09:21:25 -05:00
2 changed files with 48 additions and 0 deletions

View File

@@ -825,6 +825,18 @@ res.redirect = function redirect(url) {
address = arguments[1]
}
if (!address) {
throw new TypeError('url argument is required to res.redirect');
}
if (typeof address !== 'string') {
throw new TypeError('res.redirect: url must be a string');
}
if (typeof status !== 'number') {
throw new TypeError('res.redirect: status must be a number');
}
// Set location header
address = this.location(address).get('Location');

View File

@@ -19,6 +19,42 @@ describe('res', function(){
.expect(302, done)
})
it('should throw an error if the url is missing', function(done){
var app = express();
app.use(function (req, res) {
res.redirect(undefined)
})
request(app)
.get('/')
.expect(500, /url argument is required to res.redirect/, done)
})
it('should throw an error if the url is not a string', function(done){
var app = express();
app.use(function (req, res) {
res.redirect(['http://google.com'])
})
request(app)
.get('/')
.expect(500, /res.redirect: url must be a string/, done)
})
it('should throw an error if the status is not a number', function(done){
var app = express();
app.use(function (req, res) {
res.redirect("300", 'http://google.com')
})
request(app)
.get('/')
.expect(500, /res.redirect: status must be a number/, done)
})
it('should encode "url"', function (done) {
var app = express()