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