arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/body-parser/node_modules/qs/test/parse.js
node_modules/body-parser/node_modules/qs/test/parse.jsRaw
  1'use strict';
  2
  3var test = require('tape');
  4var qs = require('../');
  5var iconv = require('iconv-lite');
  6
  7test('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        st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c'), { a: { '0': 'b', t: 'u', c: true } });
125        st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y'), { a: { '0': 'b', '1': 'c', x: 'y' } });
126        st.end();
127    });
128
129    t.test('transforms arrays to objects (dot notation)', function (st) {
130        st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
131        st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
132        st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
133        st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
134        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' }] });
135        st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
136        st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
137        st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
138        st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
139        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' }] });
140        st.end();
141    });
142
143    t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');
144
145    t.test('correctly prunes undefined values when converting an array to an object', function (st) {
146        st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
147        st.end();
148    });
149
150    t.test('supports malformed uri characters', function (st) {
151        st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
152        st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
153        st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
154        st.end();
155    });
156
157    t.test('doesn\'t produce empty keys', function (st) {
158        st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
159        st.end();
160    });
161
162    t.test('cannot access Object prototype', function (st) {
163        qs.parse('constructor[prototype][bad]=bad');
164        qs.parse('bad[constructor][prototype][bad]=bad');
165        st.equal(typeof Object.prototype.bad, 'undefined');
166        st.end();
167    });
168
169    t.test('parses arrays of objects', function (st) {
170        st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
171        st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
172        st.end();
173    });
174
175    t.test('allows for empty strings in arrays', function (st) {
176        st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
177        st.deepEqual(qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true }), { a: ['b', null, 'c', ''] });
178        st.deepEqual(qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true }), { a: ['b', '', 'c', null] });
179        st.deepEqual(qs.parse('a[]=&a[]=b&a[]=c'), { a: ['', 'b', 'c'] });
180        st.end();
181    });
182
183    t.test('compacts sparse arrays', function (st) {
184        st.deepEqual(qs.parse('a[10]=1&a[2]=2'), { a: ['2', '1'] });
185        st.deepEqual(qs.parse('a[1][b][2][c]=1'), { a: [{ b: [{ c: '1' }] }] });
186        st.deepEqual(qs.parse('a[1][2][3][c]=1'), { a: [[[{ c: '1' }]]] });
187        st.deepEqual(qs.parse('a[1][2][3][c][1]=1'), { a: [[[{ c: ['1'] }]]] });
188        st.end();
189    });
190
191    t.test('parses semi-parsed strings', function (st) {
192        st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
193        st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
194        st.end();
195    });
196
197    t.test('parses buffers correctly', function (st) {
198        var b = new Buffer('test');
199        st.deepEqual(qs.parse({ a: b }), { a: b });
200        st.end();
201    });
202
203    t.test('continues parsing when no parent is found', function (st) {
204        st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
205        st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
206        st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
207        st.end();
208    });
209
210    t.test('does not error when parsing a very long array', function (st) {
211        var str = 'a[]=a';
212        while (Buffer.byteLength(str) < 128 * 1024) {
213            str = str + '&' + str;
214        }
215
216        st.doesNotThrow(function () { qs.parse(str); });
217
218        st.end();
219    });
220
221    t.test('should not throw when a native prototype has an enumerable property', { parallel: false }, function (st) {
222        Object.prototype.crash = '';
223        Array.prototype.crash = '';
224        st.doesNotThrow(qs.parse.bind(null, 'a=b'));
225        st.deepEqual(qs.parse('a=b'), { a: 'b' });
226        st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
227        st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
228        delete Object.prototype.crash;
229        delete Array.prototype.crash;
230        st.end();
231    });
232
233    t.test('parses a string with an alternative string delimiter', function (st) {
234        st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
235        st.end();
236    });
237
238    t.test('parses a string with an alternative RegExp delimiter', function (st) {
239        st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
240        st.end();
241    });
242
243    t.test('does not use non-splittable objects as delimiters', function (st) {
244        st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
245        st.end();
246    });
247
248    t.test('allows overriding parameter limit', function (st) {
249        st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
250        st.end();
251    });
252
253    t.test('allows setting the parameter limit to Infinity', function (st) {
254        st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
255        st.end();
256    });
257
258    t.test('allows overriding array limit', function (st) {
259        st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
260        st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
261        st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
262        st.end();
263    });
264
265    t.test('allows disabling array parsing', function (st) {
266        st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
267        st.end();
268    });
269
270    t.test('parses an object', function (st) {
271        var input = {
272            'user[name]': { 'pop[bob]': 3 },
273            'user[email]': null
274        };
275
276        var expected = {
277            user: {
278                name: { 'pop[bob]': 3 },
279                email: null
280            }
281        };
282
283        var result = qs.parse(input);
284
285        st.deepEqual(result, expected);
286        st.end();
287    });
288
289    t.test('parses an object in dot notation', function (st) {
290        var input = {
291            'user.name': { 'pop[bob]': 3 },
292            'user.email.': null
293        };
294
295        var expected = {
296            user: {
297                name: { 'pop[bob]': 3 },
298                email: null
299            }
300        };
301
302        var result = qs.parse(input, { allowDots: true });
303
304        st.deepEqual(result, expected);
305        st.end();
306    });
307
308    t.test('parses an object and not child values', function (st) {
309        var input = {
310            'user[name]': { 'pop[bob]': { 'test': 3 } },
311            'user[email]': null
312        };
313
314        var expected = {
315            user: {
316                name: { 'pop[bob]': { 'test': 3 } },
317                email: null
318            }
319        };
320
321        var result = qs.parse(input);
322
323        st.deepEqual(result, expected);
324        st.end();
325    });
326
327    t.test('does not blow up when Buffer global is missing', function (st) {
328        var tempBuffer = global.Buffer;
329        delete global.Buffer;
330        var result = qs.parse('a=b&c=d');
331        global.Buffer = tempBuffer;
332        st.deepEqual(result, { a: 'b', c: 'd' });
333        st.end();
334    });
335
336    t.test('does not crash when parsing circular references', function (st) {
337        var a = {};
338        a.b = a;
339
340        var parsed;
341
342        st.doesNotThrow(function () {
343            parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
344        });
345
346        st.equal('foo' in parsed, true, 'parsed has "foo" property');
347        st.equal('bar' in parsed.foo, true);
348        st.equal('baz' in parsed.foo, true);
349        st.equal(parsed.foo.bar, 'baz');
350        st.deepEqual(parsed.foo.baz, a);
351        st.end();
352    });
353
354    t.test('parses plain objects correctly', function (st) {
355        var a = Object.create(null);
356        a.b = 'c';
357
358        st.deepEqual(qs.parse(a), { b: 'c' });
359        var result = qs.parse({ a: a });
360        st.equal('a' in result, true, 'result has "a" property');
361        st.deepEqual(result.a, a);
362        st.end();
363    });
364
365    t.test('parses dates correctly', function (st) {
366        var now = new Date();
367        st.deepEqual(qs.parse({ a: now }), { a: now });
368        st.end();
369    });
370
371    t.test('parses regular expressions correctly', function (st) {
372        var re = /^test$/;
373        st.deepEqual(qs.parse({ a: re }), { a: re });
374        st.end();
375    });
376
377    t.test('can allow overwriting prototype properties', function (st) {
378        st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } }, { prototype: false });
379        st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' }, { prototype: false });
380        st.end();
381    });
382
383    t.test('can return plain objects', function (st) {
384        var expected = Object.create(null);
385        expected.a = Object.create(null);
386        expected.a.b = 'c';
387        expected.a.hasOwnProperty = 'd';
388        st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
389        st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
390        var expectedArray = Object.create(null);
391        expectedArray.a = Object.create(null);
392        expectedArray.a['0'] = 'b';
393        expectedArray.a.c = 'd';
394        st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
395        st.end();
396    });
397
398    t.test('can parse with custom encoding', function (st) {
399        st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
400            decoder: function (str) {
401                var reg = /\%([0-9A-F]{2})/ig;
402                var result = [];
403                var parts;
404                var last = 0;
405                while (parts = reg.exec(str)) {
406                    result.push(parseInt(parts[1], 16));
407                    last = parts.index + parts[0].length;
408                }
409                return iconv.decode(new Buffer(result), 'shift_jis').toString();
410            }
411        }), { 県: '大阪府' });
412        st.end();
413    });
414
415    t.test('throws error with wrong decoder', function (st) {
416        st.throws(function () {
417            qs.parse({}, {
418                decoder: 'string'
419            });
420        }, new TypeError('Decoder has to be a function.'));
421        st.end();
422    });
423});