Skip to content

Commit

Permalink
Merge pull request #1540 from simov/fix-defaults
Browse files Browse the repository at this point in the history
Fix recursive defaults for convenience methods
  • Loading branch information
simov committed Apr 15, 2015
2 parents 3afa3fa + c98eaa6 commit 6de11c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 4 additions & 5 deletions index.js
Expand Up @@ -75,7 +75,7 @@ request.cookie = function (str) {
return cookies.parse(str)
}

function wrapRequestMethod (method, options, requester) {
function wrapRequestMethod (method, options, requester, verb) {

return function (uri, opts, callback) {
var params = initParams(uri, opts, callback)
Expand All @@ -89,9 +89,8 @@ function wrapRequestMethod (method, options, requester) {
params.headers = extend(headers, params.headers)
}

if (typeof method === 'string') {
params.method = (method === 'del' ? 'DELETE' : method.toUpperCase())
method = request[method]
if (verb) {
params.method = (verb === 'del' ? 'DELETE' : verb.toUpperCase())
}

if (isFunction(requester)) {
Expand All @@ -114,7 +113,7 @@ request.defaults = function (options, requester) {

var verbs = ['get', 'head', 'post', 'put', 'patch', 'del']
verbs.forEach(function(verb) {
defaults[verb] = wrapRequestMethod(verb, options, requester)
defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
})

defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
Expand Down
7 changes: 6 additions & 1 deletion tests/test-defaults.js
Expand Up @@ -208,7 +208,7 @@ tape('head(object, function)', function(t) {
})

tape('recursive defaults', function(t) {
t.plan(6)
t.plan(8)

var defaultsOne = request.defaults({ headers: { foo: 'bar1' } })
, defaultsTwo = defaultsOne.defaults({ headers: { baz: 'bar2' } })
Expand All @@ -234,6 +234,11 @@ tape('recursive defaults', function(t) {
t.equal(e, null)
t.equal(b, 'TESTING!')
})

defaultsTwo.get(s.url + '/get_recursive2', function (e, r, b) {
t.equal(e, null)
t.equal(b, 'TESTING!')
})
})

tape('recursive defaults requester', function(t) {
Expand Down

0 comments on commit 6de11c0

Please sign in to comment.