From 6f0302fb78f636da320d3ffd329c18fed36aeda9 Mon Sep 17 00:00:00 2001 From: Alex Upadhyay Date: Sat, 18 Oct 2014 01:12:47 +0000 Subject: [PATCH] Fix res.redirect body when redirect status specified fixes #2402 fixes #2404 --- History.md | 1 + lib/response.js | 4 ++-- test/res.redirect.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 1db825eb..7e3f4528 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 32c098af..c26f68ac 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 08ce6e60..4044ad49 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(){