Skip to content

Commit

Permalink
Adjusted the url package to encode nested objects and arrays (to help…
Browse files Browse the repository at this point in the history
… address #5065). (#8261)

* Adjusted the url package common code to encode nested objects and arrays properly (to help address #5065).

* Adjusted to make sure a 'hasOwnProperty' param name is handled.
  • Loading branch information
hwillson authored and benjamn committed Feb 8, 2017
1 parent 3801349 commit c7e8706
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/http/httpcall_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ testAsyncMulti("httpcall - params", [
do_test("GET", "/", {foo:"bar", fruit:"apple"}, "/?foo=bar&fruit=apple", "");
do_test("POST", "/", {foo:"bar", fruit:"apple"}, "/", "foo=bar&fruit=apple");
do_test("POST", "/", {foo:"bar", fruit:"apple"}, "/", "foo=bar&fruit=apple");
do_test("GET", "/", {'foo!':"bang!"}, {}, "/?foo%21=bang%21", "");
do_test("POST", "/", {'foo!':"bang!"}, {}, "/", "foo%21=bang%21");
do_test("GET", "/", {'foo?':"bang?"}, {}, "/?foo%3F=bang%3F", "");
do_test("POST", "/", {'foo?':"bang?"}, {}, "/", "foo%3F=bang%3F");
do_test("POST", "/", {foo:"bar", fruit:"apple"}, {
content: "stuff!"}, "/?foo=bar&fruit=apple", "stuff!");
do_test("POST", "/", {foo:"bar", greeting:"Hello World"}, {
Expand Down
7 changes: 6 additions & 1 deletion packages/url/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ Package.onUse(function(api) {
api.addFiles('url_server.js', 'server');
});

// tests are in the http package
Package.onTest(function (api) {
api.use(['tinytest', 'url']);
api.addFiles('url_tests.js');
});

// More tests can be found in the http package
31 changes: 19 additions & 12 deletions packages/url/url_common.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
URL = {};

var encodeString = function(str) {
return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
var encodeString = function (str) {
return encodeURIComponent(str).replace(/\*/g, '%2A');
};


URL._encodeParams = function(params) {
var buf = [];
_.each(params, function(value, key) {
if (buf.length)
buf.push('&');
buf.push(encodeString(key), '=', encodeString(value));
});
return buf.join('').replace(/%20/g, '+');
// Encode URL paramaters into a query string, handling nested objects and
// arrays properly.
URL._encodeParams = function (params, prefix) {
var str = [];
for (var p in params) {
if (Object.prototype.hasOwnProperty.call(params, p)) {
var k = prefix ? prefix + '[' + p + ']' : p, v = params[p];
if (typeof v === 'object') {
str.push(this._encodeParams(v, k));
} else {
var encodedKey =
encodeString(k).replace('%5B', '[').replace('%5D', ']');
str.push(encodedKey + '=' + encodeString(v));
}
}
}
return str.join('&').replace(/%20/g, '+');
};


buildUrl = function(before_qmark, from_qmark, opt_query, opt_params) {
var url_without_query = before_qmark;
var query = from_qmark ? from_qmark.slice(1) : null;
Expand Down
14 changes: 14 additions & 0 deletions packages/url/url_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Tinytest.add('url - serializes params to query correctly', function (test) {
var hash = {
filter: {
type: 'Foo',
id_eq: 15,
},
array: ['1', 'a', 'dirty[]'],
hasOwnProperty: 'horrible param name',
};
var query =
'filter[type]=Foo&filter[id_eq]=15&array[0]=1&array[1]=a'
+ '&array[2]=dirty%5B%5D&hasOwnProperty=horrible+param+name';
test.equal(URL._encodeParams(hash), query);
});

0 comments on commit c7e8706

Please sign in to comment.