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

Prevent options object from being mutated. Fixes #230. #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions lib/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ mixin(Request.prototype, {
}
});

function shortcutOptions(options, method) {
options = options || {};
function shortcutOptions(opts, method) {
var options = mixin({}, opts);
options.method = method;
options.parser = (typeof options.parser !== "undefined") ? options.parser : parsers.auto;
parsers.xml.options = (typeof options.xml2js == 'undefined') ? {} : options.xml2js;
Expand Down Expand Up @@ -349,8 +349,8 @@ function head(url, options) {
return request(url, shortcutOptions(options, 'HEAD'));
}

function json(url, data, options, method) {
options = options || {};
function json(url, data, opts, method) {
var options = mixin({}, opts);
options.parser = (typeof options.parser !== "undefined") ? options.parser : parsers.auto;
options.headers = options.headers || {};
options.headers['content-type'] = 'application/json';
Expand Down
18 changes: 18 additions & 0 deletions test/restler.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,24 @@ module.exports['Deserialization'] = {
}).on('fail', function() {
test.ok(false, 'should not have got here');
});
},

'Should parse JSON without mutating options': function(test) {
var options = {};
rest.get(host + '/json', options).on('complete', function(data) {
test.equal(data.ok, true, 'returned: ' + util.inspect(data));
test.deepEqual({}, options, 'mutated: ' + util.inspect(options));
test.done();
});
},

'Should post and parse JSON without mutating options': function(test) {
var obj = { secret : 'very secret string' }, options = {};
rest.json(host + '/push-json', obj, options, 'POST').on('complete', function(data) {
test.equal(obj.secret, data.secret, 'returned: ' + util.inspect(data));
test.deepEqual({}, options, 'mutated: ' + util.inspect(options));
test.done();
});
}

};
Expand Down