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

Modified the post, put, head and del shortcuts to support uri optional param #180

Merged
merged 1 commit into from
Feb 20, 2012
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,20 @@ Request.prototype.destroy = function () {
if (!this._ended) this.end()
}

// organize params for post, put, head, del
function initParams(uri, options, callback) {
if ((typeof options === 'function') && !callback) callback = options;
if (typeof options === 'object') {
options.uri = uri;
} else if (typeof uri === 'string') {
options = {uri:uri};
} else {
options = uri;
uri = options.uri;
}
return { uri: uri, options: options, callback: callback };
}

function request (uri, options, callback) {
if ((typeof options === 'function') && !callback) callback = options;
if (typeof options === 'object') {
Expand Down Expand Up @@ -708,28 +722,28 @@ request.forever = function (agentOptions, optionsArg) {
}

request.get = request
request.post = function (options, callback) {
if (typeof options === 'string') options = {uri:options}
options.method = 'POST'
return request(options, callback)
}
request.put = function (options, callback) {
if (typeof options === 'string') options = {uri:options}
options.method = 'PUT'
return request(options, callback)
}
request.head = function (options, callback) {
if (typeof options === 'string') options = {uri:options}
options.method = 'HEAD'
request.post = function (uri, options, callback) {
var params = initParams(uri, options, callback);
params.options.method = 'POST';
return request(params.uri, params.options, params.callback)
}
request.put = function (uri, options, callback) {
var params = initParams(uri, options, callback);
params.options.method = 'PUT'
return request(params.uri, params.options, params.callback)
}
request.head = function (uri, options, callback) {
var params = initParams(uri, options, callback);
params.options.method = 'HEAD'
if (options.body || options.requestBodyStream || options.json || options.multipart) {
throw new Error("HTTP HEAD requests MUST NOT include a request body.")
}
return request(options, callback)
return request(params.uri, params.options, params.callback)
}
request.del = function (options, callback) {
if (typeof options === 'string') options = {uri:options}
options.method = 'DELETE'
return request(options, callback)
request.del = function (uri, options, callback) {
var params = initParams(uri, options, callback);
params.options.method = 'DELETE'
return request(params.uri, params.options, params.callback)
}
request.jar = function () {
return new CookieJar
Expand Down
2 changes: 1 addition & 1 deletion tests/test-redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ s.listen(s.port, function () {
})

// Should not follow post redirects by default
request.post({uri:server+'/temp', jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
request.post(server+'/temp', { jar: jar, headers: {cookie: 'foo=bar'}}, function (er, res, body) {
try {
assert.ok(hits.temp, 'Original request is to /temp')
assert.ok(!hits.temp_landing, 'No chasing the redirect when post')
Expand Down