source: frontend/node_modules/qs/test/parse.js@ cdcff72

Last change on this file since cdcff72 was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 66.5 KB
Line 
1'use strict';
2
3var test = require('tape');
4var hasPropertyDescriptors = require('has-property-descriptors')();
5var iconv = require('iconv-lite');
6var mockProperty = require('mock-property');
7var hasOverrideMistake = require('has-override-mistake')();
8var SaferBuffer = require('safer-buffer').Buffer;
9var v = require('es-value-fixtures');
10var inspect = require('object-inspect');
11var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
12var hasProto = require('has-proto')();
13
14var qs = require('../');
15var utils = require('../lib/utils');
16
17test('parse()', function (t) {
18 t.test('parses a simple string', function (st) {
19 st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
20 st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
21 st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
22 st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
23 st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
24 st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
25 st.deepEqual(qs.parse('foo'), { foo: '' });
26 st.deepEqual(qs.parse('foo='), { foo: '' });
27 st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
28 st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
29 st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
30 st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
31 st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
32 st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
33 st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
34 st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
35 cht: 'p3',
36 chd: 't:60,40',
37 chs: '250x100',
38 chl: 'Hello|World'
39 });
40 st.end();
41 });
42
43 t.test('comma: false', function (st) {
44 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
45 st.deepEqual(qs.parse('a[0]=b&a[1]=c'), { a: ['b', 'c'] });
46 st.deepEqual(qs.parse('a=b,c'), { a: 'b,c' });
47 st.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] });
48 st.end();
49 });
50
51 t.test('comma: true', function (st) {
52 st.deepEqual(qs.parse('a[]=b&a[]=c', { comma: true }), { a: ['b', 'c'] });
53 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { comma: true }), { a: ['b', 'c'] });
54 st.deepEqual(qs.parse('a=b,c', { comma: true }), { a: ['b', 'c'] });
55 st.deepEqual(qs.parse('a=b&a=c', { comma: true }), { a: ['b', 'c'] });
56 st.end();
57 });
58
59 t.test('allows enabling dot notation', function (st) {
60 st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
61 st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
62
63 st.end();
64 });
65
66 t.test('decode dot keys correctly', function (st) {
67 st.deepEqual(
68 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: false, decodeDotInKeys: false }),
69 { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
70 'with allowDots false and decodeDotInKeys false'
71 );
72 st.deepEqual(
73 qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
74 { name: { obj: { first: 'John', last: 'Doe' } } },
75 'with allowDots false and decodeDotInKeys false'
76 );
77 st.deepEqual(
78 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
79 { 'name%2Eobj': { first: 'John', last: 'Doe' } },
80 'with allowDots true and decodeDotInKeys false'
81 );
82 st.deepEqual(
83 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: true }),
84 { 'name.obj': { first: 'John', last: 'Doe' } },
85 'with allowDots true and decodeDotInKeys true'
86 );
87
88 st.deepEqual(
89 qs.parse(
90 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
91 { allowDots: false, decodeDotInKeys: false }
92 ),
93 { 'name%2Eobj%2Esubobject.first%2Egodly%2Ename': 'John', 'name%2Eobj%2Esubobject.last': 'Doe' },
94 'with allowDots false and decodeDotInKeys false'
95 );
96 st.deepEqual(
97 qs.parse(
98 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
99 { allowDots: true, decodeDotInKeys: false }
100 ),
101 { name: { obj: { subobject: { first: { godly: { name: 'John' } }, last: 'Doe' } } } },
102 'with allowDots true and decodeDotInKeys false'
103 );
104 st.deepEqual(
105 qs.parse(
106 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
107 { allowDots: true, decodeDotInKeys: true }
108 ),
109 { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
110 'with allowDots true and decodeDotInKeys true'
111 );
112 st.deepEqual(
113 qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe'),
114 { 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
115 'with allowDots and decodeDotInKeys undefined'
116 );
117
118 st.end();
119 });
120
121 t.test('decodes dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) {
122 st.deepEqual(
123 qs.parse(
124 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
125 { decodeDotInKeys: true }
126 ),
127 { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
128 'with allowDots undefined and decodeDotInKeys true'
129 );
130
131 st.end();
132 });
133
134 t.test('throws when decodeDotInKeys is not of type boolean', function (st) {
135 st['throws'](
136 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); },
137 TypeError
138 );
139
140 st['throws'](
141 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 0 }); },
142 TypeError
143 );
144 st['throws'](
145 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: NaN }); },
146 TypeError
147 );
148
149 st['throws'](
150 function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: null }); },
151 TypeError
152 );
153
154 st.end();
155 });
156
157 t.test('allows empty arrays in obj values', function (st) {
158 st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });
159 st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });
160
161 st.end();
162 });
163
164 t.test('throws when allowEmptyArrays is not of type boolean', function (st) {
165 st['throws'](
166 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 'foobar' }); },
167 TypeError
168 );
169
170 st['throws'](
171 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 0 }); },
172 TypeError
173 );
174 st['throws'](
175 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: NaN }); },
176 TypeError
177 );
178
179 st['throws'](
180 function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: null }); },
181 TypeError
182 );
183
184 st.end();
185 });
186
187 t.test('allowEmptyArrays + strictNullHandling', function (st) {
188 st.deepEqual(
189 qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),
190 { testEmptyArray: [] }
191 );
192
193 st.end();
194 });
195
196 t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
197 t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
198 t.deepEqual(
199 qs.parse('a[b][c][d][e][f][g][h]=i'),
200 { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
201 'defaults to a depth of 5'
202 );
203
204 t.test('only parses one level when depth = 1', function (st) {
205 st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
206 st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
207 st.end();
208 });
209
210 t.test('uses original key when depth = 0', function (st) {
211 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
212 st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
213 st.deepEqual(qs.parse('a.b=c', { depth: 0, allowDots: true }), { 'a[b]': 'c' }, 'normalizes dots before applying depth-0 behavior');
214 st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {}, 'respects prototype guard at depth 0');
215 st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' }, 'allows prototypes at depth 0 when enabled');
216 st.end();
217 });
218
219 t.test('ignores prototype keys when depth = 0 and allowPrototypes is false', function (st) {
220 st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {});
221 st.deepEqual(qs.parse('hasOwnProperty=bar', { depth: 0 }), {});
222 st.deepEqual(qs.parse('toString=foo&a=b', { depth: 0 }), { a: 'b' });
223 st.end();
224 });
225
226 t.test('allows prototype keys when depth = 0 and allowPrototypes is true', function (st) {
227 st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' });
228 st.end();
229 });
230
231 t.test('uses original key when depth = false', function (st) {
232 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
233 st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
234 st.end();
235 });
236
237 t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
238
239 t.test('parses an explicit array', function (st) {
240 st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
241 st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
242 st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
243 st.end();
244 });
245
246 t.test('parses a mix of simple and explicit arrays', function (st) {
247 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
248 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
249 st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
250 st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
251
252 st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
253 st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
254 st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
255
256 st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
257 st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
258 st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
259
260 st.end();
261 });
262
263 t.test('parses a nested array', function (st) {
264 st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
265 st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
266 st.end();
267 });
268
269 t.test('parses keys with literal [] inside a bracket group (#493)', function (st) {
270 // A bracket pair inside a bracket group should be treated literally as part of the key
271 st.deepEqual(
272 qs.parse('search[withbracket[]]=foobar'),
273 { search: { 'withbracket[]': 'foobar' } },
274 'treats inner [] literally when inside a bracket group'
275 );
276
277 // Single-level variant
278 st.deepEqual(
279 qs.parse('a[b[]]=c'),
280 { a: { 'b[]': 'c' } },
281 'keeps "b[]" as a literal key'
282 );
283
284 // Nested with an array push on the outer level
285 st.deepEqual(
286 qs.parse('list[][x[]]=y'),
287 { list: [{ 'x[]': 'y' }] },
288 'preserves inner [] while still treating outer [] as array push'
289 );
290
291 // Multiple nested bracket pairs: inner [] remains literal as part of the key
292 st.deepEqual(
293 qs.parse('a[b[c[]]]=d'),
294 { a: { 'b[c[]]': 'd' } },
295 'treats "b[c[]]" as a literal key inside the bracket group'
296 );
297
298 // Depth limits with literal brackets: preserve inner [] while limiting bracket-group parsing
299 st.deepEqual(
300 qs.parse('a[b[c[]]][d]=e', { depth: 1 }),
301 { a: { 'b[c[]]': { '[d]': 'e' } } },
302 'respects depth: 1 and preserves literal inner [] in the parsed key'
303 );
304
305 // Unterminated inner bracket group is wrapped as a literal remainder segment
306 st.deepEqual(
307 qs.parse('a[[]b=c'),
308 { a: { '[[]b': 'c' } },
309 'handles unterminated inner bracket groups without throwing'
310 );
311
312 st.end();
313 });
314
315 t.test('allows to specify array indices', function (st) {
316 st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
317 st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
318 st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
319 st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
320 st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
321 st.end();
322 });
323
324 t.test('limits specific array indices to arrayLimit', function (st) {
325 st.deepEqual(qs.parse('a[19]=a', { arrayLimit: 20 }), { a: ['a'] });
326 st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: { 20: 'a' } });
327
328 st.deepEqual(qs.parse('a[19]=a'), { a: ['a'] });
329 st.deepEqual(qs.parse('a[20]=a'), { a: { 20: 'a' } });
330 st.end();
331 });
332
333 t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
334
335 t.test('supports encoded = signs', function (st) {
336 st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
337 st.end();
338 });
339
340 t.test('is ok with url encoded strings', function (st) {
341 st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
342 st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
343 st.end();
344 });
345
346 t.test('allows brackets in the value', function (st) {
347 st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
348 st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
349 st.end();
350 });
351
352 t.test('allows empty values', function (st) {
353 st.deepEqual(qs.parse(''), {});
354 st.deepEqual(qs.parse(null), {});
355 st.deepEqual(qs.parse(undefined), {});
356 st.end();
357 });
358
359 t.test('transforms arrays to objects', function (st) {
360 st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
361 st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
362 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
363 st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
364 st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
365 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' }] });
366
367 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
368 st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
369 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
370 st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
371 st.end();
372 });
373
374 t.test('transforms arrays to objects (dot notation)', function (st) {
375 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
376 st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
377 st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
378 st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
379 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' }] });
380 st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
381 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
382 st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
383 st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
384 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' }] });
385 st.end();
386 });
387
388 t.test('correctly prunes undefined values when converting an array to an object', function (st) {
389 st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
390 st.end();
391 });
392
393 t.test('supports malformed uri characters', function (st) {
394 st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
395 st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
396 st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
397 st.end();
398 });
399
400 t.test('doesn\'t produce empty keys', function (st) {
401 st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
402 st.end();
403 });
404
405 t.test('cannot access Object prototype', function (st) {
406 qs.parse('constructor[prototype][bad]=bad');
407 qs.parse('bad[constructor][prototype][bad]=bad');
408 st.equal(typeof Object.prototype.bad, 'undefined');
409 st.end();
410 });
411
412 t.test('parses arrays of objects', function (st) {
413 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
414 st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
415 st.end();
416 });
417
418 t.test('allows for empty strings in arrays', function (st) {
419 st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
420
421 st.deepEqual(
422 qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
423 { a: ['b', null, 'c', ''] },
424 'with arrayLimit 20 + array indices: null then empty string works'
425 );
426 st.deepEqual(
427 qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
428 { a: { 0: 'b', 1: null, 2: 'c', 3: '' } },
429 'with arrayLimit 0 + array brackets: null then empty string works'
430 );
431
432 st.deepEqual(
433 qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
434 { a: ['b', '', 'c', null] },
435 'with arrayLimit 20 + array indices: empty string then null works'
436 );
437 st.deepEqual(
438 qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
439 { a: { 0: 'b', 1: '', 2: 'c', 3: null } },
440 'with arrayLimit 0 + array brackets: empty string then null works'
441 );
442
443 st.deepEqual(
444 qs.parse('a[]=&a[]=b&a[]=c'),
445 { a: ['', 'b', 'c'] },
446 'array brackets: empty strings work'
447 );
448 st.end();
449 });
450
451 t.test('compacts sparse arrays', function (st) {
452 st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
453 st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
454 st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
455 st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
456 st.end();
457 });
458
459 t.test('parses sparse arrays', function (st) {
460 /* eslint no-sparse-arrays: 0 */
461 st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
462 st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
463 st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
464 st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
465 st.end();
466 });
467
468 t.test('parses semi-parsed strings', function (st) {
469 st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
470 st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
471 st.end();
472 });
473
474 t.test('parses buffers correctly', function (st) {
475 var b = SaferBuffer.from('test');
476 st.deepEqual(qs.parse({ a: b }), { a: b });
477 st.end();
478 });
479
480 t.test('parses jquery-param strings', function (st) {
481 // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
482 var encoded = 'filter%5B0%5D%5B%5D=int1&filter%5B0%5D%5B%5D=%3D&filter%5B0%5D%5B%5D=77&filter%5B%5D=and&filter%5B2%5D%5B%5D=int2&filter%5B2%5D%5B%5D=%3D&filter%5B2%5D%5B%5D=8';
483 var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
484 st.deepEqual(qs.parse(encoded), expected);
485 st.end();
486 });
487
488 t.test('continues parsing when no parent is found', function (st) {
489 st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
490 st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
491 st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
492 st.end();
493 });
494
495 t.test('does not error when parsing a very long array', function (st) {
496 var str = 'a[]=a';
497 while (Buffer.byteLength(str) < 128 * 1024) {
498 str = str + '&' + str;
499 }
500
501 st.doesNotThrow(function () {
502 qs.parse(str);
503 });
504
505 st.end();
506 });
507
508 t.test('does not throw when a native prototype has an enumerable property', function (st) {
509 st.intercept(Object.prototype, 'crash', { value: '' });
510 st.intercept(Array.prototype, 'crash', { value: '' });
511
512 st.doesNotThrow(qs.parse.bind(null, 'a=b'));
513 st.deepEqual(qs.parse('a=b'), { a: 'b' });
514 st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
515 st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
516
517 st.end();
518 });
519
520 t.test('parses a string with an alternative string delimiter', function (st) {
521 st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
522 st.end();
523 });
524
525 t.test('parses a string with an alternative RegExp delimiter', function (st) {
526 st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
527 st.end();
528 });
529
530 t.test('does not use non-splittable objects as delimiters', function (st) {
531 st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
532 st.end();
533 });
534
535 t.test('allows overriding parameter limit', function (st) {
536 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
537 st.end();
538 });
539
540 t.test('allows setting the parameter limit to Infinity', function (st) {
541 st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
542 st.end();
543 });
544
545 t.test('allows overriding array limit', function (st) {
546 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
547 st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: { 0: 'b' } });
548
549 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
550 st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });
551
552 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: -1 }), { a: { 0: 'b', 1: 'c' } });
553 st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
554
555 st.end();
556 });
557
558 t.test('allows disabling array parsing', function (st) {
559 var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
560 st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
561 st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
562
563 var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
564 st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
565 st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
566
567 st.end();
568 });
569
570 t.test('allows for query string prefix', function (st) {
571 st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
572 st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
573 st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
574
575 st.end();
576 });
577
578 t.test('parses an object', function (st) {
579 var input = {
580 'user[name]': { 'pop[bob]': 3 },
581 'user[email]': null
582 };
583
584 var expected = {
585 user: {
586 name: { 'pop[bob]': 3 },
587 email: null
588 }
589 };
590
591 var result = qs.parse(input);
592
593 st.deepEqual(result, expected);
594 st.end();
595 });
596
597 t.test('parses string with comma as array divider', function (st) {
598 st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
599 st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
600 st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
601 st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
602 st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
603
604 // test cases inversed from from stringify tests
605 st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });
606 st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });
607 st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });
608
609 st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });
610 st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });
611 st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });
612
613 st.end();
614 });
615
616 t.test('parses values with comma as array divider', function (st) {
617 st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
618 st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
619 st.end();
620 });
621
622 t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
623 var decoder = function (str, defaultDecoder, charset, type) {
624 if (!isNaN(Number(str))) {
625 return parseFloat(str);
626 }
627 return defaultDecoder(str, defaultDecoder, charset, type);
628 };
629
630 st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
631 st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
632
633 st.end();
634 });
635
636 t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
637 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
638 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
639 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
640 st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
641
642 st.end();
643 });
644
645 t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
646 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
647 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
648 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
649 st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
650
651 st.end();
652 });
653
654 t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
655 st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
656 st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
657 st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
658
659 st.end();
660 });
661
662 t.test('parses an object in dot notation', function (st) {
663 var input = {
664 'user.name': { 'pop[bob]': 3 },
665 'user.email.': null
666 };
667
668 var expected = {
669 user: {
670 name: { 'pop[bob]': 3 },
671 email: null
672 }
673 };
674
675 var result = qs.parse(input, { allowDots: true });
676
677 st.deepEqual(result, expected);
678 st.end();
679 });
680
681 t.test('parses an object and not child values', function (st) {
682 var input = {
683 'user[name]': { 'pop[bob]': { test: 3 } },
684 'user[email]': null
685 };
686
687 var expected = {
688 user: {
689 name: { 'pop[bob]': { test: 3 } },
690 email: null
691 }
692 };
693
694 var result = qs.parse(input);
695
696 st.deepEqual(result, expected);
697 st.end();
698 });
699
700 t.test('does not blow up when Buffer global is missing', function (st) {
701 var restore = mockProperty(global, 'Buffer', { 'delete': true });
702
703 var result = qs.parse('a=b&c=d');
704
705 restore();
706
707 st.deepEqual(result, { a: 'b', c: 'd' });
708 st.end();
709 });
710
711 t.test('does not crash when parsing circular references', function (st) {
712 var a = {};
713 a.b = a;
714
715 var parsed;
716
717 st.doesNotThrow(function () {
718 parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
719 });
720
721 st.equal('foo' in parsed, true, 'parsed has "foo" property');
722 st.equal('bar' in parsed.foo, true);
723 st.equal('baz' in parsed.foo, true);
724 st.equal(parsed.foo.bar, 'baz');
725 st.deepEqual(parsed.foo.baz, a);
726 st.end();
727 });
728
729 t.test('does not crash when parsing deep objects', function (st) {
730 var parsed;
731 var str = 'foo';
732
733 for (var i = 0; i < 5000; i++) {
734 str += '[p]';
735 }
736
737 str += '=bar';
738
739 st.doesNotThrow(function () {
740 parsed = qs.parse(str, { depth: 5000 });
741 });
742
743 st.equal('foo' in parsed, true, 'parsed has "foo" property');
744
745 var depth = 0;
746 var ref = parsed.foo;
747 while ((ref = ref.p)) {
748 depth += 1;
749 }
750
751 st.equal(depth, 5000, 'parsed is 5000 properties deep');
752
753 st.end();
754 });
755
756 t.test('parses null objects correctly', { skip: !hasProto }, function (st) {
757 var a = { __proto__: null, b: 'c' };
758
759 st.deepEqual(qs.parse(a), { b: 'c' });
760 var result = qs.parse({ a: a });
761 st.equal('a' in result, true, 'result has "a" property');
762 st.deepEqual(result.a, a);
763 st.end();
764 });
765
766 t.test('parses dates correctly', function (st) {
767 var now = new Date();
768 st.deepEqual(qs.parse({ a: now }), { a: now });
769 st.end();
770 });
771
772 t.test('parses regular expressions correctly', function (st) {
773 var re = /^test$/;
774 st.deepEqual(qs.parse({ a: re }), { a: re });
775 st.end();
776 });
777
778 t.test('does not allow overwriting prototype properties', function (st) {
779 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
780 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
781
782 st.deepEqual(
783 qs.parse('toString', { allowPrototypes: false }),
784 {},
785 'bare "toString" results in {}'
786 );
787
788 st.end();
789 });
790
791 t.test('can allow overwriting prototype properties', function (st) {
792 st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
793 st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
794
795 st.deepEqual(
796 qs.parse('toString', { allowPrototypes: true }),
797 { toString: '' },
798 'bare "toString" results in { toString: "" }'
799 );
800
801 st.end();
802 });
803
804 t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) {
805 // We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it
806 // can't be unfrozen. Instead, we add a new non-writable property to simulate this.
807 st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true }));
808
809 st['throws'](
810 function () {
811 var obj = {};
812 obj.frozenProp = 'bar';
813 },
814 // node < 6 has a different error message
815 /^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/,
816 'regular assignment of an inherited non-writable property throws'
817 );
818
819 var parsed;
820 st.doesNotThrow(
821 function () {
822 parsed = qs.parse('frozenProp', { allowPrototypes: false });
823 },
824 'parsing a nonwritable Object.prototype property does not throw'
825 );
826
827 st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}');
828
829 st.end();
830 });
831
832 t.test('params starting with a closing bracket', function (st) {
833 st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
834 st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
835 st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
836 st.end();
837 });
838
839 t.test('params starting with a starting bracket', function (st) {
840 st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
841 st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
842 st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
843 st.end();
844 });
845
846 t.test('add keys to objects', function (st) {
847 st.deepEqual(
848 qs.parse('a[b]=c&a=d', { strictMerge: false }),
849 { a: { b: 'c', d: true } },
850 'can add keys to objects'
851 );
852
853 st.deepEqual(
854 qs.parse('a[b]=c&a=toString', { strictMerge: false }),
855 { a: { b: 'c' } },
856 'can not overwrite prototype'
857 );
858
859 st.deepEqual(
860 qs.parse('a[b]=c&a=toString', { strictMerge: false, allowPrototypes: true }),
861 { a: { b: 'c', toString: true } },
862 'can overwrite prototype with allowPrototypes true'
863 );
864
865 st.deepEqual(
866 qs.parse('a[b]=c&a=toString', { strictMerge: false, plainObjects: true }),
867 { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
868 'can overwrite prototype with plainObjects true'
869 );
870
871 st.end();
872 });
873
874 t.test('strictMerge wraps object and primitive into an array', function (st) {
875 st.deepEqual(
876 qs.parse('a[b]=c&a=d'),
877 { a: [{ b: 'c' }, 'd'] },
878 'object then primitive produces array'
879 );
880
881 st.deepEqual(
882 qs.parse('a=d&a[b]=c'),
883 { a: ['d', { b: 'c' }] },
884 'primitive then object produces array'
885 );
886
887 st.deepEqual(
888 qs.parse('a[b]=c&a=toString'),
889 { a: [{ b: 'c' }, 'toString'] },
890 'prototype-colliding value is preserved in array'
891 );
892
893 st.deepEqual(
894 qs.parse('a[b]=c&a=toString', { plainObjects: true }),
895 { __proto__: null, a: [{ __proto__: null, b: 'c' }, 'toString'] },
896 'plainObjects preserved in array wrapping'
897 );
898
899 st.end();
900 });
901
902 t.test('dunder proto is ignored', function (st) {
903 var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
904 var result = qs.parse(payload, { allowPrototypes: true });
905
906 st.deepEqual(
907 result,
908 {
909 categories: {
910 length: '42'
911 }
912 },
913 'silent [[Prototype]] payload'
914 );
915
916 var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
917
918 st.deepEqual(
919 plainResult,
920 {
921 __proto__: null,
922 categories: {
923 __proto__: null,
924 length: '42'
925 }
926 },
927 'silent [[Prototype]] payload: plain objects'
928 );
929
930 var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
931
932 st.notOk(Array.isArray(query.categories), 'is not an array');
933 st.notOk(query.categories instanceof Array, 'is not instanceof an array');
934 st.deepEqual(query.categories, { some: { json: 'toInject' } });
935 st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
936
937 st.deepEqual(
938 qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
939 {
940 foo: {
941 bar: 'stuffs'
942 }
943 },
944 'hidden values'
945 );
946
947 st.deepEqual(
948 qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
949 {
950 __proto__: null,
951 foo: {
952 __proto__: null,
953 bar: 'stuffs'
954 }
955 },
956 'hidden values: plain objects'
957 );
958
959 st.end();
960 });
961
962 t.test('can return null objects', { skip: !hasProto }, function (st) {
963 var expected = {
964 __proto__: null,
965 a: {
966 __proto__: null,
967 b: 'c',
968 hasOwnProperty: 'd'
969 }
970 };
971 st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
972 st.deepEqual(qs.parse(null, { plainObjects: true }), { __proto__: null });
973 var expectedArray = {
974 __proto__: null,
975 a: {
976 __proto__: null,
977 0: 'b',
978 c: 'd'
979 }
980 };
981 st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
982 st.end();
983 });
984
985 t.test('can parse with custom encoding', function (st) {
986 st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
987 decoder: function (str) {
988 var reg = /%([0-9A-F]{2})/ig;
989 var result = [];
990 var parts = reg.exec(str);
991 while (parts) {
992 result.push(parseInt(parts[1], 16));
993 parts = reg.exec(str);
994 }
995 return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
996 }
997 }), { 県: '大阪府' });
998 st.end();
999 });
1000
1001 t.test('receives the default decoder as a second argument', function (st) {
1002 st.plan(1);
1003 qs.parse('a', {
1004 decoder: function (str, defaultDecoder) {
1005 st.equal(defaultDecoder, utils.decode);
1006 }
1007 });
1008 st.end();
1009 });
1010
1011 t.test('throws error with wrong decoder', function (st) {
1012 st['throws'](function () {
1013 qs.parse({}, { decoder: 'string' });
1014 }, new TypeError('Decoder has to be a function.'));
1015 st.end();
1016 });
1017
1018 t.test('does not mutate the options argument', function (st) {
1019 var options = {};
1020 qs.parse('a[b]=true', options);
1021 st.deepEqual(options, {});
1022 st.end();
1023 });
1024
1025 t.test('throws if an invalid charset is specified', function (st) {
1026 st['throws'](function () {
1027 qs.parse('a=b', { charset: 'foobar' });
1028 }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
1029 st.end();
1030 });
1031
1032 t.test('parses an iso-8859-1 string if asked to', function (st) {
1033 st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
1034 st.end();
1035 });
1036
1037 var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
1038 var urlEncodedOSlashInUtf8 = '%C3%B8';
1039 var urlEncodedNumCheckmark = '%26%2310003%3B';
1040 var urlEncodedNumSmiley = '%26%239786%3B';
1041
1042 t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
1043 st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
1044 st.end();
1045 });
1046
1047 t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
1048 st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
1049 st.end();
1050 });
1051
1052 t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
1053 st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
1054 st.end();
1055 });
1056
1057 t.test('ignores an utf8 sentinel with an unknown value', function (st) {
1058 st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
1059 st.end();
1060 });
1061
1062 t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
1063 st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
1064 st.end();
1065 });
1066
1067 t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
1068 st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
1069 st.end();
1070 });
1071
1072 t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
1073 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
1074 st.end();
1075 });
1076
1077 t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
1078 st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
1079 charset: 'iso-8859-1',
1080 decoder: function (str, defaultDecoder, charset) {
1081 return str ? defaultDecoder(str, defaultDecoder, charset) : null;
1082 },
1083 interpretNumericEntities: true
1084 }), { foo: null, bar: '☺' });
1085 st.end();
1086 });
1087
1088 t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {
1089 st.deepEqual(
1090 qs.parse('null=1&ToNull=2', {
1091 decoder: function (str, defaultDecoder, charset) {
1092 return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);
1093 }
1094 }),
1095 { 'null': '1' },
1096 '"null" key is not overridden by `null` decoder result'
1097 );
1098
1099 st.end();
1100 });
1101
1102 t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
1103 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '&#9786;' });
1104 st.end();
1105 });
1106
1107 t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
1108 st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
1109 st.end();
1110 });
1111
1112 t.test('interpretNumericEntities with comma:true and iso charset does not crash', function (st) {
1113 st.deepEqual(
1114 qs.parse('b&a[]=1,' + urlEncodedNumSmiley, { comma: true, charset: 'iso-8859-1', interpretNumericEntities: true }),
1115 { b: '', a: ['1,☺'] }
1116 );
1117
1118 st.end();
1119 });
1120
1121 t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
1122 st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
1123 st.end();
1124 });
1125
1126 t.test('allows for decoding keys and values differently', function (st) {
1127 var decoder = function (str, defaultDecoder, charset, type) {
1128 if (type === 'key') {
1129 return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
1130 }
1131 if (type === 'value') {
1132 return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
1133 }
1134 throw 'this should never happen! type: ' + type;
1135 };
1136
1137 st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
1138
1139 var noopDecoder = function () { return 'x'; };
1140 noopDecoder();
1141 st['throws'](
1142 function () { decoder('x', noopDecoder, 'utf-8', 'unknown'); },
1143 'this should never happen! type: unknown',
1144 'decoder throws for unexpected type'
1145 );
1146
1147 st.end();
1148 });
1149
1150 t.test('parameter limit tests', function (st) {
1151 st.test('does not throw error when within parameter limit', function (sst) {
1152 var result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });
1153 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses without errors');
1154 sst.end();
1155 });
1156
1157 st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {
1158 sst['throws'](
1159 function () {
1160 qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: 'true' });
1161 },
1162 new TypeError('`throwOnLimitExceeded` option must be a boolean'),
1163 'throws error when throwOnLimitExceeded is present and not boolean'
1164 );
1165 sst.end();
1166 });
1167
1168 st.test('throws error when parameter limit exceeded', function (sst) {
1169 sst['throws'](
1170 function () {
1171 qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });
1172 },
1173 new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),
1174 'throws error when parameter limit is exceeded'
1175 );
1176
1177 sst['throws'](
1178 function () {
1179 qs.parse('a=1&b=2', { parameterLimit: 1, throwOnLimitExceeded: true });
1180 },
1181 new RangeError('Parameter limit exceeded. Only 1 parameter allowed.'),
1182 'throws error with singular "parameter" when parameterLimit is 1'
1183 );
1184 sst.end();
1185 });
1186
1187 st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {
1188 var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });
1189 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
1190 sst.end();
1191 });
1192
1193 st.test('silently truncates when parameter limit exceeded without error', function (sst) {
1194 var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });
1195 sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
1196 sst.end();
1197 });
1198
1199 st.test('allows unlimited parameters when parameterLimit set to Infinity', function (sst) {
1200 var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity });
1201 sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation');
1202 sst.end();
1203 });
1204
1205 st.test('allows unlimited parameters when parameterLimit is Infinity and throwOnLimitExceeded is true', function (sst) {
1206 var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity, throwOnLimitExceeded: true });
1207 sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation or throwing');
1208 sst.end();
1209 });
1210
1211 st.end();
1212 });
1213
1214 t.test('array limit tests', function (st) {
1215 st.test('does not throw error when array is within limit', function (sst) {
1216 var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 5, throwOnLimitExceeded: true });
1217 sst.deepEqual(result, { a: ['1', '2', '3'] }, 'parses array without errors');
1218 sst.end();
1219 });
1220
1221 st.test('throws error when throwOnLimitExceeded is present but not boolean for array limit', function (sst) {
1222 sst['throws'](
1223 function () {
1224 qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: 'true' });
1225 },
1226 new TypeError('`throwOnLimitExceeded` option must be a boolean'),
1227 'throws error when throwOnLimitExceeded is present and not boolean for array limit'
1228 );
1229 sst.end();
1230 });
1231
1232 st.test('throws error when array limit exceeded', function (sst) {
1233 // 4 elements exceeds limit of 3
1234 sst['throws'](
1235 function () {
1236 qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
1237 },
1238 new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
1239 'throws error when array limit is exceeded'
1240 );
1241 sst.end();
1242 });
1243
1244 st.test('does not throw when at limit', function (sst) {
1245 // 3 elements = limit of 3, should not throw
1246 var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3, throwOnLimitExceeded: true });
1247 sst.ok(Array.isArray(result.a), 'result is an array');
1248 sst.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1249 sst.end();
1250 });
1251
1252 st.test('converts array to object if length is greater than limit', function (sst) {
1253 var result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });
1254
1255 sst.deepEqual(result, { a: { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6' } }, 'parses into object if array length is greater than limit');
1256 sst.end();
1257 });
1258
1259 st.test('throws error when indexed notation exceeds arrayLimit with throwOnLimitExceeded', function (sst) {
1260 sst['throws'](
1261 function () {
1262 qs.parse('a[1001]=b', { arrayLimit: 1000, throwOnLimitExceeded: true });
1263 },
1264 new RangeError('Array limit exceeded. Only 1000 elements allowed in an array.'),
1265 'throws error for a single index exceeding arrayLimit'
1266 );
1267
1268 sst['throws'](
1269 function () {
1270 qs.parse('a[0]=1&a[1]=2&a[2]=3&a[10]=4', { arrayLimit: 6, throwOnLimitExceeded: true, allowSparse: true });
1271 },
1272 new RangeError('Array limit exceeded. Only 6 elements allowed in an array.'),
1273 'throws error when a sparse index exceeds arrayLimit'
1274 );
1275
1276 sst['throws'](
1277 function () {
1278 qs.parse('a[2]=b', { arrayLimit: 1, throwOnLimitExceeded: true });
1279 },
1280 new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1281 'throws error with singular "element" when arrayLimit is 1'
1282 );
1283
1284 sst.end();
1285 });
1286
1287 st.test('does not throw for indexed notation within arrayLimit with throwOnLimitExceeded', function (sst) {
1288 var result = qs.parse('a[4]=b', { arrayLimit: 5, throwOnLimitExceeded: true, allowSparse: true });
1289 sst.ok(Array.isArray(result.a), 'result is an array');
1290 sst.equal(result.a.length, 5, 'array has correct length');
1291 sst.equal(result.a[4], 'b', 'value at index 4 is correct');
1292 sst.end();
1293 });
1294
1295 st.test('silently converts to object for indexed notation exceeding arrayLimit without throwOnLimitExceeded', function (sst) {
1296 var result = qs.parse('a[1001]=b', { arrayLimit: 1000 });
1297 sst.deepEqual(result, { a: { 1001: 'b' } }, 'converts to object without throwing');
1298 sst.end();
1299 });
1300
1301 st.test('throws when duplicate bracket keys exceed arrayLimit with throwOnLimitExceeded', function (sst) {
1302 sst['throws'](
1303 function () {
1304 qs.parse('a[]=1&a[]=2&a[]=3&a[]=4&a[]=5&a[]=6', { arrayLimit: 5, throwOnLimitExceeded: true });
1305 },
1306 new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1307 'throws error when duplicate bracket notation exceeds array limit'
1308 );
1309 sst.end();
1310 });
1311
1312 st.end();
1313 });
1314
1315 t.end();
1316});
1317
1318test('parses empty keys', function (t) {
1319 emptyTestCases.forEach(function (testCase) {
1320 t.test('skips empty string key with ' + testCase.input, function (st) {
1321 st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
1322
1323 st.end();
1324 });
1325 });
1326});
1327
1328test('`duplicates` option', function (t) {
1329 v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
1330 if (typeof invalidOption !== 'undefined') {
1331 t['throws'](
1332 function () { qs.parse('', { duplicates: invalidOption }); },
1333 TypeError,
1334 'throws on invalid option: ' + inspect(invalidOption)
1335 );
1336 }
1337 });
1338
1339 t.deepEqual(
1340 qs.parse('foo=bar&foo=baz'),
1341 { foo: ['bar', 'baz'] },
1342 'duplicates: default, combine'
1343 );
1344
1345 t.deepEqual(
1346 qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
1347 { foo: ['bar', 'baz'] },
1348 'duplicates: combine'
1349 );
1350
1351 t.deepEqual(
1352 qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
1353 { foo: 'bar' },
1354 'duplicates: first'
1355 );
1356
1357 t.deepEqual(
1358 qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
1359 { foo: 'baz' },
1360 'duplicates: last'
1361 );
1362
1363 t.test('bracket notation always combines regardless of duplicates', function (st) {
1364 st.deepEqual(
1365 qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'last' }),
1366 { a: '2', b: ['1', '2'] },
1367 'duplicates last: unbracketed takes last, bracketed combines'
1368 );
1369
1370 st.deepEqual(
1371 qs.parse('b[]=1&b[]=2', { duplicates: 'last' }),
1372 { b: ['1', '2'] },
1373 'duplicates last: bracketed always combines'
1374 );
1375
1376 st.deepEqual(
1377 qs.parse('b[]=1&b[]=2', { duplicates: 'first' }),
1378 { b: ['1', '2'] },
1379 'duplicates first: bracketed always combines'
1380 );
1381
1382 st.deepEqual(
1383 qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'first' }),
1384 { a: '1', b: ['1', '2'] },
1385 'duplicates first: unbracketed takes first, bracketed combines'
1386 );
1387
1388 st.end();
1389 });
1390
1391 t.end();
1392});
1393
1394test('qs strictDepth option - throw cases', function (t) {
1395 t.test('throws an exception when depth exceeds the limit with strictDepth: true', function (st) {
1396 st['throws'](
1397 function () {
1398 qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
1399 },
1400 RangeError,
1401 'throws RangeError'
1402 );
1403 st.end();
1404 });
1405
1406 t.test('throws an exception for multiple nested arrays with strictDepth: true', function (st) {
1407 st['throws'](
1408 function () {
1409 qs.parse('a[0][1][2][3][4]=b', { depth: 3, strictDepth: true });
1410 },
1411 RangeError,
1412 'throws RangeError'
1413 );
1414 st.end();
1415 });
1416
1417 t.test('throws an exception for nested objects and arrays with strictDepth: true', function (st) {
1418 st['throws'](
1419 function () {
1420 qs.parse('a[b][c][0][d][e]=f', { depth: 3, strictDepth: true });
1421 },
1422 RangeError,
1423 'throws RangeError'
1424 );
1425 st.end();
1426 });
1427
1428 t.test('throws an exception for different types of values with strictDepth: true', function (st) {
1429 st['throws'](
1430 function () {
1431 qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 3, strictDepth: true });
1432 },
1433 RangeError,
1434 'throws RangeError'
1435 );
1436 st.end();
1437 });
1438
1439});
1440
1441test('qs strictDepth option - non-throw cases', function (t) {
1442 t.test('when depth is 0 and strictDepth true, do not throw', function (st) {
1443 st.doesNotThrow(
1444 function () {
1445 qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 0, strictDepth: true });
1446 },
1447 RangeError,
1448 'does not throw RangeError'
1449 );
1450 st.end();
1451 });
1452
1453 t.test('parses successfully when depth is within the limit with strictDepth: true', function (st) {
1454 st.doesNotThrow(
1455 function () {
1456 var result = qs.parse('a[b]=c', { depth: 1, strictDepth: true });
1457 st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
1458 }
1459 );
1460 st.end();
1461 });
1462
1463 t.test('does not throw an exception when depth exceeds the limit with strictDepth: false', function (st) {
1464 st.doesNotThrow(
1465 function () {
1466 var result = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
1467 st.deepEqual(result, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }, 'parses with depth limit');
1468 }
1469 );
1470 st.end();
1471 });
1472
1473 t.test('parses successfully when depth is within the limit with strictDepth: false', function (st) {
1474 st.doesNotThrow(
1475 function () {
1476 var result = qs.parse('a[b]=c', { depth: 1 });
1477 st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
1478 }
1479 );
1480 st.end();
1481 });
1482
1483 t.test('does not throw when depth is exactly at the limit with strictDepth: true', function (st) {
1484 st.doesNotThrow(
1485 function () {
1486 var result = qs.parse('a[b][c]=d', { depth: 2, strictDepth: true });
1487 st.deepEqual(result, { a: { b: { c: 'd' } } }, 'parses correctly');
1488 }
1489 );
1490 st.end();
1491 });
1492});
1493
1494test('DOS', function (t) {
1495 var arr = [];
1496 for (var i = 0; i < 105; i++) {
1497 arr[arr.length] = 'x';
1498 }
1499 var attack = 'a[]=' + arr.join('&a[]=');
1500 var result = qs.parse(attack, { arrayLimit: 100 });
1501
1502 t.notOk(Array.isArray(result.a), 'arrayLimit is respected: result is an object, not an array');
1503 t.equal(Object.keys(result.a).length, 105, 'all values are preserved');
1504
1505 t.end();
1506});
1507
1508test('arrayLimit boundary conditions', function (t) {
1509 // arrayLimit is the max number of elements allowed in an array
1510 t.test('exactly at the limit stays as array', function (st) {
1511 // 3 elements = limit of 3
1512 var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3 });
1513 st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
1514 st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1515 st.end();
1516 });
1517
1518 t.test('one over the limit converts to object', function (st) {
1519 // 4 elements exceeds limit of 3
1520 var result = qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3 });
1521 st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
1522 st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
1523 st.end();
1524 });
1525
1526 t.test('arrayLimit 1 with one value', function (st) {
1527 // 1 element = limit of 1
1528 var result = qs.parse('a[]=1', { arrayLimit: 1 });
1529 st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
1530 st.deepEqual(result.a, ['1'], 'value preserved as array');
1531 st.end();
1532 });
1533
1534 t.test('arrayLimit 1 with two values converts to object', function (st) {
1535 // 2 elements exceeds limit of 1
1536 var result = qs.parse('a[]=1&a[]=2', { arrayLimit: 1 });
1537 st.notOk(Array.isArray(result.a), 'result is not an array');
1538 st.deepEqual(result.a, { 0: '1', 1: '2' }, 'all values preserved as object');
1539 st.end();
1540 });
1541
1542 t.end();
1543});
1544
1545test('comma + arrayLimit', function (t) {
1546 t.test('comma-separated values within arrayLimit stay as array', function (st) {
1547 var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 5 });
1548 st.ok(Array.isArray(result.a), 'result is an array');
1549 st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1550 st.end();
1551 });
1552
1553 t.test('comma-separated values exceeding arrayLimit convert to object', function (st) {
1554 var result = qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3 });
1555 st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
1556 st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
1557 st.end();
1558 });
1559
1560 t.test('comma-separated values exceeding arrayLimit with throwOnLimitExceeded throws', function (st) {
1561 st['throws'](
1562 function () {
1563 qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3, throwOnLimitExceeded: true });
1564 },
1565 new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
1566 'throws error when comma-split exceeds array limit'
1567 );
1568
1569 st['throws'](
1570 function () {
1571 qs.parse('a=1,2,3', { comma: true, arrayLimit: 1, throwOnLimitExceeded: true });
1572 },
1573 new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1574 'throws error with singular "element" when arrayLimit is 1'
1575 );
1576 st.end();
1577 });
1578
1579 t.test('comma-separated values at exactly arrayLimit stay as array', function (st) {
1580 var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 3 });
1581 st.ok(Array.isArray(result.a), 'result is an array when exactly at limit');
1582 st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
1583 st.end();
1584 });
1585
1586 t.end();
1587});
1588
1589test('mixed array and object notation', function (t) {
1590 t.test('array brackets with object key - under limit', function (st) {
1591 st.deepEqual(
1592 qs.parse('a[]=b&a[c]=d'),
1593 { a: { 0: 'b', c: 'd' } },
1594 'mixing [] and [key] converts to object'
1595 );
1596 st.end();
1597 });
1598
1599 t.test('array index with object key - under limit', function (st) {
1600 st.deepEqual(
1601 qs.parse('a[0]=b&a[c]=d'),
1602 { a: { 0: 'b', c: 'd' } },
1603 'mixing [0] and [key] produces object'
1604 );
1605 st.end();
1606 });
1607
1608 t.test('plain value with array brackets - under limit', function (st) {
1609 st.deepEqual(
1610 qs.parse('a=b&a[]=c', { arrayLimit: 20 }),
1611 { a: ['b', 'c'] },
1612 'plain value combined with [] stays as array under limit'
1613 );
1614 st.end();
1615 });
1616
1617 t.test('array brackets with plain value - under limit', function (st) {
1618 st.deepEqual(
1619 qs.parse('a[]=b&a=c', { arrayLimit: 20 }),
1620 { a: ['b', 'c'] },
1621 '[] combined with plain value stays as array under limit'
1622 );
1623 st.end();
1624 });
1625
1626 t.test('plain value with array index - under limit', function (st) {
1627 st.deepEqual(
1628 qs.parse('a=b&a[0]=c', { arrayLimit: 20 }),
1629 { a: ['b', 'c'] },
1630 'plain value combined with [0] stays as array under limit'
1631 );
1632 st.end();
1633 });
1634
1635 t.test('multiple plain values with duplicates combine', function (st) {
1636 st.deepEqual(
1637 qs.parse('a=b&a=c&a=d', { arrayLimit: 20 }),
1638 { a: ['b', 'c', 'd'] },
1639 'duplicate plain keys combine into array'
1640 );
1641 st.end();
1642 });
1643
1644 t.test('multiple plain values exceeding limit', function (st) {
1645 // 3 elements (indices 0-2), max index 2 > limit 1
1646 st.deepEqual(
1647 qs.parse('a=b&a=c&a=d', { arrayLimit: 1 }),
1648 { a: { 0: 'b', 1: 'c', 2: 'd' } },
1649 'duplicate plain keys convert to object when exceeding limit'
1650 );
1651 st.end();
1652 });
1653
1654 t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) {
1655 var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } };
1656
1657 st.deepEqual(
1658 qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }),
1659 expected,
1660 'arrayLimit -1'
1661 );
1662
1663 st.deepEqual(
1664 qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }),
1665 expected,
1666 'arrayLimit 0'
1667 );
1668
1669 st.deepEqual(
1670 qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }),
1671 expected,
1672 'arrayLimit 1'
1673 );
1674
1675 st.end();
1676 });
1677
1678 t.test('uses existing array length for currentArrayLength when parsing object input with bracket keys', function (st) {
1679 var input = {};
1680 var arr = ['x', 'y'];
1681 arr.a = ['z', 'w'];
1682 input['a[]'] = arr;
1683 st.deepEqual(qs.parse(input), { a: ['x', 'y'] }, 'parses object input with bracket keys using existing array values');
1684 st.end();
1685 });
1686
1687 t.test('throws with singular message when object input bracket key exceeds arrayLimit of 1', function (st) {
1688 var input = {};
1689 var arr = ['x'];
1690 arr.a = ['z', 'w'];
1691 input['a[]'] = arr;
1692 st['throws'](
1693 function () {
1694 qs.parse(input, { throwOnLimitExceeded: true, arrayLimit: 1 });
1695 },
1696 new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1697 'throws singular error for object input exceeding arrayLimit 1'
1698 );
1699 st.end();
1700 });
1701
1702 t.end();
1703});
Note: See TracBrowser for help on using the repository browser.