Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add header persistence on redirects

  • Loading branch information...
commit e36a15f7a977b358f35f09ca1ed3b12ada9a4b73 1 parent 3d1bc09
@tj tj authored
Showing with 67 additions and 3 deletions.
  1. +10 −1 lib/node/index.js
  2. +17 −1 lib/node/utils.js
  3. +40 −1 test/node/redirects.js
View
11 lib/node/index.js
@@ -451,6 +451,7 @@ Request.prototype.parse = function(fn){
Request.prototype.redirect = function(res){
var url = res.headers.location;
+ // location
if (!~url.indexOf('://')) {
if (0 != url.indexOf('//')) {
url = '//' + this.host + url;
@@ -458,14 +459,22 @@ Request.prototype.redirect = function(res){
url = this.protocol + url;
}
+ // strip Content-* related fields
+ // in case of POST etc
+ var header = utils.cleanHeader(this.req._headers);
delete this.req;
+
+ // force GET
this.method = 'HEAD' == this.method
- ? this.method
+ ? 'HEAD'
: 'GET';
+
+ // redirect
this._data = null;
this.url = url;
this._redirectList.push(url);
this.emit('redirect', res);
+ this.set(header);
this.end(this._callback);
return this;
};
View
18 lib/node/utils.js
@@ -129,4 +129,20 @@ exports.unzip = function(req, res){
_on.call(res, type, fn);
}
};
-};
+};
+
+/**
+ * Strip content related fields from `header`.
+ *
+ * @param {Object} header
+ * @return {Object} header
+ * @api private
+ */
+
+exports.cleanHeader = function(header){
+ delete header['content-type'];
+ delete header['content-length'];
+ delete header['transfer-encoding'];
+ delete header['cookie'];
+ return header;
+};
View
41 test/node/redirects.js
@@ -35,6 +35,18 @@ app.get('/relative', function(req, res){
res.send(302);
});
+app.get('/header', function(req, res){
+ res.redirect('/header/2');
+});
+
+app.post('/header', function(req, res){
+ res.redirect('/header/2');
+});
+
+app.get('/header/2', function(req, res){
+ res.send(req.headers);
+});
+
app.listen(3003);
describe('request', function(){
@@ -58,6 +70,33 @@ describe('request', function(){
});
})
+ it('should retain header fields', function(done){
+ request
+ .get('http://localhost:3003/header')
+ .set('X-Foo', 'bar')
+ .end(function(res){
+ res.body.should.have.property('x-foo', 'bar');
+ done();
+ });
+ })
+
+ it('should remove Content-* fields', function(done){
+ request
+ .post('http://localhost:3003/header')
+ .type('txt')
+ .set('X-Foo', 'bar')
+ .set('X-Bar', 'baz')
+ .send('hey')
+ .end(function(res){
+ res.body.should.have.property('x-foo', 'bar');
+ res.body.should.have.property('x-bar', 'baz');
+ res.body.should.not.have.property('content-type');
+ res.body.should.not.have.property('content-length');
+ res.body.should.not.have.property('transfer-encoding');
+ done();
+ });
+ })
+
describe('when relative', function(){
it('should construct the FQDN', function(done){
var redirects = [];
@@ -119,4 +158,4 @@ describe('request', function(){
});
})
})
-})
+})
Please sign in to comment.
Something went wrong with that request. Please try again.