Skip to content

Commit

Permalink
fix pathname relative redirects. Closes #112
Browse files Browse the repository at this point in the history
probably some other cases we can cover but meh this fixes
what i need for now
  • Loading branch information
tj committed Aug 28, 2012
1 parent e20d384 commit 3259251
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'); });
Expand Down
27 changes: 27 additions & 0 deletions test/node/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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(){
Expand Down

0 comments on commit 3259251

Please sign in to comment.