Skip to content

Commit

Permalink
change set header function, not call this.request() until call this.e…
Browse files Browse the repository at this point in the history
…nd()
  • Loading branch information
vicanso committed Dec 30, 2015
1 parent 3768360 commit a4ddd6a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ function Request(method, url) {
this._formData = null;
this.method = method;
this.url = url;
this.header = {};
this.header = {
'User-Agent': 'node-superagent/' + pkg.version
};
this.writable = true;
this._redirects = 0;
this.redirects(5);
Expand Down Expand Up @@ -271,7 +273,7 @@ Request.prototype.set = function(field, val){
}

debug('set %s "%s"', field, val);
this.request().setHeader(field, val);
this.header[field] = val;
return this;
};

Expand All @@ -291,7 +293,8 @@ Request.prototype.set = function(field, val){

Request.prototype.unset = function(field){
debug('unset %s', field);
this.request().removeHeader(field);

delete this.header[field];
return this;
};

Expand All @@ -304,7 +307,7 @@ Request.prototype.unset = function(field){
*/

Request.prototype.get = function(field){
return this.request().getHeader(field);
return this.header[field];
};

/**
Expand Down Expand Up @@ -431,9 +434,7 @@ Request.prototype.query = function(val){

Request.prototype.send = function(data){
var obj = isObject(data);
var req = this.request();
var type = req.getHeader('Content-Type');

var type = this.get('Content-Type');
// merge
if (obj && isObject(this._data)) {
for (var key in data) {
Expand All @@ -443,7 +444,7 @@ Request.prototype.send = function(data){
} else if ('string' == typeof data) {
// default to x-www-form-urlencoded
if (!type) this.type('form');
type = req.getHeader('Content-Type');
type = this.get('Content-Type');

// concat &
if ('application/x-www-form-urlencoded' == type) {
Expand Down Expand Up @@ -637,6 +638,13 @@ Request.prototype.redirect = function(res){

delete this.req;

// remove all add header except User-Agent
for (var key in this.header) {
if (key !== 'User-Agent') {
delete this.header[key]
}
}

// redirect
this.url = url;
this._redirectList.push(url);
Expand Down Expand Up @@ -753,8 +761,9 @@ Request.prototype.request = function(){
// add cookies
if (this.cookies) req.setHeader('Cookie', this.cookies);

// set default UA
req.setHeader('User-Agent', 'node-superagent/' + pkg.version);
for (var key in this.header) {
req.setHeader(key, this.header[key]);
}

return req;
};
Expand Down Expand Up @@ -861,7 +870,7 @@ Request.prototype.end = function(fn){

// content-length
if (data && !req.getHeader('Content-Length')) {
this.set('Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data));
req.setHeader('Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data));
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/use.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var setup = require('./support/setup');
var NODE = setup.NODE;
var uri = setup.uri;

var assert = require('assert');
var request = require('../');

describe('request', function(){
this.timeout(10000);
describe('use', function(){
it('should use plugin success', function(done){
var now = '' + Date.now();
function uuid(req){
req.set('X-UUID', now);
return req;
}
function prefix(req){
req.url = uri + req.url
return req;
}
request
.get('/echo')
.use(uuid)
.use(prefix)
.end(function(err, res){
assert(res.statusCode === 200);
assert.equal(res.get('X-UUID'), now);
done();
})
})
})
})

0 comments on commit a4ddd6a

Please sign in to comment.