3 var test = require('tape');
4 var qs = require('../');
5 var iconv = require('iconv-lite');
7 test('parse()', function (t) {
8 t.test('parses a simple string', function (st) {
9 st.deepEqual(qs.parse('0=foo'), { '0': 'foo' });
10 st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
11 st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
12 st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
13 st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
14 st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
15 st.deepEqual(qs.parse('foo'), { foo: '' });
16 st.deepEqual(qs.parse('foo='), { foo: '' });
17 st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
18 st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
19 st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
20 st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
21 st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
22 st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
23 st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
24 st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
33 t.test('allows enabling dot notation', function (st) {
34 st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
35 st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
39 t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
40 t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
42 qs.parse('a[b][c][d][e][f][g][h]=i'),
43 { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
44 'defaults to a depth of 5'
47 t.test('only parses one level when depth = 1', function (st) {
48 st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
49 st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
53 t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
55 t.test('parses an explicit array', function (st) {
56 st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
57 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
58 st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
62 t.test('parses a mix of simple and explicit arrays', function (st) {
63 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
64 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
65 st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
66 st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
67 st.deepEqual(qs.parse('a[1]=b&a=c'), { a: ['b', 'c'] });
68 st.deepEqual(qs.parse('a=b&a[1]=c'), { a: ['b', 'c'] });
72 t.test('parses a nested array', function (st) {
73 st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
74 st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
78 t.test('allows to specify array indices', function (st) {
79 st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
80 st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
81 st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
85 t.test('limits specific array indices to 20', function (st) {
86 st.deepEqual(qs.parse('a[20]=a'), { a: ['a'] });
87 st.deepEqual(qs.parse('a[21]=a'), { a: { '21': 'a' } });
91 t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
93 t.test('supports encoded = signs', function (st) {
94 st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
98 t.test('is ok with url encoded strings', function (st) {
99 st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
100 st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
104 t.test('allows brackets in the value', function (st) {
105 st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
106 st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
110 t.test('allows empty values', function (st) {
111 st.deepEqual(qs.parse(''), {});
112 st.deepEqual(qs.parse(null), {});
113 st.deepEqual(qs.parse(undefined), {});
117 t.test('transforms arrays to objects', function (st) {
118 st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
119 st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
120 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
121 st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
122 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
123 st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
125 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { '0': 'b', c: true, t: 'u' } });
126 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } });
127 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { '0': 'b', '1': 'c', x: 'y' } });
128 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } });
132 t.test('transforms arrays to objects (dot notation)', function (st) {
133 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
134 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
135 st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
136 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
137 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15', '16'], bar: '2' }] });
138 st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
139 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
140 st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
141 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
142 st.deepEqual(qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb', { allowDots: true }), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
146 t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');
148 t.test('correctly prunes undefined values when converting an array to an object', function (st) {
149 st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
153 t.test('supports malformed uri characters', function (st) {
154 st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
155 st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
156 st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
160 t.test('doesn\'t produce empty keys', function (st) {
161 st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
165 t.test('cannot access Object prototype', function (st) {
166 qs.parse('constructor[prototype][bad]=bad');
167 qs.parse('bad[constructor][prototype][bad]=bad');
168 st.equal(typeof Object.prototype.bad, 'undefined');
172 t.test('parses arrays of objects', function (st) {
173 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
174 st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
178 t.test('allows for empty strings in arrays', function (st) {
179 st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
182 qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
183 { a: ['b', null, 'c', ''] },
184 'with arrayLimit 20 + array indices: null then empty string works'
187 qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
188 { a: ['b', null, 'c', ''] },
189 'with arrayLimit 0 + array brackets: null then empty string works'
193 qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
194 { a: ['b', '', 'c', null] },
195 'with arrayLimit 20 + array indices: empty string then null works'
198 qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
199 { a: ['b', '', 'c', null] },
200 'with arrayLimit 0 + array brackets: empty string then null works'
204 qs.parse('a[]=&a[]=b&a[]=c'),
205 { a: ['', 'b', 'c'] },
206 'array brackets: empty strings work'
211 t.test('compacts sparse arrays', function (st) {
212 st.deepEqual(qs.parse('a[10]=1&a[2]=2'), { a: ['2', '1'] });
213 st.deepEqual(qs.parse('a[1][b][2][c]=1'), { a: [{ b: [{ c: '1' }] }] });
214 st.deepEqual(qs.parse('a[1][2][3][c]=1'), { a: [[[{ c: '1' }]]] });
215 st.deepEqual(qs.parse('a[1][2][3][c][1]=1'), { a: [[[{ c: ['1'] }]]] });
219 t.test('parses semi-parsed strings', function (st) {
220 st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
221 st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
225 t.test('parses buffers correctly', function (st) {
226 var b = new Buffer('test');
227 st.deepEqual(qs.parse({ a: b }), { a: b });
231 t.test('continues parsing when no parent is found', function (st) {
232 st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
233 st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
234 st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
238 t.test('does not error when parsing a very long array', function (st) {
240 while (Buffer.byteLength(str) < 128 * 1024) {
241 str = str + '&' + str;
244 st.doesNotThrow(function () { qs.parse(str); });
249 t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
250 Object.prototype.crash = '';
251 Array.prototype.crash = '';
252 st.doesNotThrow(qs.parse.bind(null, 'a=b'));
253 st.deepEqual(qs.parse('a=b'), { a: 'b' });
254 st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
255 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
256 delete Object.prototype.crash;
257 delete Array.prototype.crash;
261 t.test('parses a string with an alternative string delimiter', function (st) {
262 st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
266 t.test('parses a string with an alternative RegExp delimiter', function (st) {
267 st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
271 t.test('does not use non-splittable objects as delimiters', function (st) {
272 st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
276 t.test('allows overriding parameter limit', function (st) {
277 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
281 t.test('allows setting the parameter limit to Infinity', function (st) {
282 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
286 t.test('allows overriding array limit', function (st) {
287 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
288 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
289 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
293 t.test('allows disabling array parsing', function (st) {
294 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
298 t.test('parses an object', function (st) {
300 'user[name]': { 'pop[bob]': 3 },
306 name: { 'pop[bob]': 3 },
311 var result = qs.parse(input);
313 st.deepEqual(result, expected);
317 t.test('parses an object in dot notation', function (st) {
319 'user.name': { 'pop[bob]': 3 },
325 name: { 'pop[bob]': 3 },
330 var result = qs.parse(input, { allowDots: true });
332 st.deepEqual(result, expected);
336 t.test('parses an object and not child values', function (st) {
338 'user[name]': { 'pop[bob]': { 'test': 3 } },
344 name: { 'pop[bob]': { 'test': 3 } },
349 var result = qs.parse(input);
351 st.deepEqual(result, expected);
355 t.test('does not blow up when Buffer global is missing', function (st) {
356 var tempBuffer = global.Buffer;
357 delete global.Buffer;
358 var result = qs.parse('a=b&c=d');
359 global.Buffer = tempBuffer;
360 st.deepEqual(result, { a: 'b', c: 'd' });
364 t.test('does not crash when parsing circular references', function (st) {
370 st.doesNotThrow(function () {
371 parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
374 st.equal('foo' in parsed, true, 'parsed has "foo" property');
375 st.equal('bar' in parsed.foo, true);
376 st.equal('baz' in parsed.foo, true);
377 st.equal(parsed.foo.bar, 'baz');
378 st.deepEqual(parsed.foo.baz, a);
382 t.test('parses plain objects correctly', function (st) {
383 var a = Object.create(null);
386 st.deepEqual(qs.parse(a), { b: 'c' });
387 var result = qs.parse({ a: a });
388 st.equal('a' in result, true, 'result has "a" property');
389 st.deepEqual(result.a, a);
393 t.test('parses dates correctly', function (st) {
394 var now = new Date();
395 st.deepEqual(qs.parse({ a: now }), { a: now });
399 t.test('parses regular expressions correctly', function (st) {
401 st.deepEqual(qs.parse({ a: re }), { a: re });
405 t.test('can allow overwriting prototype properties', function (st) {
406 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } }, { prototype: false });
407 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' }, { prototype: false });
411 t.test('can return plain objects', function (st) {
412 var expected = Object.create(null);
413 expected.a = Object.create(null);
415 expected.a.hasOwnProperty = 'd';
416 st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
417 st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
418 var expectedArray = Object.create(null);
419 expectedArray.a = Object.create(null);
420 expectedArray.a['0'] = 'b';
421 expectedArray.a.c = 'd';
422 st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
426 t.test('can parse with custom encoding', function (st) {
427 st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
428 decoder: function (str) {
429 var reg = /\%([0-9A-F]{2})/ig;
433 while (parts = reg.exec(str)) {
434 result.push(parseInt(parts[1], 16));
435 last = parts.index + parts[0].length;
437 return iconv.decode(new Buffer(result), 'shift_jis').toString();
443 t.test('throws error with wrong decoder', function (st) {
444 st.throws(function () {
448 }, new TypeError('Decoder has to be a function.'));