diff --git a/History.md b/History.md index 1db825eb69..7e3f452872 100644 --- a/History.md +++ b/History.md @@ -1,6 +1,7 @@ unreleased ========== + * Fix `res.redirect` body when redirect status specified * deps: accepts@~1.1.2 - Fix error when media type has invalid parameter - deps: negotiator@0.4.9 diff --git a/lib/response.js b/lib/response.js index 32c098af20..c26f68ac8a 100644 --- a/lib/response.js +++ b/lib/response.js @@ -818,11 +818,11 @@ res.redirect = function redirect(url) { // Support text/{plain,html} by default this.format({ text: function(){ - body = statusCodes[status] + '. Redirecting to ' + encodeURI(url); + body = statusCodes[status] + '. Redirecting to ' + encodeURI(address); }, html: function(){ - var u = escapeHtml(url); + var u = escapeHtml(address); body = '

' + statusCodes[status] + '. Redirecting to ' + u + '

'; }, diff --git a/test/res.redirect.js b/test/res.redirect.js index 08ce6e6076..4044ad49ee 100644 --- a/test/res.redirect.js +++ b/test/res.redirect.js @@ -106,6 +106,21 @@ describe('res', function(){ done(); }) }) + + it('should include the redirect type', function(done){ + var app = express(); + + app.use(function(req, res){ + res.redirect(301, 'http://google.com'); + }); + + request(app) + .get('/') + .set('Accept', 'text/html') + .expect('Content-Type', /html/) + .expect('Location', 'http://google.com') + .expect(301, '

Moved Permanently. Redirecting to http://google.com

', done); + }) }) describe('when accepting text', function(){ @@ -143,6 +158,21 @@ describe('res', function(){ done(); }) }) + + it('should include the redirect type', function(done){ + var app = express(); + + app.use(function(req, res){ + res.redirect(301, 'http://google.com'); + }); + + request(app) + .get('/') + .set('Accept', 'text/plain, */*') + .expect('Content-Type', /plain/) + .expect('Location', 'http://google.com') + .expect(301, 'Moved Permanently. Redirecting to http://google.com', done); + }) }) describe('when accepting neither text or html', function(){