3 var test = require('tape');
4 var qs = require('../');
6 test('parse()', function (t) {
7 t.test('parses a simple string', function (st) {
8 st.deepEqual(qs.parse('0=foo'), { '0': 'foo' });
9 st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
10 st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
11 st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
12 st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
13 st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
14 st.deepEqual(qs.parse('foo'), { foo: '' });
15 st.deepEqual(qs.parse('foo='), { foo: '' });
16 st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
17 st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
18 st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
19 st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
20 st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
21 st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
22 st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
23 st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
32 t.test('allows enabling dot notation', function (st) {
33 st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
34 st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
38 t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
39 t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
41 qs.parse('a[b][c][d][e][f][g][h]=i'),
42 { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
43 'defaults to a depth of 5'
46 t.test('only parses one level when depth = 1', function (st) {
47 st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
48 st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
52 t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
54 t.test('parses an explicit array', function (st) {
55 st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
56 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
57 st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
61 t.test('parses a mix of simple and explicit arrays', function (st) {
62 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
63 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
64 st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
65 st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
66 st.deepEqual(qs.parse('a[1]=b&a=c'), { a: ['b', 'c'] });
67 st.deepEqual(qs.parse('a=b&a[1]=c'), { a: ['b', 'c'] });
71 t.test('parses a nested array', function (st) {
72 st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
73 st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
77 t.test('allows to specify array indices', function (st) {
78 st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
79 st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
80 st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
84 t.test('limits specific array indices to 20', function (st) {
85 st.deepEqual(qs.parse('a[20]=a'), { a: ['a'] });
86 st.deepEqual(qs.parse('a[21]=a'), { a: { '21': 'a' } });
90 t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
92 t.test('supports encoded = signs', function (st) {
93 st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
97 t.test('is ok with url encoded strings', function (st) {
98 st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
99 st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
103 t.test('allows brackets in the value', function (st) {
104 st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
105 st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
109 t.test('allows empty values', function (st) {
110 st.deepEqual(qs.parse(''), {});
111 st.deepEqual(qs.parse(null), {});
112 st.deepEqual(qs.parse(undefined), {});
116 t.test('transforms arrays to objects', function (st) {
117 st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
118 st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
119 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
120 st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
121 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
122 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' }] });
124 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
125 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
126 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
127 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
131 t.test('transforms arrays to objects (dot notation)', function (st) {
132 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
133 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
134 st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
135 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
136 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' }] });
137 st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
138 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
139 st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
140 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
141 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' }] });
145 t.test('correctly prunes undefined values when converting an array to an object', function (st) {
146 st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
150 t.test('supports malformed uri characters', function (st) {
151 st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
152 st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
153 st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
157 t.test('doesn\'t produce empty keys', function (st) {
158 st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
162 t.test('cannot access Object prototype', function (st) {
163 qs.parse('constructor[prototype][bad]=bad');
164 qs.parse('bad[constructor][prototype][bad]=bad');
165 st.equal(typeof Object.prototype.bad, 'undefined');
169 t.test('parses arrays of objects', function (st) {
170 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
171 st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
175 t.test('allows for empty strings in arrays', function (st) {
176 st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
177 st.deepEqual(qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true }), { a: ['b', null, 'c', ''] });
178 st.deepEqual(qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true }), { a: ['b', '', 'c', null] });
179 st.deepEqual(qs.parse('a[]=&a[]=b&a[]=c'), { a: ['', 'b', 'c'] });
183 t.test('compacts sparse arrays', function (st) {
184 st.deepEqual(qs.parse('a[10]=1&a[2]=2'), { a: ['2', '1'] });
188 t.test('parses semi-parsed strings', function (st) {
189 st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
190 st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
194 t.test('parses buffers correctly', function (st) {
195 var b = new Buffer('test');
196 st.deepEqual(qs.parse({ a: b }), { a: b });
200 t.test('continues parsing when no parent is found', function (st) {
201 st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
202 st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
203 st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
207 t.test('does not error when parsing a very long array', function (st) {
209 while (Buffer.byteLength(str) < 128 * 1024) {
210 str = str + '&' + str;
213 st.doesNotThrow(function () { qs.parse(str); });
218 t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
219 Object.prototype.crash = '';
220 Array.prototype.crash = '';
221 st.doesNotThrow(qs.parse.bind(null, 'a=b'));
222 st.deepEqual(qs.parse('a=b'), { a: 'b' });
223 st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
224 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
225 delete Object.prototype.crash;
226 delete Array.prototype.crash;
230 t.test('parses a string with an alternative string delimiter', function (st) {
231 st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
235 t.test('parses a string with an alternative RegExp delimiter', function (st) {
236 st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
240 t.test('does not use non-splittable objects as delimiters', function (st) {
241 st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
245 t.test('allows overriding parameter limit', function (st) {
246 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
250 t.test('allows setting the parameter limit to Infinity', function (st) {
251 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
255 t.test('allows overriding array limit', function (st) {
256 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
257 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
258 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
262 t.test('allows disabling array parsing', function (st) {
263 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
267 t.test('parses an object', function (st) {
269 'user[name]': { 'pop[bob]': 3 },
275 name: { 'pop[bob]': 3 },
280 var result = qs.parse(input);
282 st.deepEqual(result, expected);
286 t.test('parses an object in dot notation', function (st) {
288 'user.name': { 'pop[bob]': 3 },
294 name: { 'pop[bob]': 3 },
299 var result = qs.parse(input, { allowDots: true });
301 st.deepEqual(result, expected);
305 t.test('parses an object and not child values', function (st) {
307 'user[name]': { 'pop[bob]': { 'test': 3 } },
313 name: { 'pop[bob]': { 'test': 3 } },
318 var result = qs.parse(input);
320 st.deepEqual(result, expected);
324 t.test('does not blow up when Buffer global is missing', function (st) {
325 var tempBuffer = global.Buffer;
326 delete global.Buffer;
327 var result = qs.parse('a=b&c=d');
328 global.Buffer = tempBuffer;
329 st.deepEqual(result, { a: 'b', c: 'd' });
333 t.test('does not crash when parsing circular references', function (st) {
339 st.doesNotThrow(function () {
340 parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
343 st.equal('foo' in parsed, true, 'parsed has "foo" property');
344 st.equal('bar' in parsed.foo, true);
345 st.equal('baz' in parsed.foo, true);
346 st.equal(parsed.foo.bar, 'baz');
347 st.deepEqual(parsed.foo.baz, a);
351 t.test('parses plain objects correctly', function (st) {
352 var a = Object.create(null);
355 st.deepEqual(qs.parse(a), { b: 'c' });
356 var result = qs.parse({ a: a });
357 st.equal('a' in result, true, 'result has "a" property');
358 st.deepEqual(result.a, a);
362 t.test('parses dates correctly', function (st) {
363 var now = new Date();
364 st.deepEqual(qs.parse({ a: now }), { a: now });
368 t.test('parses regular expressions correctly', function (st) {
370 st.deepEqual(qs.parse({ a: re }), { a: re });
374 t.test('does not allow overwriting prototype properties', function (st) {
375 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
376 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
379 qs.parse('toString', { allowPrototypes: false }),
381 'bare "toString" results in {}'
387 t.test('can allow overwriting prototype properties', function (st) {
388 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
389 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
392 qs.parse('toString', { allowPrototypes: true }),
394 'bare "toString" results in { toString: "" }'
400 t.test('params starting with a closing bracket', function (st) {
401 st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
402 st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
403 st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
407 t.test('params starting with a starting bracket', function (st) {
408 st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
409 st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
410 st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
414 t.test('add keys to objects', function (st) {
416 qs.parse('a[b]=c&a=d'),
417 { a: { b: 'c', d: true } },
418 'can add keys to objects'
422 qs.parse('a[b]=c&a=toString'),
424 'can not overwrite prototype'
428 qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
429 { a: { b: 'c', toString: true } },
430 'can overwrite prototype with allowPrototypes true'
434 qs.parse('a[b]=c&a=toString', { plainObjects: true }),
435 { a: { b: 'c', toString: true } },
436 'can overwrite prototype with plainObjects true'
442 t.test('can return null objects', { skip: !Object.create }, function (st) {
443 var expected = Object.create(null);
444 expected.a = Object.create(null);
446 expected.a.hasOwnProperty = 'd';
447 st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
448 st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
449 var expectedArray = Object.create(null);
450 expectedArray.a = Object.create(null);
451 expectedArray.a['0'] = 'b';
452 expectedArray.a.c = 'd';
453 st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);