Add Robot_JoyIt driver
This commit is contained in:
649
node_modules/qs/test/parse.js
generated
vendored
649
node_modules/qs/test/parse.js
generated
vendored
@@ -1,10 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
var test = require('tape');
|
||||
var hasPropertyDescriptors = require('has-property-descriptors')();
|
||||
var iconv = require('iconv-lite');
|
||||
var mockProperty = require('mock-property');
|
||||
var hasOverrideMistake = require('has-override-mistake')();
|
||||
var SaferBuffer = require('safer-buffer').Buffer;
|
||||
var v = require('es-value-fixtures');
|
||||
var inspect = require('object-inspect');
|
||||
var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
|
||||
var hasProto = require('has-proto')();
|
||||
|
||||
var qs = require('../');
|
||||
var utils = require('../lib/utils');
|
||||
var iconv = require('iconv-lite');
|
||||
var SaferBuffer = require('safer-buffer').Buffer;
|
||||
|
||||
test('parse()', function (t) {
|
||||
t.test('parses a simple string', function (st) {
|
||||
@@ -32,41 +40,156 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('arrayFormat: brackets allows only explicit arrays', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'brackets' }), { a: 'b,c' });
|
||||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'brackets' }), { a: ['b', 'c'] });
|
||||
t.test('comma: false', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c'), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c'), { a: 'b,c' });
|
||||
st.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] });
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('arrayFormat: indices allows only indexed arrays', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'indices' }), { a: 'b,c' });
|
||||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] });
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('arrayFormat: comma allows only comma-separated arrays', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' });
|
||||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('arrayFormat: repeat allows only repeated values', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'repeat' }), { a: 'b,c' });
|
||||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] });
|
||||
t.test('comma: true', function (st) {
|
||||
st.deepEqual(qs.parse('a[]=b&a[]=c', { comma: true }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { comma: true }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b,c', { comma: true }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b&a=c', { comma: true }), { a: ['b', 'c'] });
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('allows enabling dot notation', function (st) {
|
||||
st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' });
|
||||
st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } });
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('decode dot keys correctly', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: false, decodeDotInKeys: false }),
|
||||
{ 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
|
||||
'with allowDots false and decodeDotInKeys false'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('name.obj.first=John&name.obj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
|
||||
{ name: { obj: { first: 'John', last: 'Doe' } } },
|
||||
'with allowDots false and decodeDotInKeys false'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: false }),
|
||||
{ 'name%2Eobj': { first: 'John', last: 'Doe' } },
|
||||
'with allowDots true and decodeDotInKeys false'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { allowDots: true, decodeDotInKeys: true }),
|
||||
{ 'name.obj': { first: 'John', last: 'Doe' } },
|
||||
'with allowDots true and decodeDotInKeys true'
|
||||
);
|
||||
|
||||
st.deepEqual(
|
||||
qs.parse(
|
||||
'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
|
||||
{ allowDots: false, decodeDotInKeys: false }
|
||||
),
|
||||
{ 'name%2Eobj%2Esubobject.first%2Egodly%2Ename': 'John', 'name%2Eobj%2Esubobject.last': 'Doe' },
|
||||
'with allowDots false and decodeDotInKeys false'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse(
|
||||
'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
|
||||
{ allowDots: true, decodeDotInKeys: false }
|
||||
),
|
||||
{ name: { obj: { subobject: { first: { godly: { name: 'John' } }, last: 'Doe' } } } },
|
||||
'with allowDots true and decodeDotInKeys false'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse(
|
||||
'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
|
||||
{ allowDots: true, decodeDotInKeys: true }
|
||||
),
|
||||
{ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
|
||||
'with allowDots true and decodeDotInKeys true'
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe'),
|
||||
{ 'name%2Eobj.first': 'John', 'name%2Eobj.last': 'Doe' },
|
||||
'with allowDots and decodeDotInKeys undefined'
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('decodes dot in key of object, and allow enabling dot notation when decodeDotInKeys is set to true and allowDots is undefined', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse(
|
||||
'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
|
||||
{ decodeDotInKeys: true }
|
||||
),
|
||||
{ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
|
||||
'with allowDots undefined and decodeDotInKeys true'
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('throws when decodeDotInKeys is not of type boolean', function (st) {
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 'foobar' }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: 0 }); },
|
||||
TypeError
|
||||
);
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: NaN }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { decodeDotInKeys: null }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('allows empty arrays in obj values', function (st) {
|
||||
st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: true }), { foo: [], bar: 'baz' });
|
||||
st.deepEqual(qs.parse('foo[]&bar=baz', { allowEmptyArrays: false }), { foo: [''], bar: 'baz' });
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('throws when allowEmptyArrays is not of type boolean', function (st) {
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 'foobar' }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: 0 }); },
|
||||
TypeError
|
||||
);
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: NaN }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st['throws'](
|
||||
function () { qs.parse('foo[]&bar=baz', { allowEmptyArrays: null }); },
|
||||
TypeError
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('allowEmptyArrays + strictNullHandling', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('testEmptyArray[]', { strictNullHandling: true, allowEmptyArrays: true }),
|
||||
{ testEmptyArray: [] }
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
@@ -112,11 +235,11 @@ test('parse()', function (t) {
|
||||
st.deepEqual(qs.parse('a=b&a[0]=c'), { a: ['b', 'c'] });
|
||||
|
||||
st.deepEqual(qs.parse('a[1]=b&a=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a[]=b&a=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
||||
st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
|
||||
|
||||
st.deepEqual(qs.parse('a=b&a[1]=c', { arrayLimit: 20 }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: ['b', 'c'] });
|
||||
st.deepEqual(qs.parse('a=b&a[]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
||||
st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
|
||||
|
||||
st.end();
|
||||
@@ -241,7 +364,7 @@ test('parse()', function (t) {
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
|
||||
{ a: ['b', null, 'c', ''] },
|
||||
{ a: { 0: 'b', 1: null, 2: 'c', 3: '' } },
|
||||
'with arrayLimit 0 + array brackets: null then empty string works'
|
||||
);
|
||||
|
||||
@@ -252,7 +375,7 @@ test('parse()', function (t) {
|
||||
);
|
||||
st.deepEqual(
|
||||
qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
|
||||
{ a: ['b', '', 'c', null] },
|
||||
{ a: { 0: 'b', 1: '', 2: 'c', 3: null } },
|
||||
'with arrayLimit 0 + array brackets: empty string then null works'
|
||||
);
|
||||
|
||||
@@ -321,15 +444,15 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('should not throw when a native prototype has an enumerable property', function (st) {
|
||||
Object.prototype.crash = '';
|
||||
Array.prototype.crash = '';
|
||||
t.test('does not throw when a native prototype has an enumerable property', function (st) {
|
||||
st.intercept(Object.prototype, 'crash', { value: '' });
|
||||
st.intercept(Array.prototype, 'crash', { value: '' });
|
||||
|
||||
st.doesNotThrow(qs.parse.bind(null, 'a=b'));
|
||||
st.deepEqual(qs.parse('a=b'), { a: 'b' });
|
||||
st.doesNotThrow(qs.parse.bind(null, 'a[][b]=c'));
|
||||
st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
|
||||
delete Object.prototype.crash;
|
||||
delete Array.prototype.crash;
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
@@ -360,8 +483,14 @@ test('parse()', function (t) {
|
||||
|
||||
t.test('allows overriding array limit', function (st) {
|
||||
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
|
||||
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: 0 }), { a: ['b'] });
|
||||
|
||||
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
|
||||
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: 0 }), { a: { '-1': 'b' } });
|
||||
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: -1 }), { a: { 0: 'b', 1: 'c' } });
|
||||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
@@ -452,6 +581,15 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parses url-encoded brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
|
||||
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
|
||||
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=', { comma: true }), { foo: [['1', '2', '3'], ''] });
|
||||
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
|
||||
st.deepEqual(qs.parse('foo%5B%5D=1,2,3&foo%5B%5D=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parses comma delimited array while having percent-encoded comma treated as normal text', function (st) {
|
||||
st.deepEqual(qs.parse('foo=a%2Cb', { comma: true }), { foo: 'a,b' });
|
||||
st.deepEqual(qs.parse('foo=a%2C%20b,d', { comma: true }), { foo: ['a, b', 'd'] });
|
||||
@@ -499,10 +637,12 @@ test('parse()', function (t) {
|
||||
});
|
||||
|
||||
t.test('does not blow up when Buffer global is missing', function (st) {
|
||||
var tempBuffer = global.Buffer;
|
||||
delete global.Buffer;
|
||||
var restore = mockProperty(global, 'Buffer', { 'delete': true });
|
||||
|
||||
var result = qs.parse('a=b&c=d');
|
||||
global.Buffer = tempBuffer;
|
||||
|
||||
restore();
|
||||
|
||||
st.deepEqual(result, { a: 'b', c: 'd' });
|
||||
st.end();
|
||||
});
|
||||
@@ -552,9 +692,8 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parses null objects correctly', { skip: !Object.create }, function (st) {
|
||||
var a = Object.create(null);
|
||||
a.b = 'c';
|
||||
t.test('parses null objects correctly', { skip: !hasProto }, function (st) {
|
||||
var a = { __proto__: null, b: 'c' };
|
||||
|
||||
st.deepEqual(qs.parse(a), { b: 'c' });
|
||||
var result = qs.parse({ a: a });
|
||||
@@ -601,6 +740,34 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('does not crash when the global Object prototype is frozen', { skip: !hasPropertyDescriptors || !hasOverrideMistake }, function (st) {
|
||||
// We can't actually freeze the global Object prototype as that will interfere with other tests, and once an object is frozen, it
|
||||
// can't be unfrozen. Instead, we add a new non-writable property to simulate this.
|
||||
st.teardown(mockProperty(Object.prototype, 'frozenProp', { value: 'foo', nonWritable: true, nonEnumerable: true }));
|
||||
|
||||
st['throws'](
|
||||
function () {
|
||||
var obj = {};
|
||||
obj.frozenProp = 'bar';
|
||||
},
|
||||
// node < 6 has a different error message
|
||||
/^TypeError: Cannot assign to read only property 'frozenProp' of (?:object '#<Object>'|#<Object>)/,
|
||||
'regular assignment of an inherited non-writable property throws'
|
||||
);
|
||||
|
||||
var parsed;
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
parsed = qs.parse('frozenProp', { allowPrototypes: false });
|
||||
},
|
||||
'parsing a nonwritable Object.prototype property does not throw'
|
||||
);
|
||||
|
||||
st.deepEqual(parsed, {}, 'bare "frozenProp" results in {}');
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('params starting with a closing bracket', function (st) {
|
||||
st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
|
||||
st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
|
||||
@@ -703,17 +870,25 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('can return null objects', { skip: !Object.create }, function (st) {
|
||||
var expected = Object.create(null);
|
||||
expected.a = Object.create(null);
|
||||
expected.a.b = 'c';
|
||||
expected.a.hasOwnProperty = 'd';
|
||||
t.test('can return null objects', { skip: !hasProto }, function (st) {
|
||||
var expected = {
|
||||
__proto__: null,
|
||||
a: {
|
||||
__proto__: null,
|
||||
b: 'c',
|
||||
hasOwnProperty: 'd'
|
||||
}
|
||||
};
|
||||
st.deepEqual(qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true }), expected);
|
||||
st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
|
||||
var expectedArray = Object.create(null);
|
||||
expectedArray.a = Object.create(null);
|
||||
expectedArray.a[0] = 'b';
|
||||
expectedArray.a.c = 'd';
|
||||
st.deepEqual(qs.parse(null, { plainObjects: true }), { __proto__: null });
|
||||
var expectedArray = {
|
||||
__proto__: null,
|
||||
a: {
|
||||
__proto__: null,
|
||||
0: 'b',
|
||||
c: 'd'
|
||||
}
|
||||
};
|
||||
st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
|
||||
st.end();
|
||||
});
|
||||
@@ -790,7 +965,7 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('should ignore an utf8 sentinel with an unknown value', function (st) {
|
||||
t.test('ignores an utf8 sentinel with an unknown value', function (st) {
|
||||
st.deepEqual(qs.parse('utf8=foo&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { ø: 'ø' });
|
||||
st.end();
|
||||
});
|
||||
@@ -821,6 +996,20 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('handles a custom decoder returning `null`, with a string key of `null`', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('null=1&ToNull=2', {
|
||||
decoder: function (str, defaultDecoder, charset) {
|
||||
return str === 'ToNull' ? null : defaultDecoder(str, defaultDecoder, charset);
|
||||
}
|
||||
}),
|
||||
{ 'null': '1' },
|
||||
'"null" key is not overridden by `null` decoder result'
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('does not interpret numeric entities in iso-8859-1 when `interpretNumericEntities` is absent', function (st) {
|
||||
st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1' }), { foo: '☺' });
|
||||
st.end();
|
||||
@@ -831,6 +1020,15 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('interpretNumericEntities with comma:true and iso charset does not crash', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('b&a[]=1,' + urlEncodedNumSmiley, { comma: true, charset: 'iso-8859-1', interpretNumericEntities: true }),
|
||||
{ b: '', a: ['1,☺'] }
|
||||
);
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('does not interpret %uXXXX syntax in iso-8859-1 mode', function (st) {
|
||||
st.deepEqual(qs.parse('%u263A=%u263A', { charset: 'iso-8859-1' }), { '%u263A': '%u263A' });
|
||||
st.end();
|
||||
@@ -851,5 +1049,348 @@ test('parse()', function (t) {
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parameter limit tests', function (st) {
|
||||
st.test('does not throw error when within parameter limit', function (sst) {
|
||||
var result = qs.parse('a=1&b=2&c=3', { parameterLimit: 5, throwOnLimitExceeded: true });
|
||||
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses without errors');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('throws error when throwOnLimitExceeded is present but not boolean', function (sst) {
|
||||
sst['throws'](
|
||||
function () {
|
||||
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: 'true' });
|
||||
},
|
||||
new TypeError('`throwOnLimitExceeded` option must be a boolean'),
|
||||
'throws error when throwOnLimitExceeded is present and not boolean'
|
||||
);
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('throws error when parameter limit exceeded', function (sst) {
|
||||
sst['throws'](
|
||||
function () {
|
||||
qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: 3, throwOnLimitExceeded: true });
|
||||
},
|
||||
new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),
|
||||
'throws error when parameter limit is exceeded'
|
||||
);
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('silently truncates when throwOnLimitExceeded is not given', function (sst) {
|
||||
var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3 });
|
||||
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('silently truncates when parameter limit exceeded without error', function (sst) {
|
||||
var result = qs.parse('a=1&b=2&c=3&d=4&e=5', { parameterLimit: 3, throwOnLimitExceeded: false });
|
||||
sst.deepEqual(result, { a: '1', b: '2', c: '3' }, 'parses and truncates silently');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('allows unlimited parameters when parameterLimit set to Infinity', function (sst) {
|
||||
var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity });
|
||||
sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('array limit tests', function (st) {
|
||||
st.test('does not throw error when array is within limit', function (sst) {
|
||||
var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 5, throwOnLimitExceeded: true });
|
||||
sst.deepEqual(result, { a: ['1', '2', '3'] }, 'parses array without errors');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('throws error when throwOnLimitExceeded is present but not boolean for array limit', function (sst) {
|
||||
sst['throws'](
|
||||
function () {
|
||||
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: 'true' });
|
||||
},
|
||||
new TypeError('`throwOnLimitExceeded` option must be a boolean'),
|
||||
'throws error when throwOnLimitExceeded is present and not boolean for array limit'
|
||||
);
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('throws error when array limit exceeded', function (sst) {
|
||||
sst['throws'](
|
||||
function () {
|
||||
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3, throwOnLimitExceeded: true });
|
||||
},
|
||||
new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
|
||||
'throws error when array limit is exceeded'
|
||||
);
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.test('converts array to object if length is greater than limit', function (sst) {
|
||||
var result = qs.parse('a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5&a[6]=6', { arrayLimit: 5 });
|
||||
|
||||
sst.deepEqual(result, { a: { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6' } }, 'parses into object if array length is greater than limit');
|
||||
sst.end();
|
||||
});
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('parses empty keys', function (t) {
|
||||
emptyTestCases.forEach(function (testCase) {
|
||||
t.test('skips empty string key with ' + testCase.input, function (st) {
|
||||
st.deepEqual(qs.parse(testCase.input), testCase.noEmptyKeys);
|
||||
|
||||
st.end();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('`duplicates` option', function (t) {
|
||||
v.nonStrings.concat('not a valid option').forEach(function (invalidOption) {
|
||||
if (typeof invalidOption !== 'undefined') {
|
||||
t['throws'](
|
||||
function () { qs.parse('', { duplicates: invalidOption }); },
|
||||
TypeError,
|
||||
'throws on invalid option: ' + inspect(invalidOption)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
t.deepEqual(
|
||||
qs.parse('foo=bar&foo=baz'),
|
||||
{ foo: ['bar', 'baz'] },
|
||||
'duplicates: default, combine'
|
||||
);
|
||||
|
||||
t.deepEqual(
|
||||
qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }),
|
||||
{ foo: ['bar', 'baz'] },
|
||||
'duplicates: combine'
|
||||
);
|
||||
|
||||
t.deepEqual(
|
||||
qs.parse('foo=bar&foo=baz', { duplicates: 'first' }),
|
||||
{ foo: 'bar' },
|
||||
'duplicates: first'
|
||||
);
|
||||
|
||||
t.deepEqual(
|
||||
qs.parse('foo=bar&foo=baz', { duplicates: 'last' }),
|
||||
{ foo: 'baz' },
|
||||
'duplicates: last'
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('qs strictDepth option - throw cases', function (t) {
|
||||
t.test('throws an exception when depth exceeds the limit with strictDepth: true', function (st) {
|
||||
st['throws'](
|
||||
function () {
|
||||
qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
|
||||
},
|
||||
RangeError,
|
||||
'throws RangeError'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('throws an exception for multiple nested arrays with strictDepth: true', function (st) {
|
||||
st['throws'](
|
||||
function () {
|
||||
qs.parse('a[0][1][2][3][4]=b', { depth: 3, strictDepth: true });
|
||||
},
|
||||
RangeError,
|
||||
'throws RangeError'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('throws an exception for nested objects and arrays with strictDepth: true', function (st) {
|
||||
st['throws'](
|
||||
function () {
|
||||
qs.parse('a[b][c][0][d][e]=f', { depth: 3, strictDepth: true });
|
||||
},
|
||||
RangeError,
|
||||
'throws RangeError'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('throws an exception for different types of values with strictDepth: true', function (st) {
|
||||
st['throws'](
|
||||
function () {
|
||||
qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 3, strictDepth: true });
|
||||
},
|
||||
RangeError,
|
||||
'throws RangeError'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
test('qs strictDepth option - non-throw cases', function (t) {
|
||||
t.test('when depth is 0 and strictDepth true, do not throw', function (st) {
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
qs.parse('a[b][c][d][e]=true&a[b][c][d][f]=42', { depth: 0, strictDepth: true });
|
||||
},
|
||||
RangeError,
|
||||
'does not throw RangeError'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parses successfully when depth is within the limit with strictDepth: true', function (st) {
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
var result = qs.parse('a[b]=c', { depth: 1, strictDepth: true });
|
||||
st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
|
||||
}
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('does not throw an exception when depth exceeds the limit with strictDepth: false', function (st) {
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
var result = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
|
||||
st.deepEqual(result, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }, 'parses with depth limit');
|
||||
}
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('parses successfully when depth is within the limit with strictDepth: false', function (st) {
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
var result = qs.parse('a[b]=c', { depth: 1 });
|
||||
st.deepEqual(result, { a: { b: 'c' } }, 'parses correctly');
|
||||
}
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('does not throw when depth is exactly at the limit with strictDepth: true', function (st) {
|
||||
st.doesNotThrow(
|
||||
function () {
|
||||
var result = qs.parse('a[b][c]=d', { depth: 2, strictDepth: true });
|
||||
st.deepEqual(result, { a: { b: { c: 'd' } } }, 'parses correctly');
|
||||
}
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('DOS', function (t) {
|
||||
var arr = [];
|
||||
for (var i = 0; i < 105; i++) {
|
||||
arr[arr.length] = 'x';
|
||||
}
|
||||
var attack = 'a[]=' + arr.join('&a[]=');
|
||||
var result = qs.parse(attack, { arrayLimit: 100 });
|
||||
|
||||
t.notOk(Array.isArray(result.a), 'arrayLimit is respected: result is an object, not an array');
|
||||
t.equal(Object.keys(result.a).length, 105, 'all values are preserved');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('arrayLimit boundary conditions', function (t) {
|
||||
t.test('exactly at the limit stays as array', function (st) {
|
||||
var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3 });
|
||||
st.ok(Array.isArray(result.a), 'result is an array when exactly at limit');
|
||||
st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('one over the limit converts to object', function (st) {
|
||||
var result = qs.parse('a[]=1&a[]=2&a[]=3&a[]=4', { arrayLimit: 3 });
|
||||
st.notOk(Array.isArray(result.a), 'result is not an array when over limit');
|
||||
st.deepEqual(result.a, { 0: '1', 1: '2', 2: '3', 3: '4' }, 'all values preserved as object');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('arrayLimit 1 with two values', function (st) {
|
||||
var result = qs.parse('a[]=1&a[]=2', { arrayLimit: 1 });
|
||||
st.notOk(Array.isArray(result.a), 'result is not an array');
|
||||
st.deepEqual(result.a, { 0: '1', 1: '2' }, 'both values preserved');
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('mixed array and object notation', function (t) {
|
||||
t.test('array brackets with object key - under limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a[]=b&a[c]=d'),
|
||||
{ a: { 0: 'b', c: 'd' } },
|
||||
'mixing [] and [key] converts to object'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('array index with object key - under limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a[0]=b&a[c]=d'),
|
||||
{ a: { 0: 'b', c: 'd' } },
|
||||
'mixing [0] and [key] produces object'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('plain value with array brackets - under limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a=b&a[]=c', { arrayLimit: 20 }),
|
||||
{ a: ['b', 'c'] },
|
||||
'plain value combined with [] stays as array under limit'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('array brackets with plain value - under limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a[]=b&a=c', { arrayLimit: 20 }),
|
||||
{ a: ['b', 'c'] },
|
||||
'[] combined with plain value stays as array under limit'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('plain value with array index - under limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a=b&a[0]=c', { arrayLimit: 20 }),
|
||||
{ a: ['b', 'c'] },
|
||||
'plain value combined with [0] stays as array under limit'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('multiple plain values with duplicates combine', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a=b&a=c&a=d', { arrayLimit: 20 }),
|
||||
{ a: ['b', 'c', 'd'] },
|
||||
'duplicate plain keys combine into array'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('multiple plain values exceeding limit', function (st) {
|
||||
st.deepEqual(
|
||||
qs.parse('a=b&a=c&a=d', { arrayLimit: 2 }),
|
||||
{ a: { 0: 'b', 1: 'c', 2: 'd' } },
|
||||
'duplicate plain keys convert to object when exceeding limit'
|
||||
);
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user