From 32592511699c3ca33384c47144b6fa1af5761d0b Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Tue, 28 Aug 2012 11:36:30 -0700 Subject: [PATCH] fix pathname relative redirects. Closes #112 probably some other cases we can cover but meh this fixes what i need for now --- lib/node/index.js | 10 +++++++++- test/node/redirects.js | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/node/index.js b/lib/node/index.js index a77e8dfbf..4f704ee26 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -403,7 +403,14 @@ Request.prototype.buffer = function(val){ Request.prototype.redirect = function(res){ var url = res.headers.location; - if (!~url.indexOf('://')) url = this.protocol + url; + + if (!~url.indexOf('://')) { + if (0 != url.indexOf('//')) { + url = '//' + this.host + url; + } + url = this.protocol + url; + } + delete this.req; this.method = 'HEAD' == this.method ? this.method @@ -476,6 +483,7 @@ Request.prototype.request = function(){ // request var req = this.req = mod.request(options); this.protocol = url.protocol; + this.host = url.host; // expose events req.on('drain', function(){ self.emit('drain'); }); diff --git a/test/node/redirects.js b/test/node/redirects.js index 183e9b82a..0a0dd7739 100644 --- a/test/node/redirects.js +++ b/test/node/redirects.js @@ -26,6 +26,15 @@ app.post('/movie', function(req, res){ res.redirect('/movies/all/0'); }); +app.get('/tobi', function(req, res){ + res.send('tobi'); +}); + +app.get('/relative', function(req, res){ + res.set('Location', '/tobi'); + res.send(302); +}); + app.listen(3003); describe('request', function(){ @@ -48,6 +57,24 @@ describe('request', function(){ done(); }); }) + + describe('when relative', function(){ + it('should construct the FQDN', function(done){ + var redirects = []; + + request + .get('http://localhost:3003/relative') + .on('redirect', function(res){ + redirects.push(res.headers.location); + }) + .end(function(res){ + var arr = []; + redirects.should.eql(['/tobi']); + res.text.should.equal('tobi'); + done(); + }); + }) + }) }) describe('req.redirects(n)', function(){