From ad97d5ca2cff2d70ef5bf9de4de5c95af668053a Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 13 Apr 2012 09:05:36 -0700 Subject: [PATCH] Fixed: GET / HEAD on redirects. Closes #86 --- lib/node/index.js | 5 ++++- test/node/redirects.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/node/index.js b/lib/node/index.js index f0bf71de7..e2ad2fd37 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -380,8 +380,11 @@ Request.prototype.preventBuffer = function(){ Request.prototype.redirect = function(res){ var url = res.headers.location; delete this.req; - this.emit('redirect', res); + this.method = 'HEAD' == this.method + ? this.method + : 'GET'; this.url = url; + this.emit('redirect', res); this.end(this.callback); return this; }; diff --git a/test/node/redirects.js b/test/node/redirects.js index d3669d976..539632a17 100644 --- a/test/node/redirects.js +++ b/test/node/redirects.js @@ -21,6 +21,10 @@ app.get('/movies/all/0', function(req, res){ res.send('first movie page'); }); +app.post('/movie', function(req, res){ + res.redirect('/movies/all/0'); +}); + app.listen(3003); describe('request', function(){ @@ -66,4 +70,24 @@ describe('request', function(){ }); }) }) + + describe('on POST', function(){ + it('should redirect as GET', function(done){ + var redirects = []; + + request + .post('http://localhost:3003/movie') + .redirects(2) + .on('redirect', function(res){ + redirects.push(res.headers.location); + }) + .end(function(res){ + var arr = []; + arr.push('http://localhost:3003/movies/all/0'); + redirects.should.eql(arr); + res.text.should.equal('first movie page'); + done(); + }); + }) + }) }) \ No newline at end of file