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 semi-parsed strings', function (st) {
|
273 | st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
|
274 | st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
|
275 | st.end();
|
276 | });
|
277 |
|
278 | t.test('parses buffers correctly', function (st) {
|
279 | var b = SaferBuffer.from('test');
|
280 | st.deepEqual(qs.parse({ a: b }), { a: b });
|
281 | st.end();
|
282 | });
|
283 |
|
284 | t.test('parses jquery-param strings', function (st) {
|
285 |
|
286 | 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';
|
287 | var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
|
288 | st.deepEqual(qs.parse(encoded), expected);
|
289 | st.end();
|
290 | });
|
291 |
|
292 | t.test('continues parsing when no parent is found', function (st) {
|
293 | st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
|
294 | st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
|
295 | st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
|
296 | st.end();
|
297 | });
|
298 |
|
299 | t.test('does not error when parsing a very long array', function (st) {
|
300 | var str = 'a[]=a';
|
301 | while (Buffer.byteLength(str) < 128 * 1024) {
|
302 | str = str + '&' + str;
|
303 | }
|
304 |
|
305 | st.doesNotThrow(function () {
|
306 | qs.parse(str);
|
307 | });
|
308 |
|
309 | st.end();
|
310 | });
|
311 |
|
312 | t.test('should not throw when a native prototype has an enumerable property', function (st) {
|
313 | Object.prototype.crash = '';
|
314 | Array.prototype.crash = '';
|
315 | st.doesNotThrow(qs.parse.bind(null, 'a=b'));
|
316 | st.deepEqual(qs.parse('a=b'), { a: 'b' });
|
317 | st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
|
318 | st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
|
319 | delete Object.prototype.crash;
|
320 | delete Array.prototype.crash;
|
321 | st.end();
|
322 | });
|
323 |
|
324 | t.test('parses a string with an alternative string delimiter', function (st) {
|
325 | st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
|
326 | st.end();
|
327 | });
|
328 |
|
329 | t.test('parses a string with an alternative RegExp delimiter', function (st) {
|
330 | st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
|
331 | st.end();
|
332 | });
|
333 |
|
334 | t.test('does not use non-splittable objects as delimiters', function (st) {
|
335 | st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
|
336 | st.end();
|
337 | });
|
338 |
|
339 | t.test('allows overriding parameter limit', function (st) {
|
340 | st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
|
341 | st.end();
|
342 | });
|
343 |
|
344 | t.test('allows setting the parameter limit to Infinity', function (st) {
|
345 | st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
|
346 | st.end();
|
347 | });
|
348 |
|
349 | t.test('allows overriding array limit', function (st) {
|
350 | st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
|
351 | st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
|
352 | st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
353 | st.end();
|
354 | });
|
355 |
|
356 | t.test('allows disabling array parsing', function (st) {
|
357 | var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
|
358 | st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
|
359 | st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
|
360 |
|
361 | var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
|
362 | st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
|
363 | st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
|
364 |
|
365 | st.end();
|
366 | });
|
367 |
|
368 | t.test('allows for query string prefix', function (st) {
|
369 | st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
|
370 | st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
|
371 | st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
|
372 | st.end();
|
373 | });
|
374 |
|
375 | t.test('parses an object', function (st) {
|
376 | var input = {
|
377 | 'user[name]': { 'pop[bob]': 3 },
|
378 | 'user[email]': null
|
379 | };
|
380 |
|
381 | var expected = {
|
382 | user: {
|
383 | name: { 'pop[bob]': 3 },
|
384 | email: null
|
385 | }
|
386 | };
|
387 |
|
388 | var result = qs.parse(input);
|
389 |
|
390 | st.deepEqual(result, expected);
|
391 | st.end();
|
392 | });
|
393 |
|
394 | t.test('parses string with comma as array divider', function (st) {
|
395 | st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
|
396 | st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
|
397 | st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
|
398 | st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
|
399 | st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
|
400 | st.end();
|
401 | });
|
402 |
|
403 | t.test('parses values with comma as array divider', function (st) {
|
404 | st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
|
405 | st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
|
406 | st.end();
|
407 | });
|
408 |
|
409 | t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
|
410 | var decoder = function (str, defaultDecoder, charset, type) {
|
411 | if (!isNaN(Number(str))) {
|
412 | return parseFloat(str);
|
413 | }
|
414 | return defaultDecoder(str, defaultDecoder, charset, type);
|
415 | };
|
416 |
|
417 | st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
|
418 | st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
|
419 |
|
420 | st.end();
|
421 | });
|
422 |
|
423 | t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
|
424 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
|
425 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
|
426 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
|
427 | st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
|
428 |
|
429 | st.end();
|
430 | });
|
431 |
|
432 | t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
|
433 | st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
|
434 | st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
|
435 | st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
|
436 |
|
437 | st.end();
|
438 | });
|
439 |
|
440 | t.test('parses an object in dot notation', function (st) {
|
441 | var input = {
|
442 | 'user.name': { 'pop[bob]': 3 },
|
443 | 'user.email.': null
|
444 | };
|
445 |
|
446 | var expected = {
|
447 | user: {
|
448 | name: { 'pop[bob]': 3 },
|
449 | email: null
|
450 | }
|
451 | };
|
452 |
|
453 | var result = qs.parse(input, { allowDots: true });
|
454 |
|
455 | st.deepEqual(result, expected);
|
456 | st.end();
|
457 | });
|
458 |
|
459 | t.test('parses an object and not child values', function (st) {
|
460 | var input = {
|
461 | 'user[name]': { 'pop[bob]': { test: 3 } },
|
462 | 'user[email]': null
|
463 | };
|
464 |
|
465 | var expected = {
|
466 | user: {
|
467 | name: { 'pop[bob]': { test: 3 } },
|
468 | email: null
|
469 | }
|
470 | };
|
471 |
|
472 | var result = qs.parse(input);
|
473 |
|
474 | st.deepEqual(result, expected);
|
475 | st.end();
|
476 | });
|
477 |
|
478 | t.test('does not blow up when Buffer global is missing', function (st) {
|
479 | var tempBuffer = global.Buffer;
|
480 | delete global.Buffer;
|
481 | var result = qs.parse('a=b&c=d');
|
482 | global.Buffer = tempBuffer;
|
483 | st.deepEqual(result, { a: 'b', c: 'd' });
|
484 | st.end();
|
485 | });
|
486 |
|
487 | t.test('does not crash when parsing circular references', function (st) {
|
488 | var a = {};
|
489 | a.b = a;
|
490 |
|
491 | var parsed;
|
492 |
|
493 | st.doesNotThrow(function () {
|
494 | parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
|
495 | });
|
496 |
|
497 | st.equal('foo' in parsed, true, 'parsed has "foo" property');
|
498 | st.equal('bar' in parsed.foo, true);
|
499 | st.equal('baz' in parsed.foo, true);
|
500 | st.equal(parsed.foo.bar, 'baz');
|
501 | st.deepEqual(parsed.foo.baz, a);
|
502 | st.end();
|
503 | });
|
504 |
|
505 | t.test('does not crash when parsing deep objects', function (st) {
|
506 | var parsed;
|
507 | var str = 'foo';
|
508 |
|
509 | for (var i = 0; i < 5000; i++) {
|
510 | str += '[p]';
|
511 | }
|
512 |
|
513 | str += '=bar';
|
514 |
|
515 | st.doesNotThrow(function () {
|
516 | parsed = qs.parse(str, { depth: 5000 });
|
517 | });
|
518 |
|
519 | st.equal('foo' in parsed, true, 'parsed has "foo" property');
|
520 |
|
521 | var depth = 0;
|
522 | var ref = parsed.foo;
|
523 | while ((ref = ref.p)) {
|
524 | depth += 1;
|
525 | }
|
526 |
|
527 | st.equal(depth, 5000, 'parsed is 5000 properties deep');
|
528 |
|
529 | st.end();
|
530 | });
|
531 |
|
532 | t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
|
533 | var a = Object.create(null);
|
534 | a.b = 'c';
|
535 |
|
536 | st.deepEqual(qs.parse(a), { b: 'c' });
|
537 | var result = qs.parse({ a: a });
|
538 | st.equal('a' in result, true, 'result has "a" property');
|
539 | st.deepEqual(result.a, a);
|
540 | st.end();
|
541 | });
|
542 |
|
543 | t.test('parses dates correctly', function (st) {
|
544 | var now = new Date();
|
545 | st.deepEqual(qs.parse({ a: now }), { a: now });
|
546 | st.end();
|
547 | });
|
548 |
|
549 | t.test('parses regular expressions correctly', function (st) {
|
550 | var re = /^test$/;
|
551 | st.deepEqual(qs.parse({ a: re }), { a: re });
|
552 | st.end();
|
553 | });
|
554 |
|
555 | t.test('does not allow overwriting prototype properties', function (st) {
|
556 | st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
|
557 | st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
|
558 |
|
559 | st.deepEqual(
|
560 | qs.parse('toString', { allowPrototypes: false }),
|
561 | {},
|
562 | 'bare "toString" results in {}'
|
563 | );
|
564 |
|
565 | st.end();
|
566 | });
|
567 |
|
568 | t.test('can allow overwriting prototype properties', function (st) {
|
569 | st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
|
570 | st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
|
571 |
|
572 | st.deepEqual(
|
573 | qs.parse('toString', { allowPrototypes: true }),
|
574 | { toString: '' },
|
575 | 'bare "toString" results in { toString: "" }'
|
576 | );
|
577 |
|
578 | st.end();
|
579 | });
|
580 |
|
581 | t.test('params starting with a closing bracket', function (st) {
|
582 | st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
|
583 | st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
|
584 | st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
|
585 | st.end();
|
586 | });
|
587 |
|
588 | t.test('params starting with a starting bracket', function (st) {
|
589 | st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
|
590 | st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
|
591 | st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
|
592 | st.end();
|
593 | });
|
594 |
|
595 | t.test('add keys to objects', function (st) {
|
596 | st.deepEqual(
|
597 | qs.parse('a[b]=c&a=d'),
|
598 | { a: { b: 'c', d: true } },
|
599 | 'can add keys to objects'
|
600 | );
|
601 |
|
602 | st.deepEqual(
|
603 | qs.parse('a[b]=c&a=toString'),
|
604 | { a: { b: 'c' } },
|
605 | 'can not overwrite prototype'
|
606 | );
|
607 |
|
608 | st.deepEqual(
|
609 | qs.parse('a[b]=c&a=toString', { allowPrototypes: true }),
|
610 | { a: { b: 'c', toString: true } },
|
611 | 'can overwrite prototype with allowPrototypes true'
|
612 | );
|
613 |
|
614 | st.deepEqual(
|
615 | qs.parse('a[b]=c&a=toString', { plainObjects: true }),
|
616 | { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
|
617 | 'can overwrite prototype with plainObjects true'
|
618 | );
|
619 |
|
620 | st.end();
|
621 | });
|
622 |
|
623 | t.test('can return null objects', { skip: !Object.create }, function (st) {
|
624 | var expected = Object.create(null);
|
625 | expected.a = Object.create(null);
|
626 | expected.a.b = 'c';
|
627 | expected.a.hasOwnProperty = 'd';
|
628 | st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
|
629 | st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
|
630 | var expectedArray = Object.create(null);
|
631 | expectedArray.a = Object.create(null);
|
632 | expectedArray.a[0] = 'b';
|
633 | expectedArray.a.c = 'd';
|
634 | st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
|
635 | st.end();
|
636 | });
|
637 |
|
638 | t.test('can parse with custom encoding', function (st) {
|
639 | st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
|
640 | decoder: function (str) {
|
641 | var reg = /%([0-9A-F]{2})/ig;
|
642 | var result = [];
|
643 | var parts = reg.exec(str);
|
644 | while (parts) {
|
645 | result.push(parseInt(parts[1], 16));
|
646 | parts = reg.exec(str);
|
647 | }
|
648 | return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
|
649 | }
|
650 | }), { 県: '大阪府' });
|
651 | st.end();
|
652 | });
|
653 |
|
654 | t.test('receives the default decoder as a second argument', function (st) {
|
655 | st.plan(1);
|
656 | qs.parse('a', {
|
657 | decoder: function (str, defaultDecoder) {
|
658 | st.equal(defaultDecoder, utils.decode);
|
659 | }
|
660 | });
|
661 | st.end();
|
662 | });
|
663 |
|
664 | t.test('throws error with wrong decoder', function (st) {
|
665 | st['throws'](function () {
|
666 | qs.parse({}, { decoder: 'string' });
|
667 | }, new TypeError('Decoder has to be a function.'));
|
668 | st.end();
|
669 | });
|
670 |
|
671 | t.test('does not mutate the options argument', function (st) {
|
672 | var options = {};
|
673 | qs.parse('a[b]=true', options);
|
674 | st.deepEqual(options, {});
|
675 | st.end();
|
676 | });
|
677 |
|
678 | t.test('throws if an invalid charset is specified', function (st) {
|
679 | st['throws'](function () {
|
680 | qs.parse('a=b', { charset: 'foobar' });
|
681 | }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
|
682 | st.end();
|
683 | });
|
684 |
|
685 | t.test('parses an iso-8859-1 string if asked to', function (st) {
|
686 | st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
|
687 | st.end();
|
688 | });
|
689 |
|
690 | var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
|
691 | var urlEncodedOSlashInUtf8 = '%C3%B8';
|
692 | var urlEncodedNumCheckmark = '%26%2310003%3B';
|
693 | var urlEncodedNumSmiley = '%26%239786%3B';
|
694 |
|
695 | t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
|
696 | st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
|
697 | st.end();
|
698 | });
|
699 |
|
700 | t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
|
701 | st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
|
702 | st.end();
|
703 | });
|
704 |
|
705 | t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
|
706 | st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
|
707 | st.end();
|
708 | });
|
709 |
|
710 | t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
|
711 | st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
|
712 | st.end();
|
713 | });
|
714 |
|
715 | t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
|
716 | st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
|
717 | st.end();
|
718 | });
|
719 |
|
720 | t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
|
721 | st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
|
722 | st.end();
|
723 | });
|
724 |
|
725 | t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
|
726 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
|
727 | st.end();
|
728 | });
|
729 |
|
730 | t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
|
731 | st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
|
732 | charset: 'iso-8859-1',
|
733 | decoder: function (str, defaultDecoder, charset) {
|
734 | return str ? defaultDecoder(str, defaultDecoder, charset) : null;
|
735 | },
|
736 | interpretNumericEntities: true
|
737 | }), { foo: null, bar: '☺' });
|
738 | st.end();
|
739 | });
|
740 |
|
741 | t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
|
742 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
|
743 | st.end();
|
744 | });
|
745 |
|
746 | t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
|
747 | st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '☺' });
|
748 | st.end();
|
749 | });
|
750 |
|
751 | t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
|
752 | st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
|
753 | st.end();
|
754 | });
|
755 |
|
756 | t.test('allows for decoding keys and values differently', function (st) {
|
757 | var decoder = function (str, defaultDecoder, charset, type) {
|
758 | if (type === 'key') {
|
759 | return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
|
760 | }
|
761 | if (type === 'value') {
|
762 | return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
|
763 | }
|
764 | throw 'this should never happen! type: ' + type;
|
765 | };
|
766 |
|
767 | st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
|
768 | st.end();
|
769 | });
|
770 |
|
771 | t.end();
|
772 | });
|