Skip to content
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
45 changes: 35 additions & 10 deletions iron-request.html
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,43 @@
return '';
}
var pieces = [];
Object.keys(object).forEach(function(key) {
// TODO(rictic): handle array values here, in a consistent way with
// iron-ajax params.
pieces.push(
this._wwwFormUrlEncodePiece(key) + '=' +
this._wwwFormUrlEncodePiece(object[key]));
}, this);
pieces = this._wwwFormUrlEncodeRecurrent(null, object , pieces);
return pieces.join('&');
},

/**
* Funcion parse recurrently whole object. Support multi level nodes
*
* @param {String} keyprefix The prefix of key (parent name).
* @param {Object} object The object (or part of it) to encode
* as x-www-form-urlencoded.
* @param {Array} container Array which contains all parsed elements.
* @return {Array} array with parsed element
*/
_wwwFormUrlEncodeRecurrent: function(keyprefix, object, container) {
if(!container){
container = [];
}
if(!object){
return container;
}
Object.keys(object).forEach(function(key) {
var keyWithPrefix;
if(!keyprefix){
keyWithPrefix = key;
}else{
keyWithPrefix = keyprefix + '[' + key + ']';
}
if((Object.keys(object[key]).length > 0) && (typeof object[key] != 'string')){
container = this._wwwFormUrlEncodeRecurrent(keyWithPrefix, object[key], container);
} else {
container.push(
this._wwwFormUrlEncodePiece(keyWithPrefix) + '=' +
this._wwwFormUrlEncodePiece(object[key]));
}
}
, this);
return container;
},
/**
* @param {*} str A key or value to encode as x-www-form-urlencoded.
* @return {string} .
Expand All @@ -433,7 +460,6 @@
return encodeURIComponent(str.toString().replace(/\r?\n/g, '\r\n'))
.replace(/%20/g, '+');
},

/**
* Updates the status code and status text.
*/
Expand All @@ -443,4 +469,3 @@
}
});
</script>

20 changes: 20 additions & 0 deletions test/iron-ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,26 @@
'foo=bar%0D%0Abip&biz+bo=baz+blar');
});

test('if `contentType` is set to form encode, the body is encoded. \
Body Object contains nested objects',function() {
ajax.body = {
arraybar: [1,2,3],
"foo":"bar",
"fo": {
br : 1,
ba : "1",
bb : [],
nc : [1,2,3]
}
};
ajax.contentType = 'application/x-www-form-urlencoded';
ajax.generateRequest();

expect(server.requests[0]).to.be.ok;
expect(server.requests[0].requestBody).to.be.equal(
'arraybar%5B0%5D=1&arraybar%5B1%5D=2&arraybar%5B2%5D=3&foo=bar&fo%5Bbr%5D=1&fo%5Bba%5D=1&fo%5Bbb%5D=&fo%5Bnc%5D%5B0%5D=1&fo%5Bnc%5D%5B1%5D=2&fo%5Bnc%5D%5B2%5D=3');
});

test('if `contentType` is json, the body is json encoded', function() {
var requestObj = {foo: 'bar', baz: [1,2,3]}
ajax.body = requestObj;
Expand Down