Skip to content

Commit 6f0302f

Browse files
prafu-amzdougwilson
authored andcommitted
Fix res.redirect body when redirect status specified
fixes #2402 fixes #2404
1 parent 6f91416 commit 6f0302f

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Fix `res.redirect` body when redirect status specified
45
* deps: accepts@~1.1.2
56
- Fix error when media type has invalid parameter
67

lib/response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,11 @@ res.redirect = function redirect(url) {
818818
// Support text/{plain,html} by default
819819
this.format({
820820
text: function(){
821-
body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);
821+
body = statusCodes[status] + '. Redirecting to ' + encodeURI(address);
822822
},
823823

824824
html: function(){
825-
var u = escapeHtml(url);
825+
var u = escapeHtml(address);
826826
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
827827
},
828828

test/res.redirect.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ describe('res', function(){
106106
done();
107107
})
108108
})
109+
110+
it('should include the redirect type', function(done){
111+
var app = express();
112+
113+
app.use(function(req, res){
114+
res.redirect(301, 'http://google.com');
115+
});
116+
117+
request(app)
118+
.get('/')
119+
.set('Accept', 'text/html')
120+
.expect('Content-Type', /html/)
121+
.expect('Location', 'http://google.com')
122+
.expect(301, '<p>Moved Permanently. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);
123+
})
109124
})
110125

111126
describe('when accepting text', function(){
@@ -143,6 +158,21 @@ describe('res', function(){
143158
done();
144159
})
145160
})
161+
162+
it('should include the redirect type', function(done){
163+
var app = express();
164+
165+
app.use(function(req, res){
166+
res.redirect(301, 'http://google.com');
167+
});
168+
169+
request(app)
170+
.get('/')
171+
.set('Accept', 'text/plain, */*')
172+
.expect('Content-Type', /plain/)
173+
.expect('Location', 'http://google.com')
174+
.expect(301, 'Moved Permanently. Redirecting to http://google.com', done);
175+
})
146176
})
147177

148178
describe('when accepting neither text or html', function(){

0 commit comments

Comments
 (0)