]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/qs/test/parse.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / qs / test / parse.js
1 'use strict';
2
3 var test = require('tape');
4 var qs = require('../');
5 var iconv = require('iconv-lite');
6
7 test('parse()', function (t) {
8     t.test('parses a simple string', function (st) {
9         st.deepEqual(qs.parse('0=foo'), { '0': 'foo' });
10         st.deepEqual(qs.parse('foo=c++'), { foo: 'c  ' });
11         st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
12         st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
13         st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
14         st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
15         st.deepEqual(qs.parse('foo'), { foo: '' });
16         st.deepEqual(qs.parse('foo='), { foo: '' });
17         st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
18         st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
19         st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
20         st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
21         st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
22         st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
23         st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
24         st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
25             cht: 'p3',
26             chd: 't:60,40',
27             chs: '250x100',
28             chl: 'Hello|World'
29         });
30         st.end();
31     });
32
33     t.test('allows enabling dot notation', function (st) {
34         st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
35         st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
36         st.end();
37     });
38
39     t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
40     t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
41     t.deepEqual(
42         qs.parse('a[b][c][d][e][f][g][h]=i'),
43         { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
44         'defaults to a depth of 5'
45     );
46
47     t.test('only parses one level when depth = 1', function (st) {
48         st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
49         st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
50         st.end();
51     });
52
53     t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
54
55     t.test('parses an explicit array', function (st) {
56         st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
57         st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
58         st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
59         st.end();
60     });
61
62     t.test('parses a mix of simple and explicit arrays', function (st) {
63         st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
64         st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
65         st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
66         st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
67         st.deepEqual(qs.parse('a[1]=b&a=c'), { a: ['b', 'c'] });
68         st.deepEqual(qs.parse('a=b&a[1]=c'), { a: ['b', 'c'] });
69         st.end();
70     });
71
72     t.test('parses a nested array', function (st) {
73         st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
74         st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
75         st.end();
76     });
77
78     t.test('allows to specify array indices', function (st) {
79         st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
80         st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
81         st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
82         st.end();
83     });
84
85     t.test('limits specific array indices to 20', function (st) {
86         st.deepEqual(qs.parse('a[20]=a'), { a: ['a'] });
87         st.deepEqual(qs.parse('a[21]=a'), { a: { '21': 'a' } });
88         st.end();
89     });
90
91     t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
92
93     t.test('supports encoded = signs', function (st) {
94         st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
95         st.end();
96     });
97
98     t.test('is ok with url encoded strings', function (st) {
99         st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
100         st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
101         st.end();
102     });
103
104     t.test('allows brackets in the value', function (st) {
105         st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
106         st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
107         st.end();
108     });
109
110     t.test('allows empty values', function (st) {
111         st.deepEqual(qs.parse(''), {});
112         st.deepEqual(qs.parse(null), {});
113         st.deepEqual(qs.parse(undefined), {});
114         st.end();
115     });
116
117     t.test('transforms arrays to objects', function (st) {
118         st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
119         st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
120         st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
121         st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
122         st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
123         st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
124
125         st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { '0': 'b', c: true, t: 'u' } });
126         st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } });
127         st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { '0': 'b', '1': 'c', x: 'y' } });
128         st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } });
129         st.end();
130     });
131
132     t.test('transforms arrays to objects (dot notation)', function (st) {
133         st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
134         st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
135         st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
136         st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
137         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' }] });
138         st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
139         st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
140         st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
141         st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
142         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' }] });
143         st.end();
144     });
145
146     t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');
147
148     t.test('correctly prunes undefined values when converting an array to an object', function (st) {
149         st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
150         st.end();
151     });
152
153     t.test('supports malformed uri characters', function (st) {
154         st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
155         st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
156         st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
157         st.end();
158     });
159
160     t.test('doesn\'t produce empty keys', function (st) {
161         st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
162         st.end();
163     });
164
165     t.test('cannot access Object prototype', function (st) {
166         qs.parse('constructor[prototype][bad]=bad');
167         qs.parse('bad[constructor][prototype][bad]=bad');
168         st.equal(typeof Object.prototype.bad, 'undefined');
169         st.end();
170     });
171
172     t.test('parses arrays of objects', function (st) {
173         st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
174         st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
175         st.end();
176     });
177
178     t.test('allows for empty strings in arrays', function (st) {
179         st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
180
181         st.deepEqual(
182             qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
183             { a: ['b', null, 'c', ''] },
184             'with arrayLimit 20 + array indices: null then empty string works'
185         );
186         st.deepEqual(
187             qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
188             { a: ['b', null, 'c', ''] },
189             'with arrayLimit 0 + array brackets: null then empty string works'
190         );
191
192         st.deepEqual(
193             qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
194             { a: ['b', '', 'c', null] },
195             'with arrayLimit 20 + array indices: empty string then null works'
196         );
197         st.deepEqual(
198             qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
199             { a: ['b', '', 'c', null] },
200             'with arrayLimit 0 + array brackets: empty string then null works'
201         );
202
203         st.deepEqual(
204             qs.parse('a[]=&a[]=b&a[]=c'),
205             { a: ['', 'b', 'c'] },
206             'array brackets: empty strings work'
207         );
208         st.end();
209     });
210
211     t.test('compacts sparse arrays', function (st) {
212         st.deepEqual(qs.parse('a[10]=1&a[2]=2'), { a: ['2', '1'] });
213         st.deepEqual(qs.parse('a[1][b][2][c]=1'), { a: [{ b: [{ c: '1' }] }] });
214         st.deepEqual(qs.parse('a[1][2][3][c]=1'), { a: [[[{ c: '1' }]]] });
215         st.deepEqual(qs.parse('a[1][2][3][c][1]=1'), { a: [[[{ c: ['1'] }]]] });
216         st.end();
217     });
218
219     t.test('parses semi-parsed strings', function (st) {
220         st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
221         st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
222         st.end();
223     });
224
225     t.test('parses buffers correctly', function (st) {
226         var b = new Buffer('test');
227         st.deepEqual(qs.parse({ a: b }), { a: b });
228         st.end();
229     });
230
231     t.test('continues parsing when no parent is found', function (st) {
232         st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
233         st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
234         st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
235         st.end();
236     });
237
238     t.test('does not error when parsing a very long array', function (st) {
239         var str = 'a[]=a';
240         while (Buffer.byteLength(str) < 128 * 1024) {
241             str = str + '&' + str;
242         }
243
244         st.doesNotThrow(function () { qs.parse(str); });
245
246         st.end();
247     });
248
249     t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
250         Object.prototype.crash = '';
251         Array.prototype.crash = '';
252         st.doesNotThrow(qs.parse.bind(null, 'a=b'));
253         st.deepEqual(qs.parse('a=b'), { a: 'b' });
254         st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
255         st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
256         delete Object.prototype.crash;
257         delete Array.prototype.crash;
258         st.end();
259     });
260
261     t.test('parses a string with an alternative string delimiter', function (st) {
262         st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
263         st.end();
264     });
265
266     t.test('parses a string with an alternative RegExp delimiter', function (st) {
267         st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
268         st.end();
269     });
270
271     t.test('does not use non-splittable objects as delimiters', function (st) {
272         st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
273         st.end();
274     });
275
276     t.test('allows overriding parameter limit', function (st) {
277         st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
278         st.end();
279     });
280
281     t.test('allows setting the parameter limit to Infinity', function (st) {
282         st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
283         st.end();
284     });
285
286     t.test('allows overriding array limit', function (st) {
287         st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
288         st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
289         st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
290         st.end();
291     });
292
293     t.test('allows disabling array parsing', function (st) {
294         st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
295         st.end();
296     });
297
298     t.test('parses an object', function (st) {
299         var input = {
300             'user[name]': { 'pop[bob]': 3 },
301             'user[email]': null
302         };
303
304         var expected = {
305             user: {
306                 name: { 'pop[bob]': 3 },
307                 email: null
308             }
309         };
310
311         var result = qs.parse(input);
312
313         st.deepEqual(result, expected);
314         st.end();
315     });
316
317     t.test('parses an object in dot notation', function (st) {
318         var input = {
319             'user.name': { 'pop[bob]': 3 },
320             'user.email.': null
321         };
322
323         var expected = {
324             user: {
325                 name: { 'pop[bob]': 3 },
326                 email: null
327             }
328         };
329
330         var result = qs.parse(input, { allowDots: true });
331
332         st.deepEqual(result, expected);
333         st.end();
334     });
335
336     t.test('parses an object and not child values', function (st) {
337         var input = {
338             'user[name]': { 'pop[bob]': { 'test': 3 } },
339             'user[email]': null
340         };
341
342         var expected = {
343             user: {
344                 name: { 'pop[bob]': { 'test': 3 } },
345                 email: null
346             }
347         };
348
349         var result = qs.parse(input);
350
351         st.deepEqual(result, expected);
352         st.end();
353     });
354
355     t.test('does not blow up when Buffer global is missing', function (st) {
356         var tempBuffer = global.Buffer;
357         delete global.Buffer;
358         var result = qs.parse('a=b&c=d');
359         global.Buffer = tempBuffer;
360         st.deepEqual(result, { a: 'b', c: 'd' });
361         st.end();
362     });
363
364     t.test('does not crash when parsing circular references', function (st) {
365         var a = {};
366         a.b = a;
367
368         var parsed;
369
370         st.doesNotThrow(function () {
371             parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
372         });
373
374         st.equal('foo' in parsed, true, 'parsed has "foo" property');
375         st.equal('bar' in parsed.foo, true);
376         st.equal('baz' in parsed.foo, true);
377         st.equal(parsed.foo.bar, 'baz');
378         st.deepEqual(parsed.foo.baz, a);
379         st.end();
380     });
381
382     t.test('parses plain objects correctly', function (st) {
383         var a = Object.create(null);
384         a.b = 'c';
385
386         st.deepEqual(qs.parse(a), { b: 'c' });
387         var result = qs.parse({ a: a });
388         st.equal('a' in result, true, 'result has "a" property');
389         st.deepEqual(result.a, a);
390         st.end();
391     });
392
393     t.test('parses dates correctly', function (st) {
394         var now = new Date();
395         st.deepEqual(qs.parse({ a: now }), { a: now });
396         st.end();
397     });
398
399     t.test('parses regular expressions correctly', function (st) {
400         var re = /^test$/;
401         st.deepEqual(qs.parse({ a: re }), { a: re });
402         st.end();
403     });
404
405     t.test('can allow overwriting prototype properties', function (st) {
406         st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } }, { prototype: false });
407         st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' }, { prototype: false });
408         st.end();
409     });
410
411     t.test('can return plain objects', function (st) {
412         var expected = Object.create(null);
413         expected.a = Object.create(null);
414         expected.a.b = 'c';
415         expected.a.hasOwnProperty = 'd';
416         st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
417         st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
418         var expectedArray = Object.create(null);
419         expectedArray.a = Object.create(null);
420         expectedArray.a['0'] = 'b';
421         expectedArray.a.c = 'd';
422         st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
423         st.end();
424     });
425
426     t.test('can parse with custom encoding', function (st) {
427         st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
428             decoder: function (str) {
429                 var reg = /\%([0-9A-F]{2})/ig;
430                 var result = [];
431                 var parts;
432                 var last = 0;
433                 while (parts = reg.exec(str)) {
434                     result.push(parseInt(parts[1], 16));
435                     last = parts.index + parts[0].length;
436                 }
437                 return iconv.decode(new Buffer(result), 'shift_jis').toString();
438             }
439         }), { 県: '大阪府' });
440         st.end();
441     });
442
443     t.test('throws error with wrong decoder', function (st) {
444         st.throws(function () {
445             qs.parse({}, {
446                 decoder: 'string'
447             });
448         }, new TypeError('Decoder has to be a function.'));
449         st.end();
450     });
451 });