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(){