aboutsummaryrefslogtreecommitdiffstats
path: root/node_modules/qs/test
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/qs/test')
-rw-r--r--node_modules/qs/test/empty-keys-cases.js267
-rw-r--r--node_modules/qs/test/parse.js1933
-rw-r--r--node_modules/qs/test/stringify.js1448
-rw-r--r--node_modules/qs/test/utils.js626
4 files changed, 4274 insertions, 0 deletions
diff --git a/node_modules/qs/test/empty-keys-cases.js b/node_modules/qs/test/empty-keys-cases.js
new file mode 100644
index 0000000..2b1190e
--- /dev/null
+++ b/node_modules/qs/test/empty-keys-cases.js
@@ -0,0 +1,267 @@
+'use strict';
+
+module.exports = {
+ emptyTestCases: [
+ {
+ input: '&',
+ withEmptyKeys: {},
+ stringifyOutput: {
+ brackets: '',
+ indices: '',
+ repeat: ''
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '&&',
+ withEmptyKeys: {},
+ stringifyOutput: {
+ brackets: '',
+ indices: '',
+ repeat: ''
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '&=',
+ withEmptyKeys: { '': '' },
+ stringifyOutput: {
+ brackets: '=',
+ indices: '=',
+ repeat: '='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '&=&',
+ withEmptyKeys: { '': '' },
+ stringifyOutput: {
+ brackets: '=',
+ indices: '=',
+ repeat: '='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '&=&=',
+ withEmptyKeys: { '': ['', ''] },
+ stringifyOutput: {
+ brackets: '[]=&[]=',
+ indices: '[0]=&[1]=',
+ repeat: '=&='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '&=&=&',
+ withEmptyKeys: { '': ['', ''] },
+ stringifyOutput: {
+ brackets: '[]=&[]=',
+ indices: '[0]=&[1]=',
+ repeat: '=&='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '=',
+ withEmptyKeys: { '': '' },
+ noEmptyKeys: {},
+ stringifyOutput: {
+ brackets: '=',
+ indices: '=',
+ repeat: '='
+ }
+ },
+ {
+ input: '=&',
+ withEmptyKeys: { '': '' },
+ stringifyOutput: {
+ brackets: '=',
+ indices: '=',
+ repeat: '='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '=&&&',
+ withEmptyKeys: { '': '' },
+ stringifyOutput: {
+ brackets: '=',
+ indices: '=',
+ repeat: '='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '=&=&=&',
+ withEmptyKeys: { '': ['', '', ''] },
+ stringifyOutput: {
+ brackets: '[]=&[]=&[]=',
+ indices: '[0]=&[1]=&[2]=',
+ repeat: '=&=&='
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '=&a[]=b&a[1]=c',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: '=a',
+ withEmptyKeys: { '': 'a' },
+ noEmptyKeys: {},
+ stringifyOutput: {
+ brackets: '=a',
+ indices: '=a',
+ repeat: '=a'
+ }
+ },
+ {
+ input: 'a==a',
+ withEmptyKeys: { a: '=a' },
+ noEmptyKeys: { a: '=a' },
+ stringifyOutput: {
+ brackets: 'a==a',
+ indices: 'a==a',
+ repeat: 'a==a'
+ }
+ },
+ {
+ input: '=&a[]=b',
+ withEmptyKeys: { '': '', a: ['b'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b',
+ indices: '=&a[0]=b',
+ repeat: '=&a=b'
+ },
+ noEmptyKeys: { a: ['b'] }
+ },
+ {
+ input: '=&a[]=b&a[]=c&a[2]=d',
+ withEmptyKeys: { '': '', a: ['b', 'c', 'd'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c&a[]=d',
+ indices: '=&a[0]=b&a[1]=c&a[2]=d',
+ repeat: '=&a=b&a=c&a=d'
+ },
+ noEmptyKeys: { a: ['b', 'c', 'd'] }
+ },
+ {
+ input: '=a&=b',
+ withEmptyKeys: { '': ['a', 'b'] },
+ stringifyOutput: {
+ brackets: '[]=a&[]=b',
+ indices: '[0]=a&[1]=b',
+ repeat: '=a&=b'
+ },
+ noEmptyKeys: {}
+ },
+ {
+ input: '=a&foo=b',
+ withEmptyKeys: { '': 'a', foo: 'b' },
+ noEmptyKeys: { foo: 'b' },
+ stringifyOutput: {
+ brackets: '=a&foo=b',
+ indices: '=a&foo=b',
+ repeat: '=a&foo=b'
+ }
+ },
+ {
+ input: 'a[]=b&a=c&=',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: 'a[]=b&a=c&=',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: 'a[0]=b&a=c&=',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: 'a=b&a[]=c&=',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: 'a=b&a[0]=c&=',
+ withEmptyKeys: { '': '', a: ['b', 'c'] },
+ stringifyOutput: {
+ brackets: '=&a[]=b&a[]=c',
+ indices: '=&a[0]=b&a[1]=c',
+ repeat: '=&a=b&a=c'
+ },
+ noEmptyKeys: { a: ['b', 'c'] }
+ },
+ {
+ input: '[]=a&[]=b& []=1',
+ withEmptyKeys: { '': ['a', 'b'], ' ': ['1'] },
+ stringifyOutput: {
+ brackets: '[]=a&[]=b& []=1',
+ indices: '[0]=a&[1]=b& [0]=1',
+ repeat: '=a&=b& =1'
+ },
+ noEmptyKeys: { 0: 'a', 1: 'b', ' ': ['1'] }
+ },
+ {
+ input: '[0]=a&[1]=b&a[0]=1&a[1]=2',
+ withEmptyKeys: { '': ['a', 'b'], a: ['1', '2'] },
+ noEmptyKeys: { 0: 'a', 1: 'b', a: ['1', '2'] },
+ stringifyOutput: {
+ brackets: '[]=a&[]=b&a[]=1&a[]=2',
+ indices: '[0]=a&[1]=b&a[0]=1&a[1]=2',
+ repeat: '=a&=b&a=1&a=2'
+ }
+ },
+ {
+ input: '[deep]=a&[deep]=2',
+ withEmptyKeys: { '': { deep: ['a', '2'] }
+ },
+ stringifyOutput: {
+ brackets: '[deep][]=a&[deep][]=2',
+ indices: '[deep][0]=a&[deep][1]=2',
+ repeat: '[deep]=a&[deep]=2'
+ },
+ noEmptyKeys: { deep: ['a', '2'] }
+ },
+ {
+ input: '%5B0%5D=a&%5B1%5D=b',
+ withEmptyKeys: { '': ['a', 'b'] },
+ stringifyOutput: {
+ brackets: '[]=a&[]=b',
+ indices: '[0]=a&[1]=b',
+ repeat: '=a&=b'
+ },
+ noEmptyKeys: { 0: 'a', 1: 'b' }
+ }
+ ]
+};
diff --git a/node_modules/qs/test/parse.js b/node_modules/qs/test/parse.js
new file mode 100644
index 0000000..17f30b6
--- /dev/null
+++ b/node_modules/qs/test/parse.js
@@ -0,0 +1,1933 @@
+'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 characterizeParse = function characterizeParse(st, input, opts, expected, label) {
+ var result;
+ st.doesNotThrow(function () { result = qs.parse(input, opts); }, label + ': does not throw');
+ st.deepEqual(result, expected, label + ': parses to the current lenient output');
+};
+
+test('parse()', function (t) {
+ t.test('parses a simple string', function (st) {
+ st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
+ st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
+ st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
+ st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
+ st.deepEqual(qs.parse('a[==]=23'), { a: { '==': '23' } });
+ st.deepEqual(qs.parse('foo', { strictNullHandling: true }), { foo: null });
+ st.deepEqual(qs.parse('foo'), { foo: '' });
+ st.deepEqual(qs.parse('foo='), { foo: '' });
+ st.deepEqual(qs.parse('foo=bar'), { foo: 'bar' });
+ st.deepEqual(qs.parse(' foo = bar = baz '), { ' foo ': ' bar = baz ' });
+ st.deepEqual(qs.parse('foo=bar=baz'), { foo: 'bar=baz' });
+ st.deepEqual(qs.parse('foo=bar&bar=baz'), { foo: 'bar', bar: 'baz' });
+ st.deepEqual(qs.parse('foo2=bar2&baz2='), { foo2: 'bar2', baz2: '' });
+ st.deepEqual(qs.parse('foo=bar&baz', { strictNullHandling: true }), { foo: 'bar', baz: null });
+ st.deepEqual(qs.parse('foo=bar&baz'), { foo: 'bar', baz: '' });
+ st.deepEqual(qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World'), {
+ cht: 'p3',
+ chd: 't:60,40',
+ chs: '250x100',
+ chl: 'Hello|World'
+ });
+ st.end();
+ });
+
+ 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('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();
+ });
+
+ t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string');
+ t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string');
+ t.deepEqual(
+ qs.parse('a[b][c][d][e][f][g][h]=i'),
+ { a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } },
+ 'defaults to a depth of 5'
+ );
+
+ t.test('only parses one level when depth = 1', function (st) {
+ st.deepEqual(qs.parse('a[b][c]=d', { depth: 1 }), { a: { b: { '[c]': 'd' } } });
+ st.deepEqual(qs.parse('a[b][c][d]=e', { depth: 1 }), { a: { b: { '[c][d]': 'e' } } });
+ st.end();
+ });
+
+ t.test('uses original key when depth = 0', function (st) {
+ st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: 0 }), { 'a[0]': 'b', 'a[1]': 'c' });
+ st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: 0 }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
+ st.deepEqual(qs.parse('a.b=c', { depth: 0, allowDots: true }), { 'a[b]': 'c' }, 'normalizes dots before applying depth-0 behavior');
+ st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {}, 'respects prototype guard at depth 0');
+ st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' }, 'allows prototypes at depth 0 when enabled');
+ st.end();
+ });
+
+ t.test('ignores prototype keys when depth = 0 and allowPrototypes is false', function (st) {
+ st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {});
+ st.deepEqual(qs.parse('hasOwnProperty=bar', { depth: 0 }), {});
+ st.deepEqual(qs.parse('toString=foo&a=b', { depth: 0 }), { a: 'b' });
+ st.end();
+ });
+
+ t.test('allows prototype keys when depth = 0 and allowPrototypes is true', function (st) {
+ st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' });
+ st.end();
+ });
+
+ t.test('uses original key when depth = false', function (st) {
+ st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
+ st.deepEqual(qs.parse('a[0][0]=b&a[0][1]=c&a[1]=d&e=2', { depth: false }), { 'a[0][0]': 'b', 'a[0][1]': 'c', 'a[1]': 'd', e: '2' });
+ st.end();
+ });
+
+ t.deepEqual(qs.parse('a=b&a=c'), { a: ['b', 'c'] }, 'parses a simple array');
+
+ t.test('parses an explicit array', function (st) {
+ st.deepEqual(qs.parse('a[]=b'), { a: ['b'] });
+ st.deepEqual(qs.parse('a[]=b&a[]=c'), { a: ['b', 'c'] });
+ st.deepEqual(qs.parse('a[]=b&a[]=c&a[]=d'), { a: ['b', 'c', 'd'] });
+ st.end();
+ });
+
+ t.test('parses a mix of simple and explicit arrays', function (st) {
+ st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
+ st.deepEqual(qs.parse('a[]=b&a=c'), { a: ['b', 'c'] });
+ st.deepEqual(qs.parse('a[0]=b&a=c'), { a: ['b', 'c'] });
+ 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: { 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: { 0: 'b', 1: 'c' } });
+ st.deepEqual(qs.parse('a=b&a[]=c'), { a: ['b', 'c'] });
+
+ st.end();
+ });
+
+ t.test('parses a nested array', function (st) {
+ st.deepEqual(qs.parse('a[b][]=c&a[b][]=d'), { a: { b: ['c', 'd'] } });
+ st.deepEqual(qs.parse('a[>=]=25'), { a: { '>=': '25' } });
+ st.end();
+ });
+
+ t.test('parses keys with literal [] inside a bracket group (#493)', function (st) {
+ // A bracket pair inside a bracket group should be treated literally as part of the key
+ st.deepEqual(
+ qs.parse('search[withbracket[]]=foobar'),
+ { search: { 'withbracket[]': 'foobar' } },
+ 'treats inner [] literally when inside a bracket group'
+ );
+
+ // Single-level variant
+ st.deepEqual(
+ qs.parse('a[b[]]=c'),
+ { a: { 'b[]': 'c' } },
+ 'keeps "b[]" as a literal key'
+ );
+
+ // Nested with an array push on the outer level
+ st.deepEqual(
+ qs.parse('list[][x[]]=y'),
+ { list: [{ 'x[]': 'y' }] },
+ 'preserves inner [] while still treating outer [] as array push'
+ );
+
+ // Multiple nested bracket pairs: inner [] remains literal as part of the key
+ st.deepEqual(
+ qs.parse('a[b[c[]]]=d'),
+ { a: { 'b[c[]]': 'd' } },
+ 'treats "b[c[]]" as a literal key inside the bracket group'
+ );
+
+ // Depth limits with literal brackets: preserve inner [] while limiting bracket-group parsing
+ st.deepEqual(
+ qs.parse('a[b[c[]]][d]=e', { depth: 1 }),
+ { a: { 'b[c[]]': { '[d]': 'e' } } },
+ 'respects depth: 1 and preserves literal inner [] in the parsed key'
+ );
+
+ // Unterminated inner bracket group is wrapped as a literal remainder segment
+ st.deepEqual(
+ qs.parse('a[[]b=c'),
+ { a: { '[[]b': 'c' } },
+ 'handles unterminated inner bracket groups without throwing'
+ );
+
+ st.end();
+ });
+
+ t.test('currently parses unbalanced bracket keys after a parent leniently to literal segments (issue #558)', function (st) {
+ characterizeParse(st, 'a[bc=v', undefined, { a: { '[bc': 'v' } }, 'unclosed group after a parent');
+ characterizeParse(st, 'a[=v', undefined, { a: { '[': 'v' } }, 'bare unclosed bracket after a parent');
+ characterizeParse(st, 'a[b][c=v', undefined, { a: { b: { '[c': 'v' } } }, 'unclosed group after a valid one');
+ characterizeParse(st, 'a[b]c[d=v', undefined, { a: { b: { '[d': 'v' } } }, 'unclosed group after text following a valid one');
+ characterizeParse(st, 'filters[customtags:Env: Prod=v', undefined, { filters: { '[customtags:Env: Prod': 'v' } }, 'the issue #558 reproduction');
+ characterizeParse(st, '][a=v', undefined, { ']': { '[a': 'v' } }, 'stray close bracket before an unclosed group');
+ characterizeParse(st, 'a][b=v', undefined, { 'a]': { '[b': 'v' } }, 'stray close bracket inside the parent');
+ st.end();
+ });
+
+ t.test('currently parses unbalanced bracket keys containing inner brackets leniently (issue #558)', function (st) {
+ characterizeParse(st, 'a[b[c=v', undefined, { a: { '[b[c': 'v' } }, 'unclosed group containing an inner bracket');
+ characterizeParse(st, 'a[b[c]=v', undefined, { a: { '[b[c]': 'v' } }, 'unbalanced group with an inner bracket and one close');
+ characterizeParse(st, 'a[b][c[d=v', undefined, { a: { b: { '[c[d': 'v' } } }, 'unclosed inner-bracket group after a valid one');
+ st.end();
+ });
+
+ t.test('currently parses bracket-prefixed unbalanced keys leniently (issue #558)', function (st) {
+ characterizeParse(st, '[abc=v', undefined, { '[abc': 'v' }, 'key starting with an unclosed bracket');
+ characterizeParse(st, '[[]b=v', undefined, { '[[]b': 'v' }, 'key starting with an unbalanced bracket group');
+ st.end();
+ });
+
+ t.test('lenient unbalanced-bracket handling currently depends on the depth option (issue #558)', function (st) {
+ characterizeParse(st, 'a[b]c[d]e[f=v', { depth: 5 }, { a: { b: { d: { '[f': 'v' } } } }, 'consumes groups up to the depth budget then keeps the unclosed remainder literal');
+ characterizeParse(st, 'a[b]c[d]e[f=v', { depth: 1 }, { a: { b: { '[d]e[f': 'v' } } }, 'a lower depth keeps more of the unclosed remainder literal');
+ characterizeParse(st, 'a[bc=v', { depth: 0 }, { 'a[bc': 'v' }, 'depth 0 keeps the entire key literal');
+ st.end();
+ });
+
+ t.test('currently parses an allowDots key with a trailing unclosed bracket leniently (issue #558)', function (st) {
+ characterizeParse(st, 'a.b[c=v', { allowDots: true }, { a: { b: { '[c': 'v' } } }, 'allowDots expands the dot then keeps the unclosed bracket literal');
+ st.end();
+ });
+
+ t.test('valid and stray-close bracket keys are unaffected by unbalanced-bracket handling', function (st) {
+ characterizeParse(st, 'a]b=v', undefined, { 'a]b': 'v' }, 'stray close bracket with no open bracket stays a flat key');
+ characterizeParse(st, 'a[b]extra=v', undefined, { a: { b: 'v' } }, 'text after a balanced group is ignored');
+ st.end();
+ });
+
+ t.test('allows to specify array indices', function (st) {
+ st.deepEqual(qs.parse('a[1]=c&a[0]=b&a[2]=d'), { a: ['b', 'c', 'd'] });
+ st.deepEqual(qs.parse('a[1]=c&a[0]=b'), { a: ['b', 'c'] });
+ st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 20 }), { a: ['c'] });
+ st.deepEqual(qs.parse('a[1]=c', { arrayLimit: 0 }), { a: { 1: 'c' } });
+ st.deepEqual(qs.parse('a[1]=c'), { a: ['c'] });
+ st.end();
+ });
+
+ t.test('limits specific array indices to arrayLimit', function (st) {
+ st.deepEqual(qs.parse('a[19]=a', { arrayLimit: 20 }), { a: ['a'] });
+ st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: { 20: 'a' } });
+
+ st.deepEqual(qs.parse('a[19]=a'), { a: ['a'] });
+ st.deepEqual(qs.parse('a[20]=a'), { a: { 20: 'a' } });
+ st.end();
+ });
+
+ t.deepEqual(qs.parse('a[12b]=c'), { a: { '12b': 'c' } }, 'supports keys that begin with a number');
+
+ t.test('supports encoded = signs', function (st) {
+ st.deepEqual(qs.parse('he%3Dllo=th%3Dere'), { 'he=llo': 'th=ere' });
+ st.end();
+ });
+
+ t.test('is ok with url encoded strings', function (st) {
+ st.deepEqual(qs.parse('a[b%20c]=d'), { a: { 'b c': 'd' } });
+ st.deepEqual(qs.parse('a[b]=c%20d'), { a: { b: 'c d' } });
+ st.end();
+ });
+
+ t.test('allows brackets in the value', function (st) {
+ st.deepEqual(qs.parse('pets=["tobi"]'), { pets: '["tobi"]' });
+ st.deepEqual(qs.parse('operators=[">=", "<="]'), { operators: '[">=", "<="]' });
+ st.end();
+ });
+
+ t.test('allows empty values', function (st) {
+ st.deepEqual(qs.parse(''), {});
+ st.deepEqual(qs.parse(null), {});
+ st.deepEqual(qs.parse(undefined), {});
+ st.end();
+ });
+
+ t.test('transforms arrays to objects', function (st) {
+ st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
+ st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
+ st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
+ st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
+ st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
+ 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' }] });
+
+ st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', t: 'u' } });
+ st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
+ st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', x: 'y' } });
+ st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
+ st.end();
+ });
+
+ t.test('transforms arrays to objects (dot notation)', function (st) {
+ st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: 'baz' } });
+ st.deepEqual(qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true }), { foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } });
+ st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
+ st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
+ 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' }] });
+ st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
+ st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
+ st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
+ st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
+ 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' }] });
+ st.end();
+ });
+
+ t.test('correctly prunes undefined values when converting an array to an object', function (st) {
+ st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
+ st.end();
+ });
+
+ t.test('supports malformed uri characters', function (st) {
+ st.deepEqual(qs.parse('{%:%}', { strictNullHandling: true }), { '{%:%}': null });
+ st.deepEqual(qs.parse('{%:%}='), { '{%:%}': '' });
+ st.deepEqual(qs.parse('foo=%:%}'), { foo: '%:%}' });
+ st.end();
+ });
+
+ t.test('doesn\'t produce empty keys', function (st) {
+ st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
+ st.end();
+ });
+
+ t.test('cannot access Object prototype', function (st) {
+ qs.parse('constructor[prototype][bad]=bad');
+ qs.parse('bad[constructor][prototype][bad]=bad');
+ st.equal(typeof Object.prototype.bad, 'undefined');
+ st.end();
+ });
+
+ t.test('parses arrays of objects', function (st) {
+ st.deepEqual(qs.parse('a[][b]=c'), { a: [{ b: 'c' }] });
+ st.deepEqual(qs.parse('a[0][b]=c'), { a: [{ b: 'c' }] });
+ st.end();
+ });
+
+ t.test('allows for empty strings in arrays', function (st) {
+ st.deepEqual(qs.parse('a[]=b&a[]=&a[]=c'), { a: ['b', '', 'c'] });
+
+ st.deepEqual(
+ qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', { strictNullHandling: true, arrayLimit: 20 }),
+ { a: ['b', null, 'c', ''] },
+ 'with arrayLimit 20 + array indices: null then empty string works'
+ );
+ st.deepEqual(
+ qs.parse('a[]=b&a[]&a[]=c&a[]=', { strictNullHandling: true, arrayLimit: 0 }),
+ { a: { 0: 'b', 1: null, 2: 'c', 3: '' } },
+ 'with arrayLimit 0 + array brackets: null then empty string works'
+ );
+
+ st.deepEqual(
+ qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', { strictNullHandling: true, arrayLimit: 20 }),
+ { a: ['b', '', 'c', null] },
+ 'with arrayLimit 20 + array indices: empty string then null works'
+ );
+ st.deepEqual(
+ qs.parse('a[]=b&a[]=&a[]=c&a[]', { strictNullHandling: true, arrayLimit: 0 }),
+ { a: { 0: 'b', 1: '', 2: 'c', 3: null } },
+ 'with arrayLimit 0 + array brackets: empty string then null works'
+ );
+
+ st.deepEqual(
+ qs.parse('a[]=&a[]=b&a[]=c'),
+ { a: ['', 'b', 'c'] },
+ 'array brackets: empty strings work'
+ );
+ st.end();
+ });
+
+ t.test('compacts sparse arrays', function (st) {
+ st.deepEqual(qs.parse('a[10]=1&a[2]=2', { arrayLimit: 20 }), { a: ['2', '1'] });
+ st.deepEqual(qs.parse('a[1][b][2][c]=1', { arrayLimit: 20 }), { a: [{ b: [{ c: '1' }] }] });
+ st.deepEqual(qs.parse('a[1][2][3][c]=1', { arrayLimit: 20 }), { a: [[[{ c: '1' }]]] });
+ st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { arrayLimit: 20 }), { a: [[[{ c: ['1'] }]]] });
+ st.end();
+ });
+
+ t.test('parses sparse arrays', function (st) {
+ /* eslint no-sparse-arrays: 0 */
+ st.deepEqual(qs.parse('a[4]=1&a[1]=2', { allowSparse: true }), { a: [, '2', , , '1'] });
+ st.deepEqual(qs.parse('a[1][b][2][c]=1', { allowSparse: true }), { a: [, { b: [, , { c: '1' }] }] });
+ st.deepEqual(qs.parse('a[1][2][3][c]=1', { allowSparse: true }), { a: [, [, , [, , , { c: '1' }]]] });
+ st.deepEqual(qs.parse('a[1][2][3][c][1]=1', { allowSparse: true }), { a: [, [, , [, , , { c: [, '1'] }]]] });
+ st.end();
+ });
+
+ t.test('parses semi-parsed strings', function (st) {
+ st.deepEqual(qs.parse({ 'a[b]': 'c' }), { a: { b: 'c' } });
+ st.deepEqual(qs.parse({ 'a[b]': 'c', 'a[d]': 'e' }), { a: { b: 'c', d: 'e' } });
+ st.end();
+ });
+
+ t.test('parses buffers correctly', function (st) {
+ var b = SaferBuffer.from('test');
+ st.deepEqual(qs.parse({ a: b }), { a: b });
+ st.end();
+ });
+
+ t.test('parses jquery-param strings', function (st) {
+ // readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8'
+ var encoded = 'filter%5B0%5D%5B%5D=int1&filter%5B0%5D%5B%5D=%3D&filter%5B0%5D%5B%5D=77&filter%5B%5D=and&filter%5B2%5D%5B%5D=int2&filter%5B2%5D%5B%5D=%3D&filter%5B2%5D%5B%5D=8';
+ var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] };
+ st.deepEqual(qs.parse(encoded), expected);
+ st.end();
+ });
+
+ t.test('continues parsing when no parent is found', function (st) {
+ st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
+ st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
+ st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
+ st.end();
+ });
+
+ t.test('does not error when parsing a very long array', function (st) {
+ var str = 'a[]=a';
+ while (Buffer.byteLength(str) < 128 * 1024) {
+ str = str + '&' + str;
+ }
+
+ st.doesNotThrow(function () {
+ qs.parse(str);
+ });
+
+ st.end();
+ });
+
+ 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' }] });
+
+ st.end();
+ });
+
+ t.test('parses a string with an alternative string delimiter', function (st) {
+ st.deepEqual(qs.parse('a=b;c=d', { delimiter: ';' }), { a: 'b', c: 'd' });
+ st.end();
+ });
+
+ t.test('parses a string with an alternative RegExp delimiter', function (st) {
+ st.deepEqual(qs.parse('a=b; c=d', { delimiter: /[;,] */ }), { a: 'b', c: 'd' });
+ st.end();
+ });
+
+ t.test('does not use non-splittable objects as delimiters', function (st) {
+ st.deepEqual(qs.parse('a=b&c=d', { delimiter: true }), { a: 'b', c: 'd' });
+ st.end();
+ });
+
+ t.test('allows overriding parameter limit', function (st) {
+ st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: 1 }), { a: 'b' });
+ st.end();
+ });
+
+ t.test('allows setting the parameter limit to Infinity', function (st) {
+ st.deepEqual(qs.parse('a=b&c=d', { parameterLimit: Infinity }), { a: 'b', c: 'd' });
+ st.end();
+ });
+
+ 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: { 0: '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();
+ });
+
+ t.test('allows disabling array parsing', function (st) {
+ var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
+ st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
+ st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
+
+ var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
+ st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
+ st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
+
+ st.end();
+ });
+
+ t.test('allows for query string prefix', function (st) {
+ st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
+ st.deepEqual(qs.parse('foo=bar', { ignoreQueryPrefix: true }), { foo: 'bar' });
+ st.deepEqual(qs.parse('?foo=bar', { ignoreQueryPrefix: false }), { '?foo': 'bar' });
+
+ st.end();
+ });
+
+ t.test('parses an object', function (st) {
+ var input = {
+ 'user[name]': { 'pop[bob]': 3 },
+ 'user[email]': null
+ };
+
+ var expected = {
+ user: {
+ name: { 'pop[bob]': 3 },
+ email: null
+ }
+ };
+
+ var result = qs.parse(input);
+
+ st.deepEqual(result, expected);
+ st.end();
+ });
+
+ t.test('parses string with comma as array divider', function (st) {
+ st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] });
+ st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } });
+ st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
+ st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
+ st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });
+
+ // test cases inversed from from stringify tests
+ st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });
+ st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });
+ st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });
+
+ st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });
+ st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });
+ st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });
+
+ st.end();
+ });
+
+ t.test('parses values with comma as array divider', function (st) {
+ st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: false }), { foo: 'bar,tee' });
+ st.deepEqual(qs.parse({ foo: 'bar,tee' }, { comma: true }), { foo: ['bar', 'tee'] });
+ st.end();
+ });
+
+ t.test('use number decoder, parses string that has one number with comma option enabled', function (st) {
+ var decoder = function (str, defaultDecoder, charset, type) {
+ if (!isNaN(Number(str))) {
+ return parseFloat(str);
+ }
+ return defaultDecoder(str, defaultDecoder, charset, type);
+ };
+
+ st.deepEqual(qs.parse('foo=1', { comma: true, decoder: decoder }), { foo: 1 });
+ st.deepEqual(qs.parse('foo=0', { comma: true, decoder: decoder }), { foo: 0 });
+
+ st.end();
+ });
+
+ t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
+ st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
+ st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
+ st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
+ st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
+
+ 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'] });
+ st.deepEqual(qs.parse('foo=a%2C%20b,c%2C%20d', { comma: true }), { foo: ['a, b', 'c, d'] });
+
+ st.end();
+ });
+
+ t.test('parses an object in dot notation', function (st) {
+ var input = {
+ 'user.name': { 'pop[bob]': 3 },
+ 'user.email.': null
+ };
+
+ var expected = {
+ user: {
+ name: { 'pop[bob]': 3 },
+ email: null
+ }
+ };
+
+ var result = qs.parse(input, { allowDots: true });
+
+ st.deepEqual(result, expected);
+ st.end();
+ });
+
+ t.test('parses an object and not child values', function (st) {
+ var input = {
+ 'user[name]': { 'pop[bob]': { test: 3 } },
+ 'user[email]': null
+ };
+
+ var expected = {
+ user: {
+ name: { 'pop[bob]': { test: 3 } },
+ email: null
+ }
+ };
+
+ var result = qs.parse(input);
+
+ st.deepEqual(result, expected);
+ st.end();
+ });
+
+ t.test('does not blow up when Buffer global is missing', function (st) {
+ var restore = mockProperty(global, 'Buffer', { 'delete': true });
+
+ var result = qs.parse('a=b&c=d');
+
+ restore();
+
+ st.deepEqual(result, { a: 'b', c: 'd' });
+ st.end();
+ });
+
+ t.test('does not crash when parsing circular references', function (st) {
+ var a = {};
+ a.b = a;
+
+ var parsed;
+
+ st.doesNotThrow(function () {
+ parsed = qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a });
+ });
+
+ st.equal('foo' in parsed, true, 'parsed has "foo" property');
+ st.equal('bar' in parsed.foo, true);
+ st.equal('baz' in parsed.foo, true);
+ st.equal(parsed.foo.bar, 'baz');
+ st.deepEqual(parsed.foo.baz, a);
+ st.end();
+ });
+
+ t.test('does not crash on multi-step circular references', function (st) {
+ var a = {};
+ a.b = { c: { d: a } };
+
+ var parsed;
+
+ st.doesNotThrow(function () {
+ parsed = qs.parse({ foo: a });
+ });
+
+ st.equal('foo' in parsed, true, 'parsed has "foo" property');
+ st.equal(parsed.foo.b.c.d, parsed.foo, 'the multi-step cycle is preserved');
+ st.end();
+ });
+
+ t.test('does not crash when parsing deep objects', function (st) {
+ var parsed;
+ var str = 'foo';
+
+ for (var i = 0; i < 5000; i++) {
+ str += '[p]';
+ }
+
+ str += '=bar';
+
+ st.doesNotThrow(function () {
+ parsed = qs.parse(str, { depth: 5000 });
+ });
+
+ st.equal('foo' in parsed, true, 'parsed has "foo" property');
+
+ var depth = 0;
+ var ref = parsed.foo;
+ while ((ref = ref.p)) {
+ depth += 1;
+ }
+
+ st.equal(depth, 5000, 'parsed is 5000 properties deep');
+
+ st.end();
+ });
+
+ 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 });
+ st.equal('a' in result, true, 'result has "a" property');
+ st.deepEqual(result.a, a);
+ st.end();
+ });
+
+ t.test('parses dates correctly', function (st) {
+ var now = new Date();
+ st.deepEqual(qs.parse({ a: now }), { a: now });
+ st.end();
+ });
+
+ t.test('parses regular expressions correctly', function (st) {
+ var re = /^test$/;
+ st.deepEqual(qs.parse({ a: re }), { a: re });
+ st.end();
+ });
+
+ t.test('does not allow overwriting prototype properties', function (st) {
+ st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: false }), {});
+ st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: false }), {});
+
+ st.deepEqual(
+ qs.parse('toString', { allowPrototypes: false }),
+ {},
+ 'bare "toString" results in {}'
+ );
+
+ st.end();
+ });
+
+ t.test('can allow overwriting prototype properties', function (st) {
+ st.deepEqual(qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true }), { a: { hasOwnProperty: 'b' } });
+ st.deepEqual(qs.parse('hasOwnProperty=b', { allowPrototypes: true }), { hasOwnProperty: 'b' });
+
+ st.deepEqual(
+ qs.parse('toString', { allowPrototypes: true }),
+ { toString: '' },
+ 'bare "toString" results in { toString: "" }'
+ );
+
+ 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' });
+ st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
+ st.end();
+ });
+
+ t.test('params starting with a starting bracket', function (st) {
+ st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
+ st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
+ st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
+ st.end();
+ });
+
+ t.test('add keys to objects', function (st) {
+ st.deepEqual(
+ qs.parse('a[b]=c&a=d', { strictMerge: false }),
+ { a: { b: 'c', d: true } },
+ 'can add keys to objects'
+ );
+
+ st.deepEqual(
+ qs.parse('a[b]=c&a=toString', { strictMerge: false }),
+ { a: { b: 'c' } },
+ 'can not overwrite prototype'
+ );
+
+ st.deepEqual(
+ qs.parse('a[b]=c&a=toString', { strictMerge: false, allowPrototypes: true }),
+ { a: { b: 'c', toString: true } },
+ 'can overwrite prototype with allowPrototypes true'
+ );
+
+ st.deepEqual(
+ qs.parse('a[b]=c&a=toString', { strictMerge: false, plainObjects: true }),
+ { __proto__: null, a: { __proto__: null, b: 'c', toString: true } },
+ 'can overwrite prototype with plainObjects true'
+ );
+
+ st.end();
+ });
+
+ t.test('strictMerge wraps object and primitive into an array', function (st) {
+ st.deepEqual(
+ qs.parse('a[b]=c&a=d'),
+ { a: [{ b: 'c' }, 'd'] },
+ 'object then primitive produces array'
+ );
+
+ st.deepEqual(
+ qs.parse('a=d&a[b]=c'),
+ { a: ['d', { b: 'c' }] },
+ 'primitive then object produces array'
+ );
+
+ st.deepEqual(
+ qs.parse('a[b]=c&a=toString'),
+ { a: [{ b: 'c' }, 'toString'] },
+ 'prototype-colliding value is preserved in array'
+ );
+
+ st.deepEqual(
+ qs.parse('a[b]=c&a=toString', { plainObjects: true }),
+ { __proto__: null, a: [{ __proto__: null, b: 'c' }, 'toString'] },
+ 'plainObjects preserved in array wrapping'
+ );
+
+ st.end();
+ });
+
+ t.test('dunder proto is ignored', function (st) {
+ var payload = 'categories[__proto__]=login&categories[__proto__]&categories[length]=42';
+ var result = qs.parse(payload, { allowPrototypes: true });
+
+ st.deepEqual(
+ result,
+ {
+ categories: {
+ length: '42'
+ }
+ },
+ 'silent [[Prototype]] payload'
+ );
+
+ var plainResult = qs.parse(payload, { allowPrototypes: true, plainObjects: true });
+
+ st.deepEqual(
+ plainResult,
+ {
+ __proto__: null,
+ categories: {
+ __proto__: null,
+ length: '42'
+ }
+ },
+ 'silent [[Prototype]] payload: plain objects'
+ );
+
+ var query = qs.parse('categories[__proto__]=cats&categories[__proto__]=dogs&categories[some][json]=toInject', { allowPrototypes: true });
+
+ st.notOk(Array.isArray(query.categories), 'is not an array');
+ st.notOk(query.categories instanceof Array, 'is not instanceof an array');
+ st.deepEqual(query.categories, { some: { json: 'toInject' } });
+ st.equal(JSON.stringify(query.categories), '{"some":{"json":"toInject"}}', 'stringifies as a non-array');
+
+ st.deepEqual(
+ qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true }),
+ {
+ foo: {
+ bar: 'stuffs'
+ }
+ },
+ 'hidden values'
+ );
+
+ st.deepEqual(
+ qs.parse('foo[__proto__][hidden]=value&foo[bar]=stuffs', { allowPrototypes: true, plainObjects: true }),
+ {
+ __proto__: null,
+ foo: {
+ __proto__: null,
+ bar: 'stuffs'
+ }
+ },
+ 'hidden values: plain objects'
+ );
+
+ st.end();
+ });
+
+ t.test('object-valued input with own `__proto__` does not mutate sub-object [[Prototype]]', function (st) {
+ // JSON.parse creates own data `__proto__` properties (via CreateDataProperty),
+ // which would trigger the Object.prototype.__proto__ accessor if merged via `acc[key] = value`.
+ var out = qs.parse({
+ 'user[name]': 'alice',
+ user: JSON.parse('{"__proto__":{"isAdmin":true}}')
+ }, { allowPrototypes: false });
+
+ st.equal(out.user.name, 'alice', 'name from bracket key is preserved');
+ st.equal(out.user.isAdmin, undefined, 'attacker-controlled inherited property is not exposed');
+ st.equal(Object.getPrototypeOf(out.user), Object.prototype, 'sub-object [[Prototype]] is unchanged');
+ st.equal(Object.prototype.isAdmin, undefined, 'Object.prototype is not polluted');
+
+ st.end();
+ });
+
+ 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 }), { __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();
+ });
+
+ t.test('can parse with custom encoding', function (st) {
+ st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
+ decoder: function (str) {
+ var reg = /%([0-9A-F]{2})/ig;
+ var result = [];
+ var parts = reg.exec(str);
+ while (parts) {
+ result.push(parseInt(parts[1], 16));
+ parts = reg.exec(str);
+ }
+ return String(iconv.decode(SaferBuffer.from(result), 'shift_jis'));
+ }
+ }), { 県: '大阪府' });
+ st.end();
+ });
+
+ t.test('receives the default decoder as a second argument', function (st) {
+ st.plan(1);
+ qs.parse('a', {
+ decoder: function (str, defaultDecoder) {
+ st.equal(defaultDecoder, utils.decode);
+ }
+ });
+ st.end();
+ });
+
+ t.test('throws error with wrong decoder', function (st) {
+ st['throws'](function () {
+ qs.parse({}, { decoder: 'string' });
+ }, new TypeError('Decoder has to be a function.'));
+ st.end();
+ });
+
+ t.test('does not mutate the options argument', function (st) {
+ var options = {};
+ qs.parse('a[b]=true', options);
+ st.deepEqual(options, {});
+ st.end();
+ });
+
+ t.test('throws if an invalid charset is specified', function (st) {
+ st['throws'](function () {
+ qs.parse('a=b', { charset: 'foobar' });
+ }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
+ st.end();
+ });
+
+ t.test('parses an iso-8859-1 string if asked to', function (st) {
+ st.deepEqual(qs.parse('%A2=%BD', { charset: 'iso-8859-1' }), { '¢': '½' });
+ st.end();
+ });
+
+ var urlEncodedCheckmarkInUtf8 = '%E2%9C%93';
+ var urlEncodedOSlashInUtf8 = '%C3%B8';
+ var urlEncodedNumCheckmark = '%26%2310003%3B';
+ var urlEncodedNumSmiley = '%26%239786%3B';
+
+ t.test('prefers an utf-8 charset specified by the utf8 sentinel to a default charset of iso-8859-1', function (st) {
+ st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'iso-8859-1' }), { ø: 'ø' });
+ st.end();
+ });
+
+ t.test('prefers an iso-8859-1 charset specified by the utf8 sentinel to a default charset of utf-8', function (st) {
+ st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true, charset: 'utf-8' }), { 'ø': 'ø' });
+ st.end();
+ });
+
+ t.test('does not require the utf8 sentinel to be defined before the parameters whose decoding it affects', function (st) {
+ st.deepEqual(qs.parse('a=' + urlEncodedOSlashInUtf8 + '&utf8=' + urlEncodedNumCheckmark, { charsetSentinel: true, charset: 'utf-8' }), { a: 'ø' });
+ st.end();
+ });
+
+ 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();
+ });
+
+ t.test('uses the utf8 sentinel to switch to utf-8 when no default charset is given', function (st) {
+ st.deepEqual(qs.parse('utf8=' + urlEncodedCheckmarkInUtf8 + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { ø: 'ø' });
+ st.end();
+ });
+
+ t.test('uses the utf8 sentinel to switch to iso-8859-1 when no default charset is given', function (st) {
+ st.deepEqual(qs.parse('utf8=' + urlEncodedNumCheckmark + '&' + urlEncodedOSlashInUtf8 + '=' + urlEncodedOSlashInUtf8, { charsetSentinel: true }), { 'ø': 'ø' });
+ st.end();
+ });
+
+ t.test('interprets numeric entities in iso-8859-1 when `interpretNumericEntities`', function (st) {
+ st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'iso-8859-1', interpretNumericEntities: true }), { foo: '☺' });
+ st.end();
+ });
+
+ t.test('handles a custom decoder returning `null`, in the `iso-8859-1` charset, when `interpretNumericEntities`', function (st) {
+ st.deepEqual(qs.parse('foo=&bar=' + urlEncodedNumSmiley, {
+ charset: 'iso-8859-1',
+ decoder: function (str, defaultDecoder, charset) {
+ return str ? defaultDecoder(str, defaultDecoder, charset) : null;
+ },
+ interpretNumericEntities: true
+ }), { foo: null, bar: '☺' });
+ 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: '&#9786;' });
+ st.end();
+ });
+
+ t.test('does not interpret numeric entities when the charset is utf-8, even when `interpretNumericEntities`', function (st) {
+ st.deepEqual(qs.parse('foo=' + urlEncodedNumSmiley, { charset: 'utf-8', interpretNumericEntities: true }), { foo: '&#9786;' });
+ 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();
+ });
+
+ t.test('allows for decoding keys and values differently', function (st) {
+ var decoder = function (str, defaultDecoder, charset, type) {
+ if (type === 'key') {
+ return defaultDecoder(str, defaultDecoder, charset, type).toLowerCase();
+ }
+ if (type === 'value') {
+ return defaultDecoder(str, defaultDecoder, charset, type).toUpperCase();
+ }
+ throw 'this should never happen! type: ' + type;
+ };
+
+ st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
+
+ var noopDecoder = function () { return 'x'; };
+ noopDecoder();
+ st['throws'](
+ function () { decoder('x', noopDecoder, 'utf-8', 'unknown'); },
+ 'this should never happen! type: unknown',
+ 'decoder throws for unexpected type'
+ );
+
+ 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['throws'](
+ function () {
+ qs.parse('a=1&b=2', { parameterLimit: 1, throwOnLimitExceeded: true });
+ },
+ new RangeError('Parameter limit exceeded. Only 1 parameter allowed.'),
+ 'throws error with singular "parameter" when parameterLimit is 1'
+ );
+ 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.test('allows unlimited parameters when parameterLimit is Infinity and throwOnLimitExceeded is true', function (sst) {
+ var result = qs.parse('a=1&b=2&c=3&d=4&e=5&f=6', { parameterLimit: Infinity, throwOnLimitExceeded: true });
+ sst.deepEqual(result, { a: '1', b: '2', c: '3', d: '4', e: '5', f: '6' }, 'parses all parameters without truncation or throwing');
+ 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) {
+ // 4 elements exceeds limit of 3
+ 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('does not throw when at limit', function (sst) {
+ // 3 elements = limit of 3, should not throw
+ var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3, throwOnLimitExceeded: true });
+ sst.ok(Array.isArray(result.a), 'result is an array');
+ sst.deepEqual(result.a, ['1', '2', '3'], 'all values present');
+ 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.test('throws error when indexed notation exceeds arrayLimit with throwOnLimitExceeded', function (sst) {
+ sst['throws'](
+ function () {
+ qs.parse('a[1001]=b', { arrayLimit: 1000, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 1000 elements allowed in an array.'),
+ 'throws error for a single index exceeding arrayLimit'
+ );
+
+ sst['throws'](
+ function () {
+ qs.parse('a[0]=1&a[1]=2&a[2]=3&a[10]=4', { arrayLimit: 6, throwOnLimitExceeded: true, allowSparse: true });
+ },
+ new RangeError('Array limit exceeded. Only 6 elements allowed in an array.'),
+ 'throws error when a sparse index exceeds arrayLimit'
+ );
+
+ sst['throws'](
+ function () {
+ qs.parse('a[2]=b', { arrayLimit: 1, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws error with singular "element" when arrayLimit is 1'
+ );
+
+ sst.end();
+ });
+
+ st.test('does not throw for indexed notation within arrayLimit with throwOnLimitExceeded', function (sst) {
+ var result = qs.parse('a[4]=b', { arrayLimit: 5, throwOnLimitExceeded: true, allowSparse: true });
+ sst.ok(Array.isArray(result.a), 'result is an array');
+ sst.equal(result.a.length, 5, 'array has correct length');
+ sst.equal(result.a[4], 'b', 'value at index 4 is correct');
+ sst.end();
+ });
+
+ st.test('silently converts to object for indexed notation exceeding arrayLimit without throwOnLimitExceeded', function (sst) {
+ var result = qs.parse('a[1001]=b', { arrayLimit: 1000 });
+ sst.deepEqual(result, { a: { 1001: 'b' } }, 'converts to object without throwing');
+ sst.end();
+ });
+
+ st.test('throws when duplicate bracket keys exceed arrayLimit with throwOnLimitExceeded', function (sst) {
+ sst['throws'](
+ function () {
+ qs.parse('a[]=1&a[]=2&a[]=3&a[]=4&a[]=5&a[]=6', { arrayLimit: 5, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
+ 'throws error when duplicate bracket notation exceeds array limit'
+ );
+ sst.end();
+ });
+
+ st.test('throws when cumulative comma + duplicate-key combine exceeds arrayLimit', function (sst) {
+ sst['throws'](
+ function () {
+ qs.parse('a=1,2,3&a=4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
+ 'throws when comma groups within the limit cumulatively exceed it across duplicate keys'
+ );
+
+ sst['throws'](
+ function () {
+ qs.parse('a=v,v,v,v,v&a=v,v,v,v,v&a=v,v,v,v,v', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
+ 'throws on a subsequent part once the cumulative array is already over the limit'
+ );
+ sst.end();
+ });
+
+ st.test('throws when plain duplicate keys combine past arrayLimit at the boundary', function (sst) {
+ sst['throws'](
+ function () { qs.parse('a=x&a=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'duplicate scalar keys'
+ );
+
+ sst['throws'](
+ function () { qs.parse('a[]=x&a[]=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'duplicate bracket keys'
+ );
+ sst.end();
+ });
+
+ st.test('throws when mixed index and key notation merge past arrayLimit', function (sst) {
+ sst['throws'](
+ function () { qs.parse('a=x&a[0]=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'scalar then index that overflows on merge'
+ );
+
+ sst['throws'](
+ function () { qs.parse('a[0]=1&a[1]=2&a=3', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'indexed array then scalar that overflows on merge'
+ );
+ sst.end();
+ });
+
+ st.test('enforces arrayLimit on merge at the boundary, consistently with combine', function (sst) {
+ sst['throws'](
+ function () { qs.parse('a[0]=x&a=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'a trailing scalar merged into an at-limit array throws'
+ );
+ sst.deepEqual(
+ qs.parse('a[0]=x&a=y', { arrayLimit: 1 }),
+ { a: { 0: 'x', 1: 'y' } },
+ 'and converts to an overflow object without throwOnLimitExceeded'
+ );
+
+ sst['throws'](
+ function () { qs.parse('a[0]=x&a[]=y', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'mixed index and bracket notation merged past the limit throws'
+ );
+ sst.deepEqual(
+ qs.parse('a[0]=x&a[]=y', { arrayLimit: 1 }),
+ { a: { 0: 'x', 1: 'y' } },
+ 'mixed index and bracket notation converts like duplicate-bracket combine'
+ );
+
+ sst.end();
+ });
+
+ st.test('does not throw when cumulative comma combine stays within arrayLimit', function (sst) {
+ var result = qs.parse('a=1,2,3&a=4', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ sst.deepEqual(result, { a: ['1', '2', '3', '4'] }, 'combined array within limit is preserved');
+ sst.end();
+ });
+
+ st.test('silently combines to an overflow object when throwOnLimitExceeded is not set', function (sst) {
+ var result = qs.parse('a=1,2,3&a=4,5,6', { comma: true, arrayLimit: 5 });
+ sst.deepEqual(result, { a: { 0: '1', 1: '2', 2: '3', 3: '4', 4: '5', 5: '6' } }, 'converts to object without throwing');
+ sst.end();
+ });
+
+ st.test('does not throw for comma groups nested under bracket notation, counting each group as one element', function (sst) {
+ var result = qs.parse('a[]=1,2,3&a[]=4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ sst.deepEqual(result, { a: [['1', '2', '3'], ['4', '5', '6']] }, 'nested comma groups count as one element each');
+ sst.end();
+ });
+
+ st.test('throws before splitting when a single comma value exceeds arrayLimit', function (sst) {
+ sst['throws'](
+ function () {
+ qs.parse('a=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
+ 'a flat comma value over the limit throws'
+ );
+
+ sst['throws'](
+ function () {
+ qs.parse('a=1,2', { comma: true, arrayLimit: 1, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'singular message at arrayLimit 1'
+ );
+
+ sst['throws'](
+ function () {
+ qs.parse('a[b]=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
+ 'a non-bracket nested key comma value over the limit throws'
+ );
+ sst.end();
+ });
+
+ st.test('does not throw for a single comma value within arrayLimit', function (sst) {
+ sst.deepEqual(
+ qs.parse('a=1,2,3', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true }),
+ { a: ['1', '2', '3'] },
+ 'within the limit'
+ );
+ sst.deepEqual(
+ qs.parse('a=1,2,3,4,5', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true }),
+ { a: ['1', '2', '3', '4', '5'] },
+ 'exactly at the limit'
+ );
+ sst.end();
+ });
+
+ st.test('does not throw for a bracketed comma group within arrayLimit', function (sst) {
+ var result = qs.parse('a[]=1,2,3,4,5,6', { comma: true, arrayLimit: 5, throwOnLimitExceeded: true });
+ sst.deepEqual(result, { a: [['1', '2', '3', '4', '5', '6']] }, 'a bracketed comma group is a single element');
+ sst.end();
+ });
+
+ st.test('throws for a bracketed comma group when arrayLimit is 0', function (sst) {
+ sst['throws'](
+ function () {
+ qs.parse('a[]=1,2,3', { comma: true, arrayLimit: 0, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
+ 'a single bracketed element still exceeds arrayLimit 0'
+ );
+ 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.test('bracket notation always combines regardless of duplicates', function (st) {
+ st.deepEqual(
+ qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'last' }),
+ { a: '2', b: ['1', '2'] },
+ 'duplicates last: unbracketed takes last, bracketed combines'
+ );
+
+ st.deepEqual(
+ qs.parse('b[]=1&b[]=2', { duplicates: 'last' }),
+ { b: ['1', '2'] },
+ 'duplicates last: bracketed always combines'
+ );
+
+ st.deepEqual(
+ qs.parse('b[]=1&b[]=2', { duplicates: 'first' }),
+ { b: ['1', '2'] },
+ 'duplicates first: bracketed always combines'
+ );
+
+ st.deepEqual(
+ qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'first' }),
+ { a: '1', b: ['1', '2'] },
+ 'duplicates first: unbracketed takes first, bracketed combines'
+ );
+
+ st.end();
+ });
+
+ 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) {
+ // arrayLimit is the max number of elements allowed in an array
+ t.test('exactly at the limit stays as array', function (st) {
+ // 3 elements = limit of 3
+ var result = qs.parse('a[]=1&a[]=2&a[]=3', { arrayLimit: 3 });
+ st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
+ st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
+ st.end();
+ });
+
+ t.test('one over the limit converts to object', function (st) {
+ // 4 elements exceeds limit of 3
+ 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 one value', function (st) {
+ // 1 element = limit of 1
+ var result = qs.parse('a[]=1', { arrayLimit: 1 });
+ st.ok(Array.isArray(result.a), 'result is an array when count equals limit');
+ st.deepEqual(result.a, ['1'], 'value preserved as array');
+ st.end();
+ });
+
+ t.test('arrayLimit 1 with two values converts to object', function (st) {
+ // 2 elements exceeds limit of 1
+ 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' }, 'all values preserved as object');
+ st.end();
+ });
+
+ t.end();
+});
+
+test('comma + arrayLimit', function (t) {
+ t.test('comma-separated values within arrayLimit stay as array', function (st) {
+ var result = qs.parse('a=1,2,3', { comma: true, arrayLimit: 5 });
+ st.ok(Array.isArray(result.a), 'result is an array');
+ st.deepEqual(result.a, ['1', '2', '3'], 'all values present');
+ st.end();
+ });
+
+ t.test('comma-separated values exceeding arrayLimit convert to object', function (st) {
+ var result = qs.parse('a=1,2,3,4', { comma: true, 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('comma-separated values exceeding arrayLimit with throwOnLimitExceeded throws', function (st) {
+ st['throws'](
+ function () {
+ qs.parse('a=1,2,3,4', { comma: true, arrayLimit: 3, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
+ 'throws error when comma-split exceeds array limit'
+ );
+
+ st['throws'](
+ function () {
+ qs.parse('a=1,2,3', { comma: true, arrayLimit: 1, throwOnLimitExceeded: true });
+ },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws error with singular "element" when arrayLimit is 1'
+ );
+ st.end();
+ });
+
+ t.test('comma-separated values at exactly arrayLimit stay as array', function (st) {
+ var result = qs.parse('a=1,2,3', { comma: true, 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.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) {
+ // 3 elements (indices 0-2), max index 2 > limit 1
+ st.deepEqual(
+ qs.parse('a=b&a=c&a=d', { arrayLimit: 1 }),
+ { a: { 0: 'b', 1: 'c', 2: 'd' } },
+ 'duplicate plain keys convert to object when exceeding limit'
+ );
+ st.end();
+ });
+
+ t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) {
+ var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } };
+
+ st.deepEqual(
+ qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }),
+ expected,
+ 'arrayLimit -1'
+ );
+
+ st.deepEqual(
+ qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }),
+ expected,
+ 'arrayLimit 0'
+ );
+
+ st.deepEqual(
+ qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }),
+ expected,
+ 'arrayLimit 1'
+ );
+
+ st.end();
+ });
+
+ t.test('uses existing array length for currentArrayLength when parsing object input with bracket keys', function (st) {
+ var input = {};
+ var arr = ['x', 'y'];
+ arr.a = ['z', 'w'];
+ input['a[]'] = arr;
+ st.deepEqual(qs.parse(input), { a: ['x', 'y'] }, 'parses object input with bracket keys using existing array values');
+ st.end();
+ });
+
+ t.test('throws with singular message when object input bracket key exceeds arrayLimit of 1', function (st) {
+ var input = {};
+ var arr = ['x'];
+ arr.a = ['z', 'w'];
+ input['a[]'] = arr;
+ st['throws'](
+ function () {
+ qs.parse(input, { throwOnLimitExceeded: true, arrayLimit: 1 });
+ },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws singular error for object input exceeding arrayLimit 1'
+ );
+ st.end();
+ });
+
+ t.end();
+});
diff --git a/node_modules/qs/test/stringify.js b/node_modules/qs/test/stringify.js
new file mode 100644
index 0000000..07324cf
--- /dev/null
+++ b/node_modules/qs/test/stringify.js
@@ -0,0 +1,1448 @@
+'use strict';
+
+var test = require('tape');
+var qs = require('../');
+var utils = require('../lib/utils');
+var iconv = require('iconv-lite');
+var SaferBuffer = require('safer-buffer').Buffer;
+var hasSymbols = require('has-symbols');
+var mockProperty = require('mock-property');
+var emptyTestCases = require('./empty-keys-cases').emptyTestCases;
+var hasProto = require('has-proto')();
+var hasBigInt = require('has-bigints')();
+
+test('stringify()', function (t) {
+ t.test('stringifies a querystring object', function (st) {
+ st.equal(qs.stringify({ a: 'b' }), 'a=b');
+ st.equal(qs.stringify({ a: 1 }), 'a=1');
+ st.equal(qs.stringify({ a: 1, b: 2 }), 'a=1&b=2');
+ st.equal(qs.stringify({ a: 'A_Z' }), 'a=A_Z');
+ st.equal(qs.stringify({ a: '€' }), 'a=%E2%82%AC');
+ st.equal(qs.stringify({ a: '' }), 'a=%EE%80%80');
+ st.equal(qs.stringify({ a: 'א' }), 'a=%D7%90');
+ st.equal(qs.stringify({ a: '𐐷' }), 'a=%F0%90%90%B7');
+ st.end();
+ });
+
+ t.test('stringifies falsy values', function (st) {
+ st.equal(qs.stringify(undefined), '');
+ st.equal(qs.stringify(null), '');
+ st.equal(qs.stringify(null, { strictNullHandling: true }), '');
+ st.equal(qs.stringify(false), '');
+ st.equal(qs.stringify(0), '');
+ st.end();
+ });
+
+ t.test('stringifies symbols', { skip: !hasSymbols() }, function (st) {
+ st.equal(qs.stringify(Symbol.iterator), '');
+ st.equal(qs.stringify([Symbol.iterator]), '0=Symbol%28Symbol.iterator%29');
+ st.equal(qs.stringify({ a: Symbol.iterator }), 'a=Symbol%28Symbol.iterator%29');
+ st.equal(
+ qs.stringify({ a: [Symbol.iterator] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
+ 'a[]=Symbol%28Symbol.iterator%29'
+ );
+ st.end();
+ });
+
+ t.test('stringifies bigints', { skip: !hasBigInt }, function (st) {
+ var three = BigInt(3);
+ var encodeWithN = function (value, defaultEncoder, charset) {
+ var result = defaultEncoder(value, defaultEncoder, charset);
+ return typeof value === 'bigint' ? result + 'n' : result;
+ };
+ st.equal(qs.stringify(three), '');
+ st.equal(qs.stringify([three]), '0=3');
+ st.equal(qs.stringify([three], { encoder: encodeWithN }), '0=3n');
+ st.equal(qs.stringify({ a: three }), 'a=3');
+ st.equal(qs.stringify({ a: three }, { encoder: encodeWithN }), 'a=3n');
+ st.equal(
+ qs.stringify({ a: [three] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
+ 'a[]=3'
+ );
+ st.equal(
+ qs.stringify({ a: [three] }, { encodeValuesOnly: true, encoder: encodeWithN, arrayFormat: 'brackets' }),
+ 'a[]=3n'
+ );
+ st.end();
+ });
+
+ t.test('encodes dot in key of object when encodeDotInKeys and allowDots is provided', function (st) {
+ st.equal(
+ qs.stringify(
+ { 'name.obj': { first: 'John', last: 'Doe' } },
+ { allowDots: false, encodeDotInKeys: false }
+ ),
+ 'name.obj%5Bfirst%5D=John&name.obj%5Blast%5D=Doe',
+ 'with allowDots false and encodeDotInKeys false'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj': { first: 'John', last: 'Doe' } },
+ { allowDots: true, encodeDotInKeys: false }
+ ),
+ 'name.obj.first=John&name.obj.last=Doe',
+ 'with allowDots true and encodeDotInKeys false'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj': { first: 'John', last: 'Doe' } },
+ { allowDots: false, encodeDotInKeys: true }
+ ),
+ 'name%252Eobj%5Bfirst%5D=John&name%252Eobj%5Blast%5D=Doe',
+ 'with allowDots false and encodeDotInKeys true'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj': { first: 'John', last: 'Doe' } },
+ { allowDots: true, encodeDotInKeys: true }
+ ),
+ 'name%252Eobj.first=John&name%252Eobj.last=Doe',
+ 'with allowDots true and encodeDotInKeys true'
+ );
+
+ st.equal(
+ qs.stringify(
+ { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
+ { allowDots: false, encodeDotInKeys: false }
+ ),
+ 'name.obj.subobject%5Bfirst.godly.name%5D=John&name.obj.subobject%5Blast%5D=Doe',
+ 'with allowDots false and encodeDotInKeys false'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
+ { allowDots: true, encodeDotInKeys: false }
+ ),
+ 'name.obj.subobject.first.godly.name=John&name.obj.subobject.last=Doe',
+ 'with allowDots false and encodeDotInKeys false'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
+ { allowDots: false, encodeDotInKeys: true }
+ ),
+ 'name%252Eobj%252Esubobject%5Bfirst.godly.name%5D=John&name%252Eobj%252Esubobject%5Blast%5D=Doe',
+ 'with allowDots false and encodeDotInKeys true'
+ );
+ st.equal(
+ qs.stringify(
+ { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
+ { allowDots: true, encodeDotInKeys: true }
+ ),
+ 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
+ 'with allowDots true and encodeDotInKeys true'
+ );
+
+ st.end();
+ });
+
+ t.test('should encode dot in key of object, and automatically set allowDots to `true` when encodeDotInKeys is true and allowDots in undefined', function (st) {
+ st.equal(
+ qs.stringify(
+ { 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } },
+ { encodeDotInKeys: true }
+ ),
+ 'name%252Eobj%252Esubobject.first%252Egodly%252Ename=John&name%252Eobj%252Esubobject.last=Doe',
+ 'with allowDots undefined and encodeDotInKeys true'
+ );
+ st.end();
+ });
+
+ t.test('should encode dot in key of object when encodeDotInKeys and allowDots is provided, and nothing else when encodeValuesOnly is provided', function (st) {
+ st.equal(
+ qs.stringify({ 'name.obj': { first: 'John', last: 'Doe' } }, {
+ encodeDotInKeys: true, allowDots: true, encodeValuesOnly: true
+ }),
+ 'name%2Eobj.first=John&name%2Eobj.last=Doe'
+ );
+
+ st.equal(
+ qs.stringify({ 'name.obj.subobject': { 'first.godly.name': 'John', last: 'Doe' } }, { allowDots: true, encodeDotInKeys: true, encodeValuesOnly: true }),
+ 'name%2Eobj%2Esubobject.first%2Egodly%2Ename=John&name%2Eobj%2Esubobject.last=Doe'
+ );
+
+ st.end();
+ });
+
+ t.test('throws when `commaRoundTrip` is not a boolean', function (st) {
+ st['throws'](
+ function () { qs.stringify({}, { commaRoundTrip: 'not a boolean' }); },
+ TypeError,
+ 'throws when `commaRoundTrip` is not a boolean'
+ );
+
+ st.end();
+ });
+
+ t.test('throws when `encodeDotInKeys` is not a boolean', function (st) {
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: 'foobar' }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: 0 }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: NaN }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { encodeDotInKeys: null }); },
+ TypeError
+ );
+
+ st.end();
+ });
+
+ t.test('adds query prefix', function (st) {
+ st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
+ st.end();
+ });
+
+ t.test('with query prefix, outputs blank string given an empty object', function (st) {
+ st.equal(qs.stringify({}, { addQueryPrefix: true }), '');
+ st.end();
+ });
+
+ t.test('stringifies nested falsy values', function (st) {
+ st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D=');
+ st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D');
+ st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false');
+ st.end();
+ });
+
+ t.test('stringifies a nested object', function (st) {
+ st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
+ st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
+ st.end();
+ });
+
+ t.test('`allowDots` option: stringifies a nested object with dots notation', function (st) {
+ st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
+ st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
+ st.end();
+ });
+
+ t.test('stringifies an array value', function (st) {
+ st.equal(
+ qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'indices' }),
+ 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'brackets' }),
+ 'a%5B%5D=b&a%5B%5D=c&a%5B%5D=d',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }),
+ 'a=b%2Cc%2Cd',
+ 'comma => comma'
+ );
+ st.equal(
+ qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma', commaRoundTrip: true }),
+ 'a=b%2Cc%2Cd',
+ 'comma round trip => comma'
+ );
+ st.equal(
+ qs.stringify({ a: ['b', 'c', 'd'] }),
+ 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d',
+ 'default => indices'
+ );
+ st.end();
+ });
+
+ t.test('`skipNulls` option', function (st) {
+ st.equal(
+ qs.stringify({ a: 'b', c: null }, { skipNulls: true }),
+ 'a=b',
+ 'omits nulls when asked'
+ );
+
+ st.equal(
+ qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }),
+ 'a%5Bb%5D=c',
+ 'omits nested nulls when asked'
+ );
+
+ st.end();
+ });
+
+ t.test('omits array indices when asked', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false }), 'a=b&a=c&a=d');
+
+ st.end();
+ });
+
+ t.test('omits object key/value pair when value is empty array', function (st) {
+ st.equal(qs.stringify({ a: [], b: 'zz' }), 'b=zz');
+
+ st.end();
+ });
+
+ t.test('should not omit object key/value pair when value is empty array and when asked', function (st) {
+ st.equal(qs.stringify({ a: [], b: 'zz' }), 'b=zz');
+ st.equal(qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: false }), 'b=zz');
+ st.equal(qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: true }), 'a[]&b=zz');
+
+ st.end();
+ });
+
+ t.test('should throw when allowEmptyArrays is not of type boolean', function (st) {
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: 'foobar' }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: 0 }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: NaN }); },
+ TypeError
+ );
+
+ st['throws'](
+ function () { qs.stringify({ a: [], b: 'zz' }, { allowEmptyArrays: null }); },
+ TypeError
+ );
+
+ st.end();
+ });
+
+ t.test('allowEmptyArrays + strictNullHandling', function (st) {
+ st.equal(
+ qs.stringify(
+ { testEmptyArray: [] },
+ { strictNullHandling: true, allowEmptyArrays: true }
+ ),
+ 'testEmptyArray[]'
+ );
+
+ st.end();
+ });
+
+ t.test('stringifies an array value with one item vs multiple items', function (st) {
+ st.test('non-array item', function (s2t) {
+ s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=c');
+ s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=c');
+ s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
+ s2t.equal(qs.stringify({ a: 'c' }, { encodeValuesOnly: true }), 'a=c');
+
+ s2t.end();
+ });
+
+ st.test('array with a single item', function (s2t) {
+ s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c');
+ s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c');
+ s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c');
+ s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a[]=c'); // so it parses back as an array
+ s2t.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true }), 'a[0]=c');
+
+ s2t.end();
+ });
+
+ st.test('array with multiple items', function (s2t) {
+ s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c&a[1]=d');
+ s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c&a[]=d');
+ s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c,d');
+ s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c,d');
+ s2t.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true }), 'a[0]=c&a[1]=d');
+
+ s2t.end();
+ });
+
+ st.test('array with multiple items with a comma inside', function (s2t) {
+ s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c%2Cd,e');
+ s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma' }), 'a=c%2Cd%2Ce');
+
+ s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { encodeValuesOnly: true, arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd,e');
+ s2t.equal(qs.stringify({ a: ['c,d', 'e'] }, { arrayFormat: 'comma', commaRoundTrip: true }), 'a=c%2Cd%2Ce');
+
+ s2t.end();
+ });
+
+ st.end();
+ });
+
+ t.test('stringifies a nested array value', function (st) {
+ st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d');
+ st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d');
+ st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[b]=c,d');
+ st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true }), 'a[b][0]=c&a[b][1]=d');
+ st.end();
+ });
+
+ t.test('stringifies comma and empty array values', function (st) {
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'indices' }), 'a[0]=,&a[1]=&a[2]=c,d%');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'brackets' }), 'a[]=,&a[]=&a[]=c,d%');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'comma' }), 'a=,,,c,d%');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: false, arrayFormat: 'repeat' }), 'a=,&a=&a=c,d%');
+
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=%2C&a[1]=&a[2]=c%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=%2C&a[]=&a[]=c%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C,,c%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
+
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'indices' }), 'a%5B0%5D=%2C&a%5B1%5D=&a%5B2%5D=c%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'brackets' }), 'a%5B%5D=%2C&a%5B%5D=&a%5B%5D=c%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C%2C%2Cc%2Cd%25');
+ st.equal(qs.stringify({ a: [',', '', 'c,d%'] }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&a=&a=c%2Cd%25');
+
+ st.end();
+ });
+
+ t.test('stringifies comma and empty non-array values', function (st) {
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'indices' }), 'a=,&b=&c=c,d%');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'brackets' }), 'a=,&b=&c=c,d%');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'comma' }), 'a=,&b=&c=c,d%');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: false, arrayFormat: 'repeat' }), 'a=,&b=&c=c,d%');
+
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
+
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'indices' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'brackets' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'comma' }), 'a=%2C&b=&c=c%2Cd%25');
+ st.equal(qs.stringify({ a: ',', b: '', c: 'c,d%' }, { encode: true, encodeValuesOnly: false, arrayFormat: 'repeat' }), 'a=%2C&b=&c=c%2Cd%25');
+
+ st.end();
+ });
+
+ t.test('stringifies a nested array value with dots notation', function (st) {
+ st.equal(
+ qs.stringify(
+ { a: { b: ['c', 'd'] } },
+ { allowDots: true, encodeValuesOnly: true, arrayFormat: 'indices' }
+ ),
+ 'a.b[0]=c&a.b[1]=d',
+ 'indices: stringifies with dots + indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: { b: ['c', 'd'] } },
+ { allowDots: true, encodeValuesOnly: true, arrayFormat: 'brackets' }
+ ),
+ 'a.b[]=c&a.b[]=d',
+ 'brackets: stringifies with dots + brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: { b: ['c', 'd'] } },
+ { allowDots: true, encodeValuesOnly: true, arrayFormat: 'comma' }
+ ),
+ 'a.b=c,d',
+ 'comma: stringifies with dots + comma'
+ );
+ st.equal(
+ qs.stringify(
+ { a: { b: ['c', 'd'] } },
+ { allowDots: true, encodeValuesOnly: true }
+ ),
+ 'a.b[0]=c&a.b[1]=d',
+ 'default: stringifies with dots + indices'
+ );
+ st.end();
+ });
+
+ t.test('stringifies an object inside an array', function (st) {
+ st.equal(
+ qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
+ 'a[0][b]=c',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
+ 'a[b]=c',
+ 'repeat => repeat'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
+ 'a[][b]=c',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 'c' }] }, { encodeValuesOnly: true }),
+ 'a[0][b]=c',
+ 'default => indices'
+ );
+
+ st.equal(
+ qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'indices', encodeValuesOnly: true }),
+ 'a[0][b][c][0]=1',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'repeat', encodeValuesOnly: true }),
+ 'a[b][c]=1',
+ 'repeat => repeat'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: { c: [1] } }] }, { arrayFormat: 'brackets', encodeValuesOnly: true }),
+ 'a[][b][c][]=1',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: { c: [1] } }] }, { encodeValuesOnly: true }),
+ 'a[0][b][c][0]=1',
+ 'default => indices'
+ );
+
+ st.end();
+ });
+
+ t.test('stringifies an array with mixed objects and primitives', function (st) {
+ st.equal(
+ qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
+ 'a[0][b]=1&a[1]=2&a[2]=3',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
+ 'a[][b]=1&a[]=2&a[]=3',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true, arrayFormat: 'comma' }),
+ '???',
+ 'brackets => brackets',
+ { skip: 'TODO: figure out what this should do' }
+ );
+ st.equal(
+ qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encodeValuesOnly: true }),
+ 'a[0][b]=1&a[1]=2&a[2]=3',
+ 'default => indices'
+ );
+
+ st.end();
+ });
+
+ t.test('stringifies an object inside an array with dots notation', function (st) {
+ st.equal(
+ qs.stringify(
+ { a: [{ b: 'c' }] },
+ { allowDots: true, encode: false, arrayFormat: 'indices' }
+ ),
+ 'a[0].b=c',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: [{ b: 'c' }] },
+ { allowDots: true, encode: false, arrayFormat: 'brackets' }
+ ),
+ 'a[].b=c',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: [{ b: 'c' }] },
+ { allowDots: true, encode: false }
+ ),
+ 'a[0].b=c',
+ 'default => indices'
+ );
+
+ st.equal(
+ qs.stringify(
+ { a: [{ b: { c: [1] } }] },
+ { allowDots: true, encode: false, arrayFormat: 'indices' }
+ ),
+ 'a[0].b.c[0]=1',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: [{ b: { c: [1] } }] },
+ { allowDots: true, encode: false, arrayFormat: 'brackets' }
+ ),
+ 'a[].b.c[]=1',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: [{ b: { c: [1] } }] },
+ { allowDots: true, encode: false }
+ ),
+ 'a[0].b.c[0]=1',
+ 'default => indices'
+ );
+
+ st.end();
+ });
+
+ t.test('does not omit object keys when indices = false', function (st) {
+ st.equal(qs.stringify({ a: [{ b: 'c' }] }, { indices: false }), 'a%5Bb%5D=c');
+ st.end();
+ });
+
+ t.test('uses indices notation for arrays when indices=true', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c'] }, { indices: true }), 'a%5B0%5D=b&a%5B1%5D=c');
+ st.end();
+ });
+
+ t.test('uses indices notation for arrays when no arrayFormat is specified', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c'] }), 'a%5B0%5D=b&a%5B1%5D=c');
+ st.end();
+ });
+
+ t.test('uses indices notation for arrays when arrayFormat=indices', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' }), 'a%5B0%5D=b&a%5B1%5D=c');
+ st.end();
+ });
+
+ t.test('uses repeat notation for arrays when arrayFormat=repeat', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }), 'a=b&a=c');
+ st.end();
+ });
+
+ t.test('uses brackets notation for arrays when arrayFormat=brackets', function (st) {
+ st.equal(qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' }), 'a%5B%5D=b&a%5B%5D=c');
+ st.end();
+ });
+
+ t.test('stringifies a complicated object', function (st) {
+ st.equal(qs.stringify({ a: { b: 'c', d: 'e' } }), 'a%5Bb%5D=c&a%5Bd%5D=e');
+ st.end();
+ });
+
+ t.test('stringifies an empty value', function (st) {
+ st.equal(qs.stringify({ a: '' }), 'a=');
+ st.equal(qs.stringify({ a: null }, { strictNullHandling: true }), 'a');
+
+ st.equal(qs.stringify({ a: '', b: '' }), 'a=&b=');
+ st.equal(qs.stringify({ a: null, b: '' }, { strictNullHandling: true }), 'a&b=');
+
+ st.equal(qs.stringify({ a: { b: '' } }), 'a%5Bb%5D=');
+ st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: true }), 'a%5Bb%5D');
+ st.equal(qs.stringify({ a: { b: null } }, { strictNullHandling: false }), 'a%5Bb%5D=');
+
+ st.end();
+ });
+
+ t.test('stringifies an empty array in different arrayFormat', function (st) {
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c');
+ // arrayFormat default
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', commaRoundTrip: true }), 'b[]=&c=c');
+ // with strictNullHandling
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true, commaRoundTrip: true }), 'b[]&c=c');
+ // with skipNulls
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c');
+ st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c');
+
+ st.end();
+ });
+
+ t.test('does not crash on null/undefined entries in arrayFormat=comma with encodeValuesOnly', function (st) {
+ st.doesNotThrow(
+ function () { qs.stringify({ a: [null, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
+ 'does not pass a raw null array entry to the encoder'
+ );
+ st.doesNotThrow(
+ function () { qs.stringify({ a: [undefined, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
+ 'does not pass a raw undefined array entry to the encoder'
+ );
+ st.doesNotThrow(
+ function () { qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true }); },
+ 'does not crash on a single-null array'
+ );
+
+ st.equal(
+ qs.stringify({ a: [null, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
+ 'a=,b',
+ 'null entry joins as empty, comma stays unencoded under encodeValuesOnly'
+ );
+ st.equal(
+ qs.stringify({ a: [undefined, 'b'] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
+ 'a=,b',
+ 'undefined entry joins as empty, comma stays unencoded under encodeValuesOnly'
+ );
+ st.equal(
+ qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true }),
+ 'a=',
+ 'single-null array stringifies as empty value'
+ );
+ st.equal(
+ qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true, strictNullHandling: true }),
+ 'a',
+ 'strictNullHandling drops the equals sign for a single-null array'
+ );
+ st.equal(
+ qs.stringify({ a: [null] }, { arrayFormat: 'comma', encodeValuesOnly: true, skipNulls: true }),
+ '',
+ 'skipNulls drops a single-null array entirely'
+ );
+
+ st.end();
+ });
+
+ t.test('stringifies a null object', { skip: !hasProto }, function (st) {
+ st.equal(qs.stringify({ __proto__: null, a: 'b' }), 'a=b');
+ st.end();
+ });
+
+ t.test('returns an empty string for invalid input', function (st) {
+ st.equal(qs.stringify(undefined), '');
+ st.equal(qs.stringify(false), '');
+ st.equal(qs.stringify(null), '');
+ st.equal(qs.stringify(''), '');
+ st.end();
+ });
+
+ t.test('stringifies an object with a null object as a child', { skip: !hasProto }, function (st) {
+ st.equal(qs.stringify({ a: { __proto__: null, b: 'c' } }), 'a%5Bb%5D=c');
+ st.end();
+ });
+
+ t.test('drops keys with a value of undefined', function (st) {
+ st.equal(qs.stringify({ a: undefined }), '');
+
+ st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: true }), 'a%5Bc%5D');
+ st.equal(qs.stringify({ a: { b: undefined, c: null } }, { strictNullHandling: false }), 'a%5Bc%5D=');
+ st.equal(qs.stringify({ a: { b: undefined, c: '' } }), 'a%5Bc%5D=');
+ st.end();
+ });
+
+ t.test('url encodes values', function (st) {
+ st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
+ st.end();
+ });
+
+ t.test('stringifies a date', function (st) {
+ var now = new Date();
+ var str = 'a=' + encodeURIComponent(now.toISOString());
+ st.equal(qs.stringify({ a: now }), str);
+ st.end();
+ });
+
+ t.test('stringifies the weird object from qs', function (st) {
+ st.equal(qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' }), 'my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F');
+ st.end();
+ });
+
+ t.test('skips properties that are part of the object prototype', function (st) {
+ st.intercept(Object.prototype, 'crash', { value: 'test' });
+
+ st.equal(qs.stringify({ a: 'b' }), 'a=b');
+ st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
+
+ st.end();
+ });
+
+ t.test('stringifies boolean values', function (st) {
+ st.equal(qs.stringify({ a: true }), 'a=true');
+ st.equal(qs.stringify({ a: { b: true } }), 'a%5Bb%5D=true');
+ st.equal(qs.stringify({ b: false }), 'b=false');
+ st.equal(qs.stringify({ b: { c: false } }), 'b%5Bc%5D=false');
+ st.end();
+ });
+
+ t.test('stringifies buffer values', function (st) {
+ st.equal(qs.stringify({ a: SaferBuffer.from('test') }), 'a=test');
+ st.equal(qs.stringify({ a: { b: SaferBuffer.from('test') } }), 'a%5Bb%5D=test');
+ st.end();
+ });
+
+ t.test('stringifies an object using an alternative delimiter', function (st) {
+ st.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
+ st.end();
+ });
+
+ t.test('does not blow up when Buffer global is missing', function (st) {
+ var restore = mockProperty(global, 'Buffer', { 'delete': true });
+
+ var result = qs.stringify({ a: 'b', c: 'd' });
+
+ restore();
+
+ st.equal(result, 'a=b&c=d');
+ st.end();
+ });
+
+ t.test('does not crash when parsing circular references', function (st) {
+ var a = {};
+ a.b = a;
+
+ st['throws'](
+ function () { qs.stringify({ 'foo[bar]': 'baz', 'foo[baz]': a }); },
+ /RangeError: Cyclic object value/,
+ 'cyclic values throw'
+ );
+
+ var circular = {
+ a: 'value'
+ };
+ circular.a = circular;
+ st['throws'](
+ function () { qs.stringify(circular); },
+ /RangeError: Cyclic object value/,
+ 'cyclic values throw'
+ );
+
+ var arr = ['a'];
+ st.doesNotThrow(
+ function () { qs.stringify({ x: arr, y: arr }); },
+ 'non-cyclic values do not throw'
+ );
+
+ st.end();
+ });
+
+ t.test('non-circular duplicated references can still work', function (st) {
+ var hourOfDay = {
+ 'function': 'hour_of_day'
+ };
+
+ var p1 = {
+ 'function': 'gte',
+ arguments: [hourOfDay, 0]
+ };
+ var p2 = {
+ 'function': 'lte',
+ arguments: [hourOfDay, 23]
+ };
+
+ st.equal(
+ qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }),
+ 'filters[$and][0][function]=gte&filters[$and][0][arguments][0][function]=hour_of_day&filters[$and][0][arguments][1]=0&filters[$and][1][function]=lte&filters[$and][1][arguments][0][function]=hour_of_day&filters[$and][1][arguments][1]=23'
+ );
+ st.equal(
+ qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }),
+ 'filters[$and][][function]=gte&filters[$and][][arguments][][function]=hour_of_day&filters[$and][][arguments][]=0&filters[$and][][function]=lte&filters[$and][][arguments][][function]=hour_of_day&filters[$and][][arguments][]=23'
+ );
+ st.equal(
+ qs.stringify({ filters: { $and: [p1, p2] } }, { encodeValuesOnly: true, arrayFormat: 'repeat' }),
+ 'filters[$and][function]=gte&filters[$and][arguments][function]=hour_of_day&filters[$and][arguments]=0&filters[$and][function]=lte&filters[$and][arguments][function]=hour_of_day&filters[$and][arguments]=23'
+ );
+
+ st.end();
+ });
+
+ t.test('selects properties when filter=array', function (st) {
+ st.equal(qs.stringify({ a: 'b' }, { filter: ['a'] }), 'a=b');
+ st.equal(qs.stringify({ a: 1 }, { filter: [] }), '');
+
+ st.equal(
+ qs.stringify(
+ { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
+ { filter: ['a', 'b', 0, 2], arrayFormat: 'indices' }
+ ),
+ 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
+ 'indices => indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
+ { filter: ['a', 'b', 0, 2], arrayFormat: 'brackets' }
+ ),
+ 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=3',
+ 'brackets => brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: { b: [1, 2, 3, 4], c: 'd' }, c: 'f' },
+ { filter: ['a', 'b', 0, 2] }
+ ),
+ 'a%5Bb%5D%5B0%5D=1&a%5Bb%5D%5B2%5D=3',
+ 'default => indices'
+ );
+
+ st.end();
+ });
+
+ t.test('skips null/undefined entries in filter=array', function (st) {
+ st.doesNotThrow(
+ function () { qs.stringify({ a: 'b', undefined: 'x' }, { filter: ['a', undefined] }); },
+ 'does not pass a raw undefined filter entry to the encoder'
+ );
+ st.doesNotThrow(
+ function () { qs.stringify({ a: 'b', 'null': 'x' }, { filter: ['a', null] }); },
+ 'does not pass a raw null filter entry to the encoder'
+ );
+
+ st.equal(
+ qs.stringify({ a: 'b', undefined: 'x', c: 'd' }, { filter: ['a', undefined, 'c'] }),
+ 'a=b&c=d',
+ 'undefined filter entry is skipped, remaining keys are kept'
+ );
+ st.equal(
+ qs.stringify({ a: 'b', 'null': 'x', c: 'd' }, { filter: ['a', null, 'c'] }),
+ 'a=b&c=d',
+ 'null filter entry is skipped, remaining keys are kept'
+ );
+ st.equal(
+ qs.stringify({ a: 'b', 'null': 'x' }, { filter: [null] }),
+ '',
+ 'filter array containing only null yields empty string'
+ );
+
+ st.end();
+ });
+
+ t.test('supports custom representations when filter=function', function (st) {
+ var calls = 0;
+ var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
+ var filterFunc = function (prefix, value) {
+ calls += 1;
+ if (calls === 1) {
+ st.equal(prefix, '', 'prefix is empty');
+ st.equal(value, obj);
+ } else if (prefix === 'c') {
+ return void 0;
+ } else if (value instanceof Date) {
+ st.equal(prefix, 'e[f]');
+ return value.getTime();
+ }
+ return value;
+ };
+
+ st.equal(qs.stringify(obj, { filter: filterFunc }), 'a=b&e%5Bf%5D=1257894000000');
+ st.equal(calls, 5);
+ st.end();
+ });
+
+ t.test('can disable uri encoding', function (st) {
+ st.equal(qs.stringify({ a: 'b' }, { encode: false }), 'a=b');
+ st.equal(qs.stringify({ a: { b: 'c' } }, { encode: false }), 'a[b]=c');
+ st.equal(qs.stringify({ a: 'b', c: null }, { strictNullHandling: true, encode: false }), 'a=b&c');
+ st.end();
+ });
+
+ t.test('can sort the keys', function (st) {
+ var sort = function (a, b) {
+ return a.localeCompare(b);
+ };
+ st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
+ st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
+ st.end();
+ });
+
+ t.test('can sort the keys at depth 3 or more too', function (st) {
+ var sort = function (a, b) {
+ return a.localeCompare(b);
+ };
+ st.equal(
+ qs.stringify(
+ { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
+ { sort: sort, encode: false }
+ ),
+ 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb'
+ );
+ st.equal(
+ qs.stringify(
+ { a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
+ { sort: null, encode: false }
+ ),
+ 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b'
+ );
+ st.end();
+ });
+
+ t.test('can stringify with custom encoding', function (st) {
+ st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
+ encoder: function (str) {
+ if (str.length === 0) {
+ return '';
+ }
+ var buf = iconv.encode(str, 'shiftjis');
+ var result = [];
+ for (var i = 0; i < buf.length; ++i) {
+ result.push(buf.readUInt8(i).toString(16));
+ }
+ return '%' + result.join('%');
+ }
+ }), '%8c%a7=%91%e5%8d%e3%95%7b&=');
+ st.end();
+ });
+
+ t.test('receives the default encoder as a second argument', function (st) {
+ st.plan(8);
+
+ qs.stringify({ a: 1, b: new Date(), c: true, d: [1] }, {
+ encoder: function (str) {
+ st.match(typeof str, /^(?:string|number|boolean)$/);
+ return '';
+ }
+ });
+
+ st.end();
+ });
+
+ t.test('receives the default encoder as a second argument', function (st) {
+ st.plan(2);
+
+ qs.stringify({ a: 1 }, {
+ encoder: function (str, defaultEncoder) {
+ st.equal(defaultEncoder, utils.encode);
+ }
+ });
+
+ st.end();
+ });
+
+ t.test('throws error with wrong encoder', function (st) {
+ st['throws'](function () {
+ qs.stringify({}, { encoder: 'string' });
+ }, new TypeError('Encoder has to be a function.'));
+ st.end();
+ });
+
+ t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
+ st.equal(qs.stringify({ a: SaferBuffer.from([1]) }, {
+ encoder: function (buffer) {
+ if (typeof buffer === 'string') {
+ return buffer;
+ }
+ return String.fromCharCode(buffer.readUInt8(0) + 97);
+ }
+ }), 'a=b');
+
+ st.equal(qs.stringify({ a: SaferBuffer.from('a b') }, {
+ encoder: function (buffer) {
+ return buffer;
+ }
+ }), 'a=a b');
+ st.end();
+ });
+
+ t.test('serializeDate option', function (st) {
+ var date = new Date();
+ st.equal(
+ qs.stringify({ a: date }),
+ 'a=' + date.toISOString().replace(/:/g, '%3A'),
+ 'default is toISOString'
+ );
+
+ var mutatedDate = new Date();
+ mutatedDate.toISOString = function () {
+ throw new SyntaxError();
+ };
+ st['throws'](function () {
+ mutatedDate.toISOString();
+ }, SyntaxError);
+ st.equal(
+ qs.stringify({ a: mutatedDate }),
+ 'a=' + Date.prototype.toISOString.call(mutatedDate).replace(/:/g, '%3A'),
+ 'toISOString works even when method is not locally present'
+ );
+
+ var specificDate = new Date(6);
+ st.equal(
+ qs.stringify(
+ { a: specificDate },
+ { serializeDate: function (d) { return d.getTime() * 7; } }
+ ),
+ 'a=42',
+ 'custom serializeDate function called'
+ );
+
+ st.equal(
+ qs.stringify(
+ { a: [date] },
+ {
+ serializeDate: function (d) { return d.getTime(); },
+ arrayFormat: 'comma'
+ }
+ ),
+ 'a=' + date.getTime(),
+ 'works with arrayFormat comma'
+ );
+ st.equal(
+ qs.stringify(
+ { a: [date] },
+ {
+ serializeDate: function (d) { return d.getTime(); },
+ arrayFormat: 'comma',
+ commaRoundTrip: true
+ }
+ ),
+ 'a%5B%5D=' + date.getTime(),
+ 'works with arrayFormat comma'
+ );
+
+ st.end();
+ });
+
+ t.test('RFC 1738 serialization', function (st) {
+ st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
+ st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
+ st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC1738 }), 'a+b=a+b');
+
+ st.equal(qs.stringify({ 'foo(ref)': 'bar' }, { format: qs.formats.RFC1738 }), 'foo(ref)=bar');
+
+ st.end();
+ });
+
+ t.test('RFC 3986 spaces serialization', function (st) {
+ st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
+ st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
+ st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }, { format: qs.formats.RFC3986 }), 'a%20b=a%20b');
+
+ st.end();
+ });
+
+ t.test('Backward compatibility to RFC 3986', function (st) {
+ st.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
+ st.equal(qs.stringify({ 'a b': SaferBuffer.from('a b') }), 'a%20b=a%20b');
+
+ st.end();
+ });
+
+ t.test('Edge cases and unknown formats', function (st) {
+ ['UFO1234', false, 1234, null, {}, []].forEach(function (format) {
+ st['throws'](
+ function () {
+ qs.stringify({ a: 'b c' }, { format: format });
+ },
+ new TypeError('Unknown format option provided.')
+ );
+ });
+ st.end();
+ });
+
+ t.test('encodeValuesOnly', function (st) {
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
+ { encodeValuesOnly: true, arrayFormat: 'indices' }
+ ),
+ 'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h',
+ 'encodeValuesOnly + indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
+ { encodeValuesOnly: true, arrayFormat: 'brackets' }
+ ),
+ 'a=b&c[]=d&c[]=e%3Df&f[][]=g&f[][]=h',
+ 'encodeValuesOnly + brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
+ { encodeValuesOnly: true, arrayFormat: 'repeat' }
+ ),
+ 'a=b&c=d&c=e%3Df&f=g&f=h',
+ 'encodeValuesOnly + repeat'
+ );
+
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
+ { arrayFormat: 'indices' }
+ ),
+ 'a=b&c%5B0%5D=d&c%5B1%5D=e&f%5B0%5D%5B0%5D=g&f%5B1%5D%5B0%5D=h',
+ 'no encodeValuesOnly + indices'
+ );
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
+ { arrayFormat: 'brackets' }
+ ),
+ 'a=b&c%5B%5D=d&c%5B%5D=e&f%5B%5D%5B%5D=g&f%5B%5D%5B%5D=h',
+ 'no encodeValuesOnly + brackets'
+ );
+ st.equal(
+ qs.stringify(
+ { a: 'b', c: ['d', 'e'], f: [['g'], ['h']] },
+ { arrayFormat: 'repeat' }
+ ),
+ 'a=b&c=d&c=e&f=g&f=h',
+ 'no encodeValuesOnly + repeat'
+ );
+
+ st.end();
+ });
+
+ t.test('encodeValuesOnly - strictNullHandling', function (st) {
+ st.equal(
+ qs.stringify(
+ { a: { b: null } },
+ { encodeValuesOnly: true, strictNullHandling: true }
+ ),
+ 'a[b]'
+ );
+ st.end();
+ });
+
+ t.test('strictNullHandling: applies the formatter to the encoded key (RFC1738)', function (st) {
+ st.equal(
+ qs.stringify(
+ { 'a b': null, 'c d': 'e f' },
+ { strictNullHandling: false, format: 'RFC1738' }
+ ),
+ 'a+b=&c+d=e+f',
+ 'without: as expected'
+ );
+
+ st.equal(
+ qs.stringify(
+ { 'a b': null, 'c d': 'e f' },
+ { strictNullHandling: true, format: 'RFC1738' }
+ ),
+ 'a+b&c+d=e+f',
+ 'with: as expected'
+ );
+
+ st.end();
+ });
+
+ t.test('throws if an invalid charset is specified', function (st) {
+ st['throws'](function () {
+ qs.stringify({ a: 'b' }, { charset: 'foobar' });
+ }, new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'));
+ st.end();
+ });
+
+ t.test('respects a charset of iso-8859-1', function (st) {
+ st.equal(qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' }), '%E6=%E6');
+ st.end();
+ });
+
+ t.test('encodes unrepresentable chars as numeric entities in iso-8859-1 mode', function (st) {
+ st.equal(qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' }), 'a=%26%239786%3B');
+ st.end();
+ });
+
+ t.test('respects an explicit charset of utf-8 (the default)', function (st) {
+ st.equal(qs.stringify({ a: 'æ' }, { charset: 'utf-8' }), 'a=%C3%A6');
+ st.end();
+ });
+
+ t.test('`charsetSentinel` option', function (st) {
+ st.equal(
+ qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'utf-8' }),
+ 'utf8=%E2%9C%93&a=%C3%A6',
+ 'adds the right sentinel when instructed to and the charset is utf-8'
+ );
+
+ st.equal(
+ qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' }),
+ 'utf8=%26%2310003%3B&a=%E6',
+ 'adds the right sentinel when instructed to and the charset is iso-8859-1'
+ );
+
+ st.equal(
+ qs.stringify({ a: 1, b: 2 }, { charsetSentinel: true, delimiter: ';' }),
+ 'utf8=%E2%9C%93;a=1;b=2',
+ 'uses the configured delimiter after the sentinel'
+ );
+
+ st.end();
+ });
+
+ t.test('does not mutate the options argument', function (st) {
+ var options = {};
+ qs.stringify({}, options);
+ st.deepEqual(options, {});
+ st.end();
+ });
+
+ t.test('strictNullHandling works with custom filter', function (st) {
+ var filter = function (prefix, value) {
+ return value;
+ };
+
+ var options = { strictNullHandling: true, filter: filter };
+ st.equal(qs.stringify({ key: null }, options), 'key');
+ st.end();
+ });
+
+ t.test('strictNullHandling works with null serializeDate', function (st) {
+ var serializeDate = function () {
+ return null;
+ };
+ var options = { strictNullHandling: true, serializeDate: serializeDate };
+ var date = new Date();
+ st.equal(qs.stringify({ key: date }, options), 'key');
+ st.end();
+ });
+
+ t.test('allows for encoding keys and values differently', function (st) {
+ var encoder = function (str, defaultEncoder, charset, type) {
+ if (type === 'key') {
+ return defaultEncoder(str, defaultEncoder, charset, type).toLowerCase();
+ }
+ if (type === 'value') {
+ return defaultEncoder(str, defaultEncoder, charset, type).toUpperCase();
+ }
+ throw 'this should never happen! type: ' + type;
+ };
+
+ st.deepEqual(qs.stringify({ KeY: 'vAlUe' }, { encoder: encoder }), 'key=VALUE');
+
+ var noopEncoder = function () { return 'x'; };
+ noopEncoder();
+ st['throws'](
+ function () { encoder('x', noopEncoder, 'utf-8', 'unknown'); },
+ 'this should never happen! type: unknown',
+ 'encoder throws for unexpected type'
+ );
+
+ st.end();
+ });
+
+ t.test('objects inside arrays', function (st) {
+ var obj = { a: { b: { c: 'd', e: 'f' } } };
+ var withArray = { a: { b: [{ c: 'd', e: 'f' }] } };
+
+ st.equal(qs.stringify(obj, { encode: false }), 'a[b][c]=d&a[b][e]=f', 'no array, no arrayFormat');
+ st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'brackets' }), 'a[b][c]=d&a[b][e]=f', 'no array, bracket');
+ st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'indices' }), 'a[b][c]=d&a[b][e]=f', 'no array, indices');
+ st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'no array, repeat');
+ st.equal(qs.stringify(obj, { encode: false, arrayFormat: 'comma' }), 'a[b][c]=d&a[b][e]=f', 'no array, comma');
+
+ st.equal(qs.stringify(withArray, { encode: false }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, no arrayFormat');
+ st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'brackets' }), 'a[b][][c]=d&a[b][][e]=f', 'array, bracket');
+ st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'indices' }), 'a[b][0][c]=d&a[b][0][e]=f', 'array, indices');
+ st.equal(qs.stringify(withArray, { encode: false, arrayFormat: 'repeat' }), 'a[b][c]=d&a[b][e]=f', 'array, repeat');
+ st.equal(
+ qs.stringify(withArray, { encode: false, arrayFormat: 'comma' }),
+ '???',
+ 'array, comma',
+ { skip: 'TODO: figure out what this should do' }
+ );
+
+ st.end();
+ });
+
+ t.test('stringifies sparse arrays', function (st) {
+ /* eslint no-sparse-arrays: 0 */
+ st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1]=2&a[4]=1');
+ st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=2&a[]=1');
+ st.equal(qs.stringify({ a: [, '2', , , '1'] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a=2&a=1');
+
+ st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][b][2][c]=1');
+ st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][b][][c]=1');
+ st.equal(qs.stringify({ a: [, { b: [, , { c: '1' }] }] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[b][c]=1');
+
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c]=1');
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c]=1');
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: '1' }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
+
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[1][2][3][c][1]=1');
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[][][][c][]=1');
+ st.equal(qs.stringify({ a: [, [, , [, , , { c: [, '1'] }]]] }, { encodeValuesOnly: true, arrayFormat: 'repeat' }), 'a[c]=1');
+
+ st.end();
+ });
+
+ t.test('encodes a very long string', function (st) {
+ var chars = [];
+ var expected = [];
+ for (var i = 0; i < 5e3; i++) {
+ chars.push(' ' + i);
+
+ expected.push('%20' + i);
+ }
+
+ var obj = {
+ foo: chars.join('')
+ };
+
+ st.equal(
+ qs.stringify(obj, { arrayFormat: 'brackets', charset: 'utf-8' }),
+ 'foo=' + expected.join('')
+ );
+
+ st.end();
+ });
+
+ t.end();
+});
+
+test('stringifies empty keys', function (t) {
+ emptyTestCases.forEach(function (testCase) {
+ t.test('stringifies an object with empty string key with ' + testCase.input, function (st) {
+ st.deepEqual(
+ qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'indices' }),
+ testCase.stringifyOutput.indices,
+ 'test case: ' + testCase.input + ', indices'
+ );
+ st.deepEqual(
+ qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'brackets' }),
+ testCase.stringifyOutput.brackets,
+ 'test case: ' + testCase.input + ', brackets'
+ );
+ st.deepEqual(
+ qs.stringify(testCase.withEmptyKeys, { encode: false, arrayFormat: 'repeat' }),
+ testCase.stringifyOutput.repeat,
+ 'test case: ' + testCase.input + ', repeat'
+ );
+
+ st.end();
+ });
+ });
+
+ t.test('edge case with object/arrays', function (st) {
+ st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false }), '[][0]=2&[][1]=3');
+ st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false }), '[][0]=2&[][1]=3&[a]=2');
+ st.deepEqual(qs.stringify({ '': { '': [2, 3] } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3');
+ st.deepEqual(qs.stringify({ '': { '': [2, 3], a: 2 } }, { encode: false, arrayFormat: 'indices' }), '[][0]=2&[][1]=3&[a]=2');
+
+ st.end();
+ });
+
+ t.test('stringifies non-string keys', function (st) {
+ var S = Object('abc');
+ S.toString = function () {
+ return 'd';
+ };
+ var actual = qs.stringify({ a: 'b', 'false': {}, 1e+22: 'c', d: 'e' }, {
+ filter: ['a', false, null, 10000000000000000000000, S],
+ allowDots: true,
+ encodeDotInKeys: true
+ });
+
+ st.equal(actual, 'a=b&1e%2B22=c&d=e', 'stringifies correctly');
+
+ st.end();
+ });
+
+ t.test('round-trips keys containing percent-encoded bracket text', function (st) {
+ var cases = [
+ { 'a%5Bb': 'c' },
+ { 'a%5Db': 'c' },
+ { 'a%255Bb': 'c' },
+ { 'a%255Db': 'c' },
+ { a: { 'b%5Bc': 'd' } },
+ { a: { 'b%255Bc': 'd' } },
+ { 'a%5B%255Bb': 'c' }
+ ];
+ for (var i = 0; i < cases.length; i++) {
+ st.deepEqual(
+ qs.parse(qs.stringify(cases[i])),
+ cases[i],
+ 'round-trips ' + JSON.stringify(cases[i])
+ );
+ }
+
+ st.end();
+ });
+
+ t.test('parses input containing percent-encoded bracket text without mangling', function (st) {
+ st.deepEqual(qs.parse('a%25255Bb=c'), { 'a%255Bb': 'c' }, 'a%25255Bb decodes to a%255Bb, not a%5Bb');
+ st.deepEqual(qs.parse('a%25255Db=c'), { 'a%255Db': 'c' }, 'a%25255Db decodes to a%255Db, not a%5Db');
+ st.deepEqual(qs.parse('a%5Bb%25255Bc%5D=d'), { a: { 'b%255Bc': 'd' } }, 'nested %25255B decodes to %255B inside segment, not %5B');
+
+ st.end();
+ });
+});
diff --git a/node_modules/qs/test/utils.js b/node_modules/qs/test/utils.js
new file mode 100644
index 0000000..713b89b
--- /dev/null
+++ b/node_modules/qs/test/utils.js
@@ -0,0 +1,626 @@
+'use strict';
+
+var test = require('tape');
+var inspect = require('object-inspect');
+var SaferBuffer = require('safer-buffer').Buffer;
+var forEach = require('for-each');
+var v = require('es-value-fixtures');
+
+var utils = require('../lib/utils');
+
+test('merge()', function (t) {
+ t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
+
+ t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
+
+ t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
+
+ var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
+ t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
+
+ var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
+ t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
+
+ var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
+ t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
+
+ var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
+ t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
+
+ var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
+ t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
+
+ var func = function f() {};
+ func();
+ t.deepEqual(
+ utils.merge(func, { foo: 'bar' }),
+ [func, { foo: 'bar' }],
+ 'functions can not be merged into'
+ );
+
+ func.bar = 'baz';
+ t.deepEqual(
+ utils.merge({ foo: 'bar' }, func),
+ { foo: 'bar', bar: 'baz' },
+ 'functions can be merge sources'
+ );
+
+ t.test(
+ 'avoids invoking array setters unnecessarily',
+ { skip: typeof Object.defineProperty !== 'function' },
+ function (st) {
+ var setCount = 0;
+ var getCount = 0;
+ var observed = [];
+ Object.defineProperty(observed, 0, {
+ get: function () {
+ getCount += 1;
+ return { bar: 'baz' };
+ },
+ set: function () { setCount += 1; }
+ });
+ utils.merge(observed, [null]);
+ st.equal(setCount, 0);
+ st.equal(getCount, 1);
+ observed[0] = observed[0]; // eslint-disable-line no-self-assign
+ st.equal(setCount, 1);
+ st.equal(getCount, 2);
+
+ st.end();
+ }
+ );
+
+ t.test('with overflow objects (from arrayLimit)', function (st) {
+ // arrayLimit is max index, so with limit 0, max index 0 is allowed (1 element)
+ // To create overflow, need 2+ elements with limit 0, or 3+ with limit 1, etc.
+ st.test('merges primitive into overflow object at next index', function (s2t) {
+ // Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
+ var overflow = utils.combine(['a', 'b'], 'c', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
+ var merged = utils.merge(overflow, 'd');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds primitive at next numeric index');
+
+ s2t.end();
+ });
+
+ st.test('merges primitive into regular object with numeric keys normally', function (s2t) {
+ var obj = { 0: 'a', 1: 'b' };
+ s2t.notOk(utils.isOverflow(obj), 'plain object is not marked as overflow');
+ var merged = utils.merge(obj, 'c');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b', c: true }, 'adds primitive as key (not at next index)');
+ s2t.end();
+ });
+
+ st.test('merges primitive into object with non-numeric keys normally', function (s2t) {
+ var obj = { foo: 'bar' };
+ var merged = utils.merge(obj, 'baz');
+ s2t.deepEqual(merged, { foo: 'bar', baz: true }, 'adds primitive as key with value true');
+
+ s2t.end();
+ });
+
+ st.test('with strictMerge, wraps object and primitive in array', function (s2t) {
+ var obj = { foo: 'bar' };
+ var merged = utils.merge(obj, 'baz', { strictMerge: true });
+ s2t.deepEqual(merged, [{ foo: 'bar' }, 'baz'], 'wraps in array with strictMerge');
+ s2t.end();
+ });
+
+ st.test('merges overflow object into primitive', function (s2t) {
+ // Create an overflow object via combine: 2 elements (indices 0-1) with limit 0
+ var overflow = utils.combine(['a'], 'b', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
+ var merged = utils.merge('c', overflow);
+ s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
+ s2t.deepEqual(merged, { 0: 'c', 1: 'a', 2: 'b' }, 'creates object with primitive at 0, source values shifted');
+
+ s2t.end();
+ });
+
+ st.test('merges overflow object into primitive with plainObjects', function (s2t) {
+ var overflow = utils.combine(['a'], 'b', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
+ var merged = utils.merge('c', overflow, { plainObjects: true });
+ s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
+ s2t.deepEqual(merged, { __proto__: null, 0: 'c', 1: 'a', 2: 'b' }, 'creates null-proto object with primitive at 0');
+
+ s2t.end();
+ });
+
+ st.test('merges overflow object with multiple values into primitive', function (s2t) {
+ // Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
+ var overflow = utils.combine(['b', 'c'], 'd', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
+ var merged = utils.merge('a', overflow);
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'shifts all source indices by 1');
+
+ s2t.end();
+ });
+
+ st.test('merges regular object into primitive as array', function (s2t) {
+ var obj = { foo: 'bar' };
+ var merged = utils.merge('a', obj);
+ s2t.deepEqual(merged, ['a', { foo: 'bar' }], 'creates array with primitive and object');
+
+ s2t.end();
+ });
+
+ st.test('merges primitive into array that exceeds arrayLimit', function (s2t) {
+ var arr = ['a', 'b', 'c'];
+ var merged = utils.merge(arr, 'd', { arrayLimit: 1 });
+ s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to overflow object with primitive appended');
+
+ s2t.end();
+ });
+
+ st.test('merges array into primitive that exceeds arrayLimit', function (s2t) {
+ var merged = utils.merge('a', ['b', 'c'], { arrayLimit: 1 });
+ s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'converts to overflow object');
+
+ s2t.end();
+ });
+
+ st.test('merges primitive into array at the arrayLimit boundary, consistently with combine', function (s2t) {
+ var merged = utils.merge(['a'], 'b', { arrayLimit: 1 });
+ s2t.ok(utils.isOverflow(merged), 'result is marked as overflow at the boundary');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b' }, 'converts to overflow object instead of a length-2 array');
+
+ s2t.end();
+ });
+
+ st.test('merges two arrays that exceed arrayLimit into an overflow object', function (s2t) {
+ var merged = utils.merge(['a'], ['b'], { arrayLimit: 1 });
+ s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
+ s2t.deepEqual(merged, { 0: 'a', 1: 'b' }, 'array-into-array merge enforces arrayLimit like combine');
+
+ s2t.end();
+ });
+
+ st.test('throws at the arrayLimit boundary when merging a primitive into an array with throwOnLimitExceeded', function (s2t) {
+ s2t['throws'](
+ function () { utils.merge(['a'], 'b', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws when the resulting length would exceed arrayLimit'
+ );
+
+ s2t.end();
+ });
+
+ st.test('throws when merging two arrays past arrayLimit with throwOnLimitExceeded', function (s2t) {
+ s2t['throws'](
+ function () { utils.merge(['a'], ['b'], { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'array-into-array merge throws rather than silently exceeding arrayLimit'
+ );
+ s2t['throws'](
+ function () { utils.merge(['a', 'b', 'c'], ['d', 'e', 'f'], { arrayLimit: 2, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 2 elements allowed in an array.'),
+ 'uses the plural message when arrayLimit is not 1'
+ );
+
+ s2t.end();
+ });
+
+ st.test('throws instead of merging primitive into over-limit array when throwOnLimitExceeded is set', function (s2t) {
+ s2t['throws'](
+ function () { utils.merge(['a', 'b', 'c'], 'd', { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws rather than converting to an overflow object'
+ );
+ s2t['throws'](
+ function () { utils.merge(['a', 'b', 'c'], 'd', { arrayLimit: 2, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 2 elements allowed in an array.'),
+ 'uses the plural message when arrayLimit is not 1'
+ );
+
+ s2t.end();
+ });
+
+ st.test('throws instead of merging array into primitive when throwOnLimitExceeded is set', function (s2t) {
+ s2t['throws'](
+ function () { utils.merge('a', ['b', 'c'], { arrayLimit: 1, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'throws rather than converting to an overflow object'
+ );
+ s2t['throws'](
+ function () { utils.merge('a', ['b', 'c', 'd'], { arrayLimit: 2, throwOnLimitExceeded: true }); },
+ new RangeError('Array limit exceeded. Only 2 elements allowed in an array.'),
+ 'uses the plural message when arrayLimit is not 1'
+ );
+
+ s2t.end();
+ });
+
+ st.end();
+ });
+
+ t.end();
+});
+
+test('assign()', function (t) {
+ var target = { a: 1, b: 2 };
+ var source = { b: 3, c: 4 };
+ var result = utils.assign(target, source);
+
+ t.equal(result, target, 'returns the target');
+ t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
+ t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
+
+ t.end();
+});
+
+test('combine()', function (t) {
+ t.test('both arrays', function (st) {
+ var a = [1];
+ var b = [2];
+ var combined = utils.combine(a, b);
+
+ st.deepEqual(a, [1], 'a is not mutated');
+ st.deepEqual(b, [2], 'b is not mutated');
+ st.notEqual(a, combined, 'a !== combined');
+ st.notEqual(b, combined, 'b !== combined');
+ st.deepEqual(combined, [1, 2], 'combined is a + b');
+
+ st.end();
+ });
+
+ t.test('one array, one non-array', function (st) {
+ var aN = 1;
+ var a = [aN];
+ var bN = 2;
+ var b = [bN];
+
+ var combinedAnB = utils.combine(aN, b);
+ st.deepEqual(b, [bN], 'b is not mutated');
+ st.notEqual(aN, combinedAnB, 'aN + b !== aN');
+ st.notEqual(a, combinedAnB, 'aN + b !== a');
+ st.notEqual(bN, combinedAnB, 'aN + b !== bN');
+ st.notEqual(b, combinedAnB, 'aN + b !== b');
+ st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
+
+ var combinedABn = utils.combine(a, bN);
+ st.deepEqual(a, [aN], 'a is not mutated');
+ st.notEqual(aN, combinedABn, 'a + bN !== aN');
+ st.notEqual(a, combinedABn, 'a + bN !== a');
+ st.notEqual(bN, combinedABn, 'a + bN !== bN');
+ st.notEqual(b, combinedABn, 'a + bN !== b');
+ st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
+
+ st.end();
+ });
+
+ t.test('neither is an array', function (st) {
+ var combined = utils.combine(1, 2);
+ st.notEqual(1, combined, '1 + 2 !== 1');
+ st.notEqual(2, combined, '1 + 2 !== 2');
+ st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
+
+ st.end();
+ });
+
+ t.test('with arrayLimit', function (st) {
+ st.test('under the limit', function (s2t) {
+ var combined = utils.combine(['a', 'b'], 'c', 10, false);
+ s2t.deepEqual(combined, ['a', 'b', 'c'], 'returns array when under limit');
+ s2t.ok(Array.isArray(combined), 'result is an array');
+ s2t.end();
+ });
+
+ st.test('exactly at the limit stays as array', function (s2t) {
+ var combined = utils.combine(['a', 'b'], 'c', 3, false);
+ s2t.deepEqual(combined, ['a', 'b', 'c'], 'stays as array when count equals limit');
+ s2t.ok(Array.isArray(combined), 'result is an array');
+ s2t.end();
+ });
+
+ st.test('over the limit', function (s2t) {
+ var combined = utils.combine(['a', 'b', 'c'], 'd', 3, false);
+ s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to object when over limit');
+ s2t.notOk(Array.isArray(combined), 'result is not an array');
+ s2t.end();
+ });
+
+ st.test('with arrayLimit 1', function (s2t) {
+ var combined = utils.combine([], 'a', 1, false);
+ s2t.deepEqual(combined, ['a'], 'stays as array when count equals limit');
+ s2t.ok(Array.isArray(combined), 'result is an array');
+ s2t.end();
+ });
+
+ st.test('with arrayLimit 0 converts single element to object', function (s2t) {
+ var combined = utils.combine([], 'a', 0, false);
+ s2t.deepEqual(combined, { 0: 'a' }, 'converts to object when count exceeds limit');
+ s2t.notOk(Array.isArray(combined), 'result is not an array');
+ s2t.end();
+ });
+
+ st.test('with arrayLimit 0 and two elements converts to object', function (s2t) {
+ var combined = utils.combine(['a'], 'b', 0, false);
+ s2t.deepEqual(combined, { 0: 'a', 1: 'b' }, 'converts to object when count exceeds limit');
+ s2t.notOk(Array.isArray(combined), 'result is not an array');
+ s2t.end();
+ });
+
+ st.test('with plainObjects option', function (s2t) {
+ var combined = utils.combine(['a', 'b'], 'c', 1, true);
+ var expected = { __proto__: null, 0: 'a', 1: 'b', 2: 'c' };
+ s2t.deepEqual(combined, expected, 'converts to object with null prototype');
+ s2t.equal(Object.getPrototypeOf(combined), null, 'result has null prototype when plainObjects is true');
+ s2t.end();
+ });
+
+ st.end();
+ });
+
+ t.test('with throwOnLimitExceeded', function (st) {
+ st.test('throws when concatenation exceeds the limit', function (s2t) {
+ s2t['throws'](
+ function () { utils.combine(['a', 'b', 'c'], 'd', 3, false, true); },
+ new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
+ 'throws instead of converting to an overflow object'
+ );
+ s2t['throws'](
+ function () { utils.combine([], 'a', 0, false, true); },
+ new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
+ 'throws with the correct count at arrayLimit 0'
+ );
+ s2t.end();
+ });
+
+ st.test('throws when adding to an existing overflow object', function (s2t) {
+ var overflow = utils.combine(['a', 'b'], 'c', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
+
+ s2t['throws'](
+ function () { utils.combine(overflow, 'd', 0, false, true); },
+ new RangeError('Array limit exceeded. Only 0 elements allowed in an array.'),
+ 'throws rather than appending to the overflow object'
+ );
+ s2t['throws'](
+ function () { utils.combine(overflow, 'd', 1, false, true); },
+ new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
+ 'uses the singular message at arrayLimit 1'
+ );
+ s2t.end();
+ });
+
+ st.test('does not throw when within the limit', function (s2t) {
+ var combined = utils.combine(['a'], 'b', 5, false, true);
+ s2t.deepEqual(combined, ['a', 'b'], 'returns the array unchanged when under the limit');
+ s2t.end();
+ });
+
+ st.end();
+ });
+
+ t.test('with existing overflow object', function (st) {
+ st.test('adds to existing overflow object at next index', function (s2t) {
+ // Create overflow object first via combine: 3 elements (indices 0-2) with limit 0
+ var overflow = utils.combine(['a', 'b'], 'c', 0, false);
+ s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
+
+ var combined = utils.combine(overflow, 'd', 10, false);
+ s2t.equal(combined, overflow, 'returns the same object (mutated)');
+ s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds value at next numeric index');
+ s2t.end();
+ });
+
+ st.test('does not treat plain object with numeric keys as overflow', function (s2t) {
+ var plainObj = { 0: 'a', 1: 'b' };
+ s2t.notOk(utils.isOverflow(plainObj), 'plain object is not marked as overflow');
+
+ // combine treats this as a regular value, not an overflow object to append to
+ var combined = utils.combine(plainObj, 'c', 10, false);
+ s2t.deepEqual(combined, [{ 0: 'a', 1: 'b' }, 'c'], 'concatenates as regular values');
+ s2t.end();
+ });
+
+ st.end();
+ });
+
+ t.end();
+});
+
+test('decode', function (t) {
+ t.equal(
+ utils.decode('a+b'),
+ 'a b',
+ 'decodes + to space'
+ );
+
+ t.equal(
+ utils.decode('name%2Eobj'),
+ 'name.obj',
+ 'decodes a string'
+ );
+ t.equal(
+ utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),
+ 'name.obj.foo',
+ 'decodes a string in iso-8859-1'
+ );
+
+ t.end();
+});
+
+test('encode', function (t) {
+ forEach(v.nullPrimitives, function (nullish) {
+ t['throws'](
+ function () { utils.encode(nullish); },
+ TypeError,
+ inspect(nullish) + ' is not a string'
+ );
+ });
+
+ t.equal(utils.encode(''), '', 'empty string returns itself');
+ t.deepEqual(utils.encode([]), [], 'empty array returns itself');
+ t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');
+
+ t.test('symbols', { skip: !v.hasSymbols }, function (st) {
+ st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');
+
+ st.end();
+ });
+
+ t.equal(
+ utils.encode('(abc)'),
+ '%28abc%29',
+ 'encodes parentheses'
+ );
+ t.equal(
+ utils.encode({ toString: function () { return '(abc)'; } }),
+ '%28abc%29',
+ 'toStrings and encodes parentheses'
+ );
+
+ t.equal(
+ utils.encode('abc 123 💩', null, 'iso-8859-1'),
+ 'abc%20123%20%26%2355357%3B%26%2356489%3B',
+ 'encodes in iso-8859-1'
+ );
+
+ var longString = '';
+ var expectedString = '';
+ for (var i = 0; i < 1500; i++) {
+ longString += ' ';
+ expectedString += '%20';
+ }
+
+ t.equal(
+ utils.encode(longString),
+ expectedString,
+ 'encodes a long string'
+ );
+
+ var boundary = '';
+ var expected = '';
+ for (var j = 0; j < 1023; j++) {
+ boundary += 'a';
+ expected += 'a';
+ }
+ boundary += '😀';
+ expected += '%F0%9F%98%80';
+
+ t.equal(
+ utils.encode(boundary),
+ expected,
+ 'encodes a surrogate pair split across long-string chunks'
+ );
+
+ var laterBoundary = '';
+ var laterExpected = '';
+ for (var k = 0; k < 2047; k++) {
+ laterBoundary += 'a';
+ laterExpected += 'a';
+ }
+ laterBoundary += '😀';
+ laterExpected += '%F0%9F%98%80';
+
+ t.equal(
+ utils.encode(laterBoundary),
+ laterExpected,
+ 'encodes a surrogate pair split across a later chunk boundary'
+ );
+
+ var twoPairs = '';
+ for (k = 0; k < 1023; k++) {
+ twoPairs += 'a';
+ }
+ twoPairs += '😀';
+ for (k = 0; k < 1022; k++) {
+ twoPairs += 'b';
+ }
+ twoPairs += '😀';
+
+ t.equal(
+ (utils.encode(twoPairs).match(/%F0%9F%98%80/g) || []).length,
+ 2,
+ 'encodes two surrogate pairs each split across a chunk boundary'
+ );
+
+ var roundTrip = '';
+ for (k = 0; k < 1023; k++) {
+ roundTrip += 'a';
+ }
+ roundTrip += '😀';
+
+ t.equal(
+ decodeURIComponent(utils.encode(roundTrip)),
+ roundTrip,
+ 'a boundary-split surrogate pair round-trips through decodeURIComponent'
+ );
+
+ var loneBoundary = '';
+ var loneExpected = '';
+ for (k = 0; k < 1023; k++) {
+ loneBoundary += 'a';
+ loneExpected += 'a';
+ }
+ loneBoundary += '\uD83DX';
+ loneExpected += '%F0%9F%91%98';
+
+ t.equal(
+ utils.encode(loneBoundary),
+ loneExpected,
+ 'a lone high surrogate at a chunk boundary encodes the same as mid-chunk'
+ );
+
+ t.equal(
+ utils.encode('\x28\x29'),
+ '%28%29',
+ 'encodes parens normally'
+ );
+ t.equal(
+ utils.encode('\x28\x29', null, null, null, 'RFC1738'),
+ '()',
+ 'does not encode parens in RFC1738'
+ );
+
+ // todo RFC1738 format
+
+ t.equal(
+ utils.encode('Āက豈'),
+ '%C4%80%E1%80%80%EF%A4%80',
+ 'encodes multibyte chars'
+ );
+
+ t.equal(
+ utils.encode('\uD83D \uDCA9'),
+ '%F0%9F%90%A0%F0%BA%90%80',
+ 'encodes lone surrogates'
+ );
+
+ t.end();
+});
+
+test('isBuffer()', function (t) {
+ var fn = function () {};
+ fn();
+ forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], fn, /a/g], function (x) {
+ t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
+ });
+
+ var fakeBuffer = { constructor: Buffer };
+ t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
+
+ var saferBuffer = SaferBuffer.from('abc');
+ t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
+
+ var buffer = SaferBuffer.from('abc');
+ t.notEqual(saferBuffer, buffer, 'different buffer instances');
+ t.equal(utils.isBuffer(buffer), true, 'another Buffer instance is a buffer');
+ t.end();
+});
+
+test('isRegExp()', function (t) {
+ t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');
+ t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');
+ t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');
+
+ forEach(v.primitives, function (primitive) {
+ t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');
+ });
+
+ t.end();
+});