1 | 'use strict';
|
2 |
|
3 | var test = require('tape');
|
4 | var qs = require('../');
|
5 | var utils = require('../lib/utils');
|
6 | var iconv = require('iconv-lite');
|
7 | var SaferBuffer = require('safer-buffer').Buffer;
|
8 |
|
9 | test('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('arrayFormat: brackets allows only explicit arrays', function (st) {
|
36 | st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
37 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
38 | st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'brackets' }), { a: 'b,c' });
|
39 | st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
40 | st.end();
|
41 | });
|
42 |
|
43 | t.test('arrayFormat: indices allows only indexed arrays', function (st) {
|
44 | st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
45 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
46 | st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'indices' }), { a: 'b,c' });
|
47 | st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
48 | st.end();
|
49 | });
|
50 |
|
51 | t.test('arrayFormat: comma allows only comma-separated arrays', function (st) {
|
52 | st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
53 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
54 | st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' });
|
55 | st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
56 | st.end();
|
57 | });
|
58 |
|
59 | t.test('arrayFormat: repeat allows only repeated values', function (st) {
|
60 | st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
61 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
62 | st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'repeat' }), { a: 'b,c' });
|
63 | st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
64 | st.end();
|
65 | });
|
66 |
|
67 | t.test('allows enabling dot notation', function (st) {
|
68 | st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
|
69 | st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
|
70 | st.end();
|
71 | });
|
72 |
|
73 | t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
|
74 | t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
|
75 | t.deepEqual(
|
76 | qs.parse('a[b][c][d][e][f][g][h]=i'),
|
77 | { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
|
78 | 'defaults to a depth of 5'
|
79 | );
|
80 |
|
81 | t.test('only parses one level when depth = 1', function (st) {
|
82 | st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
|
83 | st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
|
84 | st.end();
|
85 | });
|
86 |
|
87 | t.test('uses original key when depth = 0', function (st) {
|
88 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
|
89 | 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' });
|
90 | st.end();
|
91 | });
|
92 |
|
93 | t.test('uses original key when depth = false', function (st) {
|
94 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
|
95 | 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' });
|
96 | st.end();
|
97 | });
|
98 |
|
99 | t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
|
100 |
|
101 | t.test('parses an explicit array', function (st) {
|
102 | st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
|
103 | st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
|
104 | st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
|
105 | st.end();
|
106 | });
|
107 |
|
108 | t.test('parses a mix of simple and explicit arrays', function (st) {
|
109 | st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
|
110 | st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
|
111 | st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
|
112 | st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
|
113 |
|
114 | st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
115 | st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
|
116 | st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
|
117 |
|
118 | st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
119 | st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
|
120 | st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
|
121 |
|
122 | st.end();
|
123 | });
|
124 |
|
125 | t.test('parses a nested array', function (st) {
|
126 | st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
|
127 | st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
|
128 | st.end();
|
129 | });
|
130 |
|
131 | t.test('allows to specify array indices', function (st) {
|
132 | st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
|
133 | st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
|
134 | st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
|
135 | st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
|
136 | st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
|
137 | st.end();
|
138 | });
|
139 |
|
140 | t.test('limits specific array indices to arrayLimit', function (st) {
|
141 | st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
|
142 | st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
|
143 | st.end();
|
144 | });
|
145 |
|
146 | t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
|
147 |
|
148 | t.test('supports encoded = signs', function (st) {
|
149 | st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
|
150 | st.end();
|
151 | });
|
152 |
|
153 | t.test('is ok with url encoded strings', function (st) {
|
154 | st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
|
155 | st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
|
156 | st.end();
|
157 | });
|
158 |
|
159 | t.test('allows brackets in the value', function (st) {
|
160 | st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
|
161 | st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
|
162 | st.end();
|
163 | });
|
164 |
|
165 | t.test('allows empty values', function (st) {
|
166 | st.deepEqual(qs.parse(''), {});
|
167 | st.deepEqual(qs.parse(null), {});
|
168 | st.deepEqual(qs.parse(undefined), {});
|
169 | st.end();
|
170 | });
|
171 |
|
172 | t.test('transforms arrays to objects', function (st) {
|
173 | st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
|
174 | st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
|
175 | st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
|
176 | st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
|
177 | st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
|
178 | 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' }] });
|
179 |
|
180 | st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
|
181 | st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
|
182 | st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
|
183 | st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
|
184 | st.end();
|
185 | });
|
186 |
|
187 | t.test('transforms arrays to objects (dot notation)', function (st) {
|
188 | st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
|
189 | st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
|
190 | st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
|
191 | st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
|
192 | 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' }] });
|
193 | st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
|
194 | st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
|
195 | st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
|
196 | st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
|
197 | 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' }] });
|
198 | st.end();
|
199 | });
|
200 |
|
201 | t.test('correctly prunes undefined values when converting an array to an object', function (st) {
|
202 | st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
|
203 | st.end();
|
204 | });
|
205 |
|
206 | t.test('supports malformed uri characters', function (st) {
|
207 | st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
|
208 | st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
|
209 | st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
|
210 | st.end();
|
211 | });
|
212 |
|
213 | t.test('doesn\'t produce empty keys', function (st) {
|
214 | st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
|
215 | st.end();
|
216 | });
|
217 |
|
218 | t.test('cannot access Object prototype', function (st) {
|
219 | qs.parse('constructor[prototype][bad]=bad');
|
220 | qs.parse('bad[constructor][prototype][bad]=bad');
|
221 | st.equal(typeof Object.prototype.bad, 'undefined');
|
222 | st.end();
|
223 | });
|
224 |
|
225 | t.test('parses arrays of objects', function (st) {
|
226 | st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
|
227 | st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
|
228 | st.end();
|
229 | });
|
230 |
|
231 | t.test('allows for empty strings in arrays', function (st) {
|
232 | st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
|
233 |
|
234 | st.deepEqual(
|
235 | qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
|
236 | { a: ['b', null, 'c', ''] },
|
237 | 'with arrayLimit 20 + array indices: null then empty string works'
|
238 | );
|
239 | st.deepEqual(
|
240 | qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
|
241 | { a: ['b', null, 'c', ''] },
|
242 | 'with arrayLimit 0 + array brackets: null then empty string works'
|
243 | );
|
244 |
|
245 | st.deepEqual(
|
246 | qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
|
247 | { a: ['b', '', 'c', null] },
|
248 | 'with arrayLimit 20 + array indices: empty string then null works'
|
249 | );
|
250 | st.deepEqual(
|
251 | qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
|
252 | { a: ['b', '', 'c', null] },
|
253 | 'with arrayLimit 0 + array brackets: empty string then null works'
|
254 | );
|
255 |
|
256 | st.deepEqual(
|
257 | qs.parse('a[]=&a[]=b&a[]=c'),
|
258 | { a: ['', 'b', 'c'] },
|
259 | 'array brackets: empty strings work'
|
260 | );
|
261 | st.end();
|
262 | });
|
263 |
|
264 | t.test('compacts sparse arrays', function (st) {
|
265 | st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
|
266 | st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
|
267 | st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
|
268 | st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
|
269 | st.end();
|
270 | });
|
271 |
|
272 | t.test('parses sparse arrays', function (st) {
|
273 |
|
274 | st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
|
275 | st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
|
276 | st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
|
277 | st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
|
278 | st.end();
|
279 | });
|
280 |
|
281 | t.test('parses semi-parsed strings', function (st) {
|
282 | st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
|
283 | st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
|
284 | st.end();
|
285 | });
|
286 |
|
287 | t.test('parses buffers correctly', function (st) {
|
288 | var b = SaferBuffer.from('test');
|
289 | st.deepEqual(qs.parse({ a: b }), { a: b });
|
290 | st.end();
|
291 | });
|
292 |
|
293 | t.test('parses jquery-param strings', function (st) {
|
294 |
|
295 | 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';
|
296 | var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
|
297 | st.deepEqual(qs.parse(encoded), expected);
|
298 | st.end();
|
299 | });
|
300 |
|
301 | t.test('continues parsing when no parent is found', function (st) {
|
302 | st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
|
303 | st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
|
304 | st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
|
305 | st.end();
|
306 | });
|
307 |
|
308 | t.test('does not error when parsing a very long array', function (st) {
|
309 | var str = 'a[]=a';
|
310 | while (Buffer.byteLength(str) < 128 * 1024) {
|
311 | str = str + '&' + str;
|
312 | }
|
313 |
|
314 | st.doesNotThrow(function () {
|
315 | qs.parse(str);
|
316 | });
|
317 |
|
318 | st.end();
|
319 | });
|
320 |
|
321 | t.test('should not throw when a native prototype has an enumerable property', function (st) {
|
322 | Object.prototype.crash = '';
|
323 | Array.prototype.crash = '';
|
324 | st.doesNotThrow(qs.parse.bind(null, 'a=b'));
|
325 | st.deepEqual(qs.parse('a=b'), { a: 'b' });
|
326 | st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
|
327 | st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
|
328 | delete Object.prototype.crash;
|
329 | delete Array.prototype.crash;
|
330 | st.end();
|
331 | });
|
332 |
|
333 | t.test('parses a string with an alternative string delimiter', function (st) {
|
334 | st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
|
335 | st.end();
|
336 | });
|
337 |
|
338 | t.test('parses a string with an alternative RegExp delimiter', function (st) {
|
339 | st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
|
340 | st.end();
|
341 | });
|
342 |
|
343 | t.test('does not use non-splittable objects as delimiters', function (st) {
|
344 | st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
|
345 | st.end();
|
346 | });
|
347 |
|
348 | t.test('allows overriding parameter limit', function (st) {
|
349 | st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
|
350 | st.end();
|
351 | });
|
352 |
|
353 | t.test('allows setting the parameter limit to Infinity', function (st) {
|
354 | st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
|
355 | st.end();
|
356 | });
|
357 |
|
358 | t.test('allows overriding array limit', function (st) {
|
359 | st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
|
360 | st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
|
361 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
362 | st.end();
|
363 | });
|
364 |
|
365 | t.test('allows disabling array parsing', function (st) {
|
366 | var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
|
367 | st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
|
368 | st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
|
369 |
|
370 | var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
|
371 | st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
|
372 | st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
|
373 |
|
374 | st.end();
|
375 | });
|
376 |
|
377 | t.test('allows for query string prefix', function (st) {
|
378 | st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
|
379 | st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
|
380 | st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
|
381 | st.end();
|
382 | });
|
383 |
|
384 | t.test('parses an object', function (st) {
|
385 | var input = {
|
386 | 'user[name]': { 'pop[bob]': 3 },
|
387 | 'user[email]': null
|
388 | };
|
389 |
|
390 | var expected = {
|
391 | user: {
|
392 | name: { 'pop[bob]': 3 },
|
393 | email: null
|
394 | }
|
395 | };
|
396 |
|
397 | var result = qs.parse(input);
|
398 |
|
399 | st.deepEqual(result, expected);
|
400 | st.end();
|
401 | });
|
402 |
|
403 | t.test('parses string with comma as array divider', function (st) {
|
404 | st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
|
405 | st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
|
406 | st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
|
407 | st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
|
408 | st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
|
409 | st.end();
|
410 | });
|
411 |
|
412 | t.test('parses values with comma as array divider', function (st) {
|
413 | st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
|
414 | st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
|
415 | st.end();
|
416 | });
|
417 |
|
418 | t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
|
419 | var decoder = function (str, defaultDecoder, charset, type) {
|
420 | if (!isNaN(Number(str))) {
|
421 | return parseFloat(str);
|
422 | }
|
423 | return defaultDecoder(str, defaultDecoder, charset, type);
|
424 | };
|
425 |
|
426 | st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
|
427 | st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
|
428 |
|
429 | st.end();
|
430 | });
|
431 |
|
432 | t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
|
433 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
|
434 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
|
435 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
|
436 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
|
437 |
|
438 | st.end();
|
439 | });
|
440 |
|
441 | t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
|
442 | st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
|
443 | st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
|
444 | st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
|
445 |
|
446 | st.end();
|
447 | });
|
448 |
|
449 | t.test('parses an object in dot notation', function (st) {
|
450 | var input = {
|
451 | 'user.name': { 'pop[bob]': 3 },
|
452 | 'user.email.': null
|
453 | };
|
454 |
|
455 | var expected = {
|
456 | user: {
|
457 | name: { 'pop[bob]': 3 },
|
458 | email: null
|
459 | }
|
460 | };
|
461 |
|
462 | var result = qs.parse(input, { allowDots: true });
|
463 |
|
464 | st.deepEqual(result, expected);
|
465 | st.end();
|
466 | });
|
467 |
|
468 | t.test('parses an object and not child values', function (st) {
|
469 | var input = {
|
470 | 'user[name]': { 'pop[bob]': { test: 3 } },
|
471 | 'user[email]': null
|
472 | };
|
473 |
|
474 | var expected = {
|
475 | user: {
|
476 | name: { 'pop[bob]': { test: 3 } },
|
477 | email: null
|
478 | }
|
479 | };
|
480 |
|
481 | var result = qs.parse(input);
|
482 |
|
483 | st.deepEqual(result, expected);
|
484 | st.end();
|
485 | });
|
486 |
|
487 | t.test('does not blow up when Buffer global is missing', function (st) {
|
488 | var tempBuffer = global.Buffer;
|
489 | delete global.Buffer;
|
490 | var result = qs.parse('a=b&c=d');
|
491 | global.Buffer = tempBuffer;
|
492 | st.deepEqual(result, { a: 'b', c: 'd' });
|
493 | st.end();
|
494 | });
|
495 |
|
496 | t.test('does not crash when parsing circular references', function (st) {
|
497 | var a = {};
|
498 | a.b = a;
|
499 |
|
500 | var parsed;
|
501 |
|
502 | st.doesNotThrow(function () {
|
503 | parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
|
504 | });
|
505 |
|
506 | st.equal('foo' in parsed, true, 'parsed has "foo" property');
|
507 | st.equal('bar' in parsed.foo, true);
|
508 | st.equal('baz' in parsed.foo, true);
|
509 | st.equal(parsed.foo.bar, 'baz');
|
510 | st.deepEqual(parsed.foo.baz, a);
|
511 | st.end();
|
512 | });
|
513 |
|
514 | t.test('does not crash when parsing deep objects', function (st) {
|
515 | var parsed;
|
516 | var str = 'foo';
|
517 |
|
518 | for (var i = 0; i < 5000; i++) {
|
519 | str += '[p]';
|
520 | }
|
521 |
|
522 | str += '=bar';
|
523 |
|
524 | st.doesNotThrow(function () {
|
525 | parsed = qs.parse(str, { depth: 5000 });
|
526 | });
|
527 |
|
528 | st.equal('foo' in parsed, true, 'parsed has "foo" property');
|
529 |
|
530 | var depth = 0;
|
531 | var ref = parsed.foo;
|
532 | while ((ref = ref.p)) {
|
533 | depth += 1;
|
534 | }
|
535 |
|
536 | st.equal(depth, 5000, 'parsed is 5000 properties deep');
|
537 |
|
538 | st.end();
|
539 | });
|
540 |
|
541 | t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
|
542 | var a = Object.create(null);
|
543 | a.b = 'c';
|
544 |
|
545 | st.deepEqual(qs.parse(a), { b: 'c' });
|
546 | var result = qs.parse({ a: a });
|
547 | st.equal('a' in result, true, 'result has "a" property');
|
548 | st.deepEqual(result.a, a);
|
549 | st.end();
|
550 | });
|
551 |
|
552 | t.test('parses dates correctly', function (st) {
|
553 | var now = new Date();
|
554 | st.deepEqual(qs.parse({ a: now }), { a: now });
|
555 | st.end();
|
556 | });
|
557 |
|
558 | t.test('parses regular expressions correctly', function (st) {
|
559 | var re = /^test$/;
|
560 | st.deepEqual(qs.parse({ a: re }), { a: re });
|
561 | st.end();
|
562 | });
|
563 |
|
564 | t.test('does not allow overwriting prototype properties', function (st) {
|
565 | st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
|
566 | st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
|
567 |
|
568 | st.deepEqual(
|
569 | qs.parse('toString', { allowPrototypes: false }),
|
570 | {},
|
571 | 'bare "toString" results in {}'
|
572 | );
|
573 |
|
574 | st.end();
|
575 | });
|
576 |
|
577 | t.test('can allow overwriting prototype properties', function (st) {
|
578 | st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
|
579 | st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
|
580 |
|
581 | st.deepEqual(
|
582 | qs.parse('toString', { allowPrototypes: true }),
|
583 | { toString: '' },
|
584 | 'bare "toString" results in { toString: "" }'
|
585 | );
|
586 |
|
587 | st.end();
|
588 | });
|
589 |
|
590 | t.test('params starting with a closing bracket', function (st) {
|
591 | st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
|
592 | st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
|
593 | st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
|
594 | st.end();
|
595 | });
|
596 |
|
597 | t.test('params starting with a starting bracket', function (st) {
|
598 | st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
|
599 | st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
|
600 | st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
|
601 | st.end();
|
602 | });
|
603 |
|
604 | t.test('add keys to objects', function (st) {
|
605 | st.deepEqual(
|
606 | qs.parse('a[b]=c&a=d'),
|
607 | { a: { b: 'c', d: true } },
|
608 | 'can add keys to objects'
|
609 | );
|
610 |
|
611 | st.deepEqual(
|
612 | qs.parse('a[b]=c&a=toString'),
|
613 | { a: { b: 'c' } },
|
614 | 'can not overwrite prototype'
|
615 | );
|
616 |
|
617 | st.deepEqual(
|
618 | qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
|
619 | { a: { b: 'c', toString: true } },
|
620 | 'can overwrite prototype with allowPrototypes true'
|
621 | );
|
622 |
|
623 | st.deepEqual(
|
624 | qs.parse('a[b]=c&a=toString', { plainObjects: true }),
|
625 | { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
|
626 | 'can overwrite prototype with plainObjects true'
|
627 | );
|
628 |
|
629 | st.end();
|
630 | });
|
631 |
|
632 | t.test('can return null objects', { skip: !Object.create }, function (st) {
|
633 | var expected = Object.create(null);
|
634 | expected.a = Object.create(null);
|
635 | expected.a.b = 'c';
|
636 | expected.a.hasOwnProperty = 'd';
|
637 | st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
|
638 | st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
|
639 | var expectedArray = Object.create(null);
|
640 | expectedArray.a = Object.create(null);
|
641 | expectedArray.a[0] = 'b';
|
642 | expectedArray.a.c = 'd';
|
643 | st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
|
644 | st.end();
|
645 | });
|
646 |
|
647 | t.test('can parse with custom encoding', function (st) {
|
648 | st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
|
649 | decoder: function (str) {
|
650 | var reg = /%([0-9A-F]{2})/ig;
|
651 | var result = [];
|
652 | var parts = reg.exec(str);
|
653 | while (parts) {
|
654 | result.push(parseInt(parts[1], 16));
|
655 | parts = reg.exec(str);
|
656 | }
|
657 | return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
|
658 | }
|
659 | }), { 県: '大阪府' });
|
660 | st.end();
|
661 | });
|
662 |
|
663 | t.test('receives the default decoder as a second argument', function (st) {
|
664 | st.plan(1);
|
665 | qs.parse('a', {
|
666 | decoder: function (str, defaultDecoder) {
|
667 | st.equal(defaultDecoder, utils.decode);
|
668 | }
|
669 | });
|
670 | st.end();
|
671 | });
|
672 |
|
673 | t.test('throws error with wrong decoder', function (st) {
|
674 | st['throws'](function () {
|
675 | qs.parse({}, { decoder: 'string' });
|
676 | }, new TypeError('Decoder has to be a function.'));
|
677 | st.end();
|
678 | });
|
679 |
|
680 | t.test('does not mutate the options argument', function (st) {
|
681 | var options = {};
|
682 | qs.parse('a[b]=true', options);
|
683 | st.deepEqual(options, {});
|
684 | st.end();
|
685 | });
|
686 |
|
687 | t.test('throws if an invalid charset is specified', function (st) {
|
688 | st['throws'](function () {
|
689 | qs.parse('a=b', { charset: 'foobar' });
|
690 | }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
|
691 | st.end();
|
692 | });
|
693 |
|
694 | t.test('parses an iso-8859-1 string if asked to', function (st) {
|
695 | st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
|
696 | st.end();
|
697 | });
|
698 |
|
699 | var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
|
700 | var urlEncodedOSlashInUtf8 = '%C3%B8';
|
701 | var urlEncodedNumCheckmark = '%26%2310003%3B';
|
702 | var urlEncodedNumSmiley = '%26%239786%3B';
|
703 |
|
704 | t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
|
705 | st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
|
706 | st.end();
|
707 | });
|
708 |
|
709 | t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
|
710 | st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
|
711 | st.end();
|
712 | });
|
713 |
|
714 | t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
|
715 | st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
|
716 | st.end();
|
717 | });
|
718 |
|
719 | t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
|
720 | st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
|
721 | st.end();
|
722 | });
|
723 |
|
724 | t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
|
725 | st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
|
726 | st.end();
|
727 | });
|
728 |
|
729 | t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
|
730 | st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
|
731 | st.end();
|
732 | });
|
733 |
|
734 | t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
|
735 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
|
736 | st.end();
|
737 | });
|
738 |
|
739 | t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
|
740 | st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
|
741 | charset: 'iso-8859-1',
|
742 | decoder: function (str, defaultDecoder, charset) {
|
743 | return str ? defaultDecoder(str, defaultDecoder, charset) : null;
|
744 | },
|
745 | interpretNumericEntities: true
|
746 | }), { foo: null, bar: '☺' });
|
747 | st.end();
|
748 | });
|
749 |
|
750 | t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
|
751 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
|
752 | st.end();
|
753 | });
|
754 |
|
755 | t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
|
756 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '☺' });
|
757 | st.end();
|
758 | });
|
759 |
|
760 | t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
|
761 | st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
|
762 | st.end();
|
763 | });
|
764 |
|
765 | t.test('allows for decoding keys and values differently', function (st) {
|
766 | var decoder = function (str, defaultDecoder, charset, type) {
|
767 | if (type === 'key') {
|
768 | return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
|
769 | }
|
770 | if (type === 'value') {
|
771 | return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
|
772 | }
|
773 | throw 'this should never happen! type: ' + type;
|
774 | };
|
775 |
|
776 | st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
|
777 | st.end();
|
778 | });
|
779 |
|
780 | t.end();
|
781 | });
|