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