• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3var test = require('tape');
4var qs = require('../');
5var utils = require('../lib/utils');
6var iconv = require('iconv-lite');
7var SaferBuffer = require('safer-buffer').Buffer;
8
9test('parse()', function (t) {
10    t.test('parses a simple string', function (st) {
11        st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
12        st.deepEqual(qs.parse('foo=c++'), { foo: 'c  ' });
13        st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
14        st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
15        st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
16        st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
17        st.deepEqual(qs.parse('foo'), { foo: '' });
18        st.deepEqual(qs.parse('foo='), { foo: '' });
19        st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
20        st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
21        st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
22        st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
23        st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
24        st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
25        st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
26        st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
27            cht: 'p3',
28            chd: 't:60,40',
29            chs: '250x100',
30            chl: 'Hello|World'
31        });
32        st.end();
33    });
34
35    t.test('allows enabling dot notation', function (st) {
36        st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
37        st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
38        st.end();
39    });
40
41    t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
42    t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
43    t.deepEqual(
44        qs.parse('a[b][c][d][e][f][g][h]=i'),
45        { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
46        'defaults to a depth of 5'
47    );
48
49    t.test('only parses one level when depth = 1', function (st) {
50        st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
51        st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
52        st.end();
53    });
54
55    t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
56
57    t.test('parses an explicit array', function (st) {
58        st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
59        st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
60        st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
61        st.end();
62    });
63
64    t.test('parses a mix of simple and explicit arrays', function (st) {
65        st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
66        st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
67        st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
68        st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
69
70        st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
71        st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
72        st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
73
74        st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
75        st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
76        st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
77
78        st.end();
79    });
80
81    t.test('parses a nested array', function (st) {
82        st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
83        st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
84        st.end();
85    });
86
87    t.test('allows to specify array indices', function (st) {
88        st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
89        st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
90        st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
91        st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
92        st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
93        st.end();
94    });
95
96    t.test('limits specific array indices to arrayLimit', function (st) {
97        st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
98        st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
99        st.end();
100    });
101
102    t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
103
104    t.test('supports encoded = signs', function (st) {
105        st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
106        st.end();
107    });
108
109    t.test('is ok with url encoded strings', function (st) {
110        st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
111        st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
112        st.end();
113    });
114
115    t.test('allows brackets in the value', function (st) {
116        st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
117        st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
118        st.end();
119    });
120
121    t.test('allows empty values', function (st) {
122        st.deepEqual(qs.parse(''), {});
123        st.deepEqual(qs.parse(null), {});
124        st.deepEqual(qs.parse(undefined), {});
125        st.end();
126    });
127
128    t.test('transforms arrays to objects', function (st) {
129        st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
130        st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
131        st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
132        st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
133        st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
134        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' }] });
135
136        st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
137        st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
138        st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
139        st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
140        st.end();
141    });
142
143    t.test('transforms arrays to objects (dot notation)', function (st) {
144        st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
145        st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
146        st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
147        st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
148        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' }] });
149        st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
150        st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
151        st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
152        st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
153        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' }] });
154        st.end();
155    });
156
157    t.test('correctly prunes undefined values when converting an array to an object', function (st) {
158        st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
159        st.end();
160    });
161
162    t.test('supports malformed uri characters', function (st) {
163        st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
164        st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
165        st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
166        st.end();
167    });
168
169    t.test('doesn\'t produce empty keys', function (st) {
170        st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
171        st.end();
172    });
173
174    t.test('cannot access Object prototype', function (st) {
175        qs.parse('constructor[prototype][bad]=bad');
176        qs.parse('bad[constructor][prototype][bad]=bad');
177        st.equal(typeof Object.prototype.bad, 'undefined');
178        st.end();
179    });
180
181    t.test('parses arrays of objects', function (st) {
182        st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
183        st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
184        st.end();
185    });
186
187    t.test('allows for empty strings in arrays', function (st) {
188        st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
189
190        st.deepEqual(
191            qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
192            { a: ['b', null, 'c', ''] },
193            'with arrayLimit 20 + array indices: null then empty string works'
194        );
195        st.deepEqual(
196            qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
197            { a: ['b', null, 'c', ''] },
198            'with arrayLimit 0 + array brackets: null then empty string works'
199        );
200
201        st.deepEqual(
202            qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
203            { a: ['b', '', 'c', null] },
204            'with arrayLimit 20 + array indices: empty string then null works'
205        );
206        st.deepEqual(
207            qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
208            { a: ['b', '', 'c', null] },
209            'with arrayLimit 0 + array brackets: empty string then null works'
210        );
211
212        st.deepEqual(
213            qs.parse('a[]=&a[]=b&a[]=c'),
214            { a: ['', 'b', 'c'] },
215            'array brackets: empty strings work'
216        );
217        st.end();
218    });
219
220    t.test('compacts sparse arrays', function (st) {
221        st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
222        st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
223        st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
224        st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
225        st.end();
226    });
227
228    t.test('parses semi-parsed strings', function (st) {
229        st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
230        st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
231        st.end();
232    });
233
234    t.test('parses buffers correctly', function (st) {
235        var b = SaferBuffer.from('test');
236        st.deepEqual(qs.parse({ a: b }), { a: b });
237        st.end();
238    });
239
240    t.test('continues parsing when no parent is found', function (st) {
241        st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
242        st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
243        st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
244        st.end();
245    });
246
247    t.test('does not error when parsing a very long array', function (st) {
248        var str = 'a[]=a';
249        while (Buffer.byteLength(str) < 128 * 1024) {
250            str = str + '&' + str;
251        }
252
253        st.doesNotThrow(function () {
254            qs.parse(str);
255        });
256
257        st.end();
258    });
259
260    t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
261        Object.prototype.crash = '';
262        Array.prototype.crash = '';
263        st.doesNotThrow(qs.parse.bind(null, 'a=b'));
264        st.deepEqual(qs.parse('a=b'), { a: 'b' });
265        st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
266        st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
267        delete Object.prototype.crash;
268        delete Array.prototype.crash;
269        st.end();
270    });
271
272    t.test('parses a string with an alternative string delimiter', function (st) {
273        st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
274        st.end();
275    });
276
277    t.test('parses a string with an alternative RegExp delimiter', function (st) {
278        st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
279        st.end();
280    });
281
282    t.test('does not use non-splittable objects as delimiters', function (st) {
283        st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
284        st.end();
285    });
286
287    t.test('allows overriding parameter limit', function (st) {
288        st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
289        st.end();
290    });
291
292    t.test('allows setting the parameter limit to Infinity', function (st) {
293        st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
294        st.end();
295    });
296
297    t.test('allows overriding array limit', function (st) {
298        st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
299        st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
300        st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
301        st.end();
302    });
303
304    t.test('allows disabling array parsing', function (st) {
305        st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
306        st.end();
307    });
308
309    t.test('allows for query string prefix', function (st) {
310        st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
311        st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
312        st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
313        st.end();
314    });
315
316    t.test('parses an object', function (st) {
317        var input = {
318            'user[name]': { 'pop[bob]': 3 },
319            'user[email]': null
320        };
321
322        var expected = {
323            user: {
324                name: { 'pop[bob]': 3 },
325                email: null
326            }
327        };
328
329        var result = qs.parse(input);
330
331        st.deepEqual(result, expected);
332        st.end();
333    });
334
335    t.test('parses an object in dot notation', function (st) {
336        var input = {
337            'user.name': { 'pop[bob]': 3 },
338            'user.email.': null
339        };
340
341        var expected = {
342            user: {
343                name: { 'pop[bob]': 3 },
344                email: null
345            }
346        };
347
348        var result = qs.parse(input, { allowDots: true });
349
350        st.deepEqual(result, expected);
351        st.end();
352    });
353
354    t.test('parses an object and not child values', function (st) {
355        var input = {
356            'user[name]': { 'pop[bob]': { test: 3 } },
357            'user[email]': null
358        };
359
360        var expected = {
361            user: {
362                name: { 'pop[bob]': { test: 3 } },
363                email: null
364            }
365        };
366
367        var result = qs.parse(input);
368
369        st.deepEqual(result, expected);
370        st.end();
371    });
372
373    t.test('does not blow up when Buffer global is missing', function (st) {
374        var tempBuffer = global.Buffer;
375        delete global.Buffer;
376        var result = qs.parse('a=b&c=d');
377        global.Buffer = tempBuffer;
378        st.deepEqual(result, { a: 'b', c: 'd' });
379        st.end();
380    });
381
382    t.test('does not crash when parsing circular references', function (st) {
383        var a = {};
384        a.b = a;
385
386        var parsed;
387
388        st.doesNotThrow(function () {
389            parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
390        });
391
392        st.equal('foo' in parsed, true, 'parsed has "foo" property');
393        st.equal('bar' in parsed.foo, true);
394        st.equal('baz' in parsed.foo, true);
395        st.equal(parsed.foo.bar, 'baz');
396        st.deepEqual(parsed.foo.baz, a);
397        st.end();
398    });
399
400    t.test('does not crash when parsing deep objects', function (st) {
401        var parsed;
402        var str = 'foo';
403
404        for (var i = 0; i < 5000; i++) {
405            str += '[p]';
406        }
407
408        str += '=bar';
409
410        st.doesNotThrow(function () {
411            parsed = qs.parse(str, { depth: 5000 });
412        });
413
414        st.equal('foo' in parsed, true, 'parsed has "foo" property');
415
416        var depth = 0;
417        var ref = parsed.foo;
418        while ((ref = ref.p)) {
419            depth += 1;
420        }
421
422        st.equal(depth, 5000, 'parsed is 5000 properties deep');
423
424        st.end();
425    });
426
427    t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
428        var a = Object.create(null);
429        a.b = 'c';
430
431        st.deepEqual(qs.parse(a), { b: 'c' });
432        var result = qs.parse({ a: a });
433        st.equal('a' in result, true, 'result has "a" property');
434        st.deepEqual(result.a, a);
435        st.end();
436    });
437
438    t.test('parses dates correctly', function (st) {
439        var now = new Date();
440        st.deepEqual(qs.parse({ a: now }), { a: now });
441        st.end();
442    });
443
444    t.test('parses regular expressions correctly', function (st) {
445        var re = /^test$/;
446        st.deepEqual(qs.parse({ a: re }), { a: re });
447        st.end();
448    });
449
450    t.test('does not allow overwriting prototype properties', function (st) {
451        st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
452        st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
453
454        st.deepEqual(
455            qs.parse('toString', { allowPrototypes: false }),
456            {},
457            'bare "toString" results in {}'
458        );
459
460        st.end();
461    });
462
463    t.test('can allow overwriting prototype properties', function (st) {
464        st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
465        st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
466
467        st.deepEqual(
468            qs.parse('toString', { allowPrototypes: true }),
469            { toString: '' },
470            'bare "toString" results in { toString: "" }'
471        );
472
473        st.end();
474    });
475
476    t.test('params starting with a closing bracket', function (st) {
477        st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
478        st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
479        st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
480        st.end();
481    });
482
483    t.test('params starting with a starting bracket', function (st) {
484        st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
485        st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
486        st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
487        st.end();
488    });
489
490    t.test('add keys to objects', function (st) {
491        st.deepEqual(
492            qs.parse('a[b]=c&a=d'),
493            { a: { b: 'c', d: true } },
494            'can add keys to objects'
495        );
496
497        st.deepEqual(
498            qs.parse('a[b]=c&a=toString'),
499            { a: { b: 'c' } },
500            'can not overwrite prototype'
501        );
502
503        st.deepEqual(
504            qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
505            { a: { b: 'c', toString: true } },
506            'can overwrite prototype with allowPrototypes true'
507        );
508
509        st.deepEqual(
510            qs.parse('a[b]=c&a=toString', { plainObjects: true }),
511            { a: { b: 'c', toString: true } },
512            'can overwrite prototype with plainObjects true'
513        );
514
515        st.end();
516    });
517
518    t.test('can return null objects', { skip: !Object.create }, function (st) {
519        var expected = Object.create(null);
520        expected.a = Object.create(null);
521        expected.a.b = 'c';
522        expected.a.hasOwnProperty = 'd';
523        st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
524        st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
525        var expectedArray = Object.create(null);
526        expectedArray.a = Object.create(null);
527        expectedArray.a[0] = 'b';
528        expectedArray.a.c = 'd';
529        st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
530        st.end();
531    });
532
533    t.test('can parse with custom encoding', function (st) {
534        st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
535            decoder: function (str) {
536                var reg = /%([0-9A-F]{2})/ig;
537                var result = [];
538                var parts = reg.exec(str);
539                while (parts) {
540                    result.push(parseInt(parts[1], 16));
541                    parts = reg.exec(str);
542                }
543                return iconv.decode(SaferBuffer.from(result), 'shift_jis').toString();
544            }
545        }), { : '大阪府' });
546        st.end();
547    });
548
549    t.test('receives the default decoder as a second argument', function (st) {
550        st.plan(1);
551        qs.parse('a', {
552            decoder: function (str, defaultDecoder) {
553                st.equal(defaultDecoder, utils.decode);
554            }
555        });
556        st.end();
557    });
558
559    t.test('throws error with wrong decoder', function (st) {
560        st['throws'](function () {
561            qs.parse({}, { decoder: 'string' });
562        }, new TypeError('Decoder has to be a function.'));
563        st.end();
564    });
565
566    t.test('does not mutate the options argument', function (st) {
567        var options = {};
568        qs.parse('a[b]=true', options);
569        st.deepEqual(options, {});
570        st.end();
571    });
572
573    t.end();
574});
575