Skip to content

Commit

Permalink
Fix res.redirect body when redirect status specified
Browse files Browse the repository at this point in the history
fixes #2402
fixes #2404
  • Loading branch information
prafu-amz authored and dougwilson committed Oct 18, 2014
1 parent 6f91416 commit 6f0302f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>';
},

Expand Down
30 changes: 30 additions & 0 deletions test/res.redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p>Moved Permanently. Redirecting to <a href="http://google.com">http://google.com</a></p>', done);
})
})

describe('when accepting text', function(){
Expand Down Expand Up @@ -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(){
Expand Down

0 comments on commit 6f0302f

Please sign in to comment.