Skip to content

Commit

Permalink
Merge pull request request#946 from aj0strow/merge-default-headers
Browse files Browse the repository at this point in the history
defaults: merge headers
  • Loading branch information
mikeal committed Jul 7, 2014
2 parents 9961c5a + 9dd741f commit 2a064c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ request.defaults = function (options, requester) {
var def = function (method) {
var d = function (uri, opts, callback) {
var params = initParams(uri, opts, callback)
for (var i in options) {
if (params.options[i] === undefined) params.options[i] = options[i]
Object.keys(options).forEach(function (key) {
if (key !== 'headers' && params.options[key] === undefined) {
params.options[key] = options[key]
}
})
if (options.headers) {
var headers = {}
util._extend(headers, options.headers)
util._extend(headers, params.options.headers)
params.options.headers = headers
}
if(typeof requester === 'function') {
if(method === request) {
Expand Down
17 changes: 17 additions & 0 deletions tests/test-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ s.listen(s.port, function () {
counter += 1;
});

s.on('/merge-headers', function (req, resp) {
assert.equal(req.headers.foo, 'bar')
assert.equal(req.headers.merged, 'yes')
resp.writeHead(200)
resp.end()
});

request.defaults({
headers:{foo:"bar", merged:"no"}
})(s.url + '/merge-headers', {
headers:{merged:"yes"}
}, function (e, r, b){
if (e) throw e
assert.equal(r.statusCode, 200)
counter += 1
});

s.on('/post', function (req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers['content-type'], null);
Expand Down

0 comments on commit 2a064c3

Please sign in to comment.