diff --git a/lib/response.js b/lib/response.js index e3c309744fb..3c2101da77e 100644 --- a/lib/response.js +++ b/lib/response.js @@ -856,7 +856,9 @@ res.redirect = function redirect(url) { // Respond this.status(status); - this.set('Content-Length', Buffer.byteLength(body)); + if (!this.get('Transfer-Encoding')) { + this.set('Content-Length', Buffer.byteLength(body)); + } if (this.req.method === 'HEAD') { this.end(); diff --git a/test/res.redirect.js b/test/res.redirect.js index 8d2b164e4a2..2b4838dc04f 100644 --- a/test/res.redirect.js +++ b/test/res.redirect.js @@ -211,4 +211,23 @@ describe('res', function(){ .end(done) }) }) + + describe('when Transfer-Encoding is set', function(){ + it('should not set Content-Length', function(done){ + var app = express(); + + app.use(function(req, res){ + res.set('Transfer-Encoding', 'chunked'); + res.redirect('http://google.com'); + }); + + request(app) + .get('/') + .set('Accept', 'text/plain, */*') + .expect(utils.shouldNotHaveHeader('Content-Length')) + .expect(utils.shouldHaveHeader('Transfer-Encoding')) + .expect('Location', 'http://google.com') + .expect(302, 'Found. Redirecting to http://google.com', done); + }) + }) })