Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix recursive requester #1430

Merged
merged 2 commits into from Feb 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 14 additions & 25 deletions index.js
Expand Up @@ -54,47 +54,40 @@ function request (uri, options, callback) {
return new request.Request(options)
}

function requester(params) {
if(typeof params.options._requester === 'function') {
return params.options._requester
}
return request
}

request.get = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'GET'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.head = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'HEAD'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.post = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'POST'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.put = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'PUT'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.patch = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'PATCH'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.del = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'DELETE'
return requester(params)(params.uri || null, params.options, params.callback)
return this(params.uri || null, params.options, params.callback)
}

request.jar = function (store) {
Expand Down Expand Up @@ -130,25 +123,21 @@ request.defaults = function (options, requester) {
}

if (isFunction(requester)) {
if (method === self) {
method = requester
} else {
params.options._requester = requester
}
method = requester
}

return method(params.options, params.callback)
}
}

var defaults = wrap(self)
defaults.get = wrap(self.get)
defaults.patch = wrap(self.patch)
defaults.post = wrap(self.post)
defaults.put = wrap(self.put)
defaults.head = wrap(self.head)
defaults.del = wrap(self.del)
defaults.cookie = wrap(self.cookie)
defaults.get = self.get
defaults.patch = self.patch
defaults.post = self.post
defaults.put = self.put
defaults.head = self.head
defaults.del = self.del
defaults.cookie = self.cookie
defaults.jar = self.jar
defaults.defaults = self.defaults
return defaults
Expand Down
29 changes: 29 additions & 0 deletions tests/test-defaults.js
Expand Up @@ -213,6 +213,35 @@ tape('recursive defaults', function(t) {
})
})

tape('recursive defaults requester', function(t) {
t.plan(4)

var defaultsOne = request.defaults({}, function(options, callback) {
var headers = options.headers || {}
headers.foo = 'bar1'
options.headers = headers

request(options, callback)
})
, defaultsTwo = defaultsOne.defaults({}, function(options, callback) {
var headers = options.headers || {}
headers.baz = 'bar2'
options.headers = headers

defaultsOne(options, callback)
})

defaultsOne.get(s.url + '/get_recursive1', function (e, r, b) {
t.equal(e, null)
t.equal(b, 'TESTING!')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.deepEqual(r.request.headers, {foo: 'bar1'})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t.deepEqual(r.request.headers, {baz: 'bar2', foo: 'bar1'})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, you have to love that style of writing tests :) Anyway, not your fault.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup I agree that is pretty disjointed, but thats not from this PR :)

})
})

tape('test custom request handler function', function(t) {
t.plan(2)

Expand Down