diff --git a/lib/querystring.js b/lib/querystring.js index e58cc426f..a25e78f1b 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -26,7 +26,7 @@ function stringifyArray(arr, prefix) { for (var i = 0, len = arr.length; i < len; i++) { if (prefix) - ret.push(stringify(arr[i], prefix + '[]')); + ret.push(stringify(arr[i], prefix + '[' + i + ']')); else ret.push(stringify(arr[i])); } diff --git a/test/querystring_spec.js b/test/querystring_spec.js index 34c1748c6..06057cf9a 100644 --- a/test/querystring_spec.js +++ b/test/querystring_spec.js @@ -109,7 +109,7 @@ describe('stringify', function() { it('works', function() { var res = stringify({ foo: ['bar', 'baz'] }); - res.should.eql('foo[]=bar&foo[]=baz'); + res.should.eql('foo[0]=bar&foo[1]=baz'); }) }) @@ -118,11 +118,48 @@ describe('stringify', function() { it('works', function() { var res = stringify({ foo: [{'1': 'bar'}, {'2': 'baz'}] }); - res.should.eql('foo[][1]=bar&foo[][2]=baz'); + res.should.eql('foo[0][1]=bar&foo[1][2]=baz'); }) }) }) + describe('with a complex object', function() { + + var data = { + field1: 'x', + field3: [ + { + subfield1: 'this is a string', + subfield2: ['this is array'], + subfield3: ['another array'] + }, + { + subfield1: 'this is a string', + subfield2: ['this is array'], + subfield3: ['another array'] + }, + { + subfield1: 'this is a string', + subfield2: ['this is array'], + subfield3: ['another array'] + }, + ] + }; + + // TODO + it('works as expected', function() { + var encoded = stringify(data) + encoded.should.eql('field1=x&field3[0][subfield1]=this%20is%20a%20string&field3[0][subfield2][0]=this%20is%20array&field3[0][subfield3][0]=another%20array&field3[1][subfield1]=this%20is%20a%20string&field3[1][subfield2][0]=this%20is%20array&field3[1][subfield3][0]=another%20array&field3[2][subfield1]=this%20is%20a%20string&field3[2][subfield2][0]=this%20is%20array&field3[2][subfield3][0]=another%20array') + + // console.log(data); + // console.log(encoded.replace(/\&/g, '\n')); + + // var qs = require("querystring"); + // var res = qs.decode(encoded); + // console.log(res); + }) + }) + })