diff --git a/lib/response.js b/lib/response.js index 9362d0ed5d..24c0c9c799 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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'); diff --git a/test/res.redirect.js b/test/res.redirect.js index 264e0f2b8f..3295706dc9 100644 --- a/test/res.redirect.js +++ b/test/res.redirect.js @@ -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()