Skip to content

Commit b0cfe7c

Browse files
committed
[Tests] increase coverage
1 parent d9b4c66 commit b0cfe7c

3 files changed

Lines changed: 120 additions & 3 deletions

File tree

test/parse.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ test('parse()', function (t) {
213213
st.end();
214214
});
215215

216+
t.test('ignores prototype keys when depth = 0 and allowPrototypes is false', function (st) {
217+
st.deepEqual(qs.parse('toString=foo', { depth: 0 }), {});
218+
st.deepEqual(qs.parse('hasOwnProperty=bar', { depth: 0 }), {});
219+
st.deepEqual(qs.parse('toString=foo&a=b', { depth: 0 }), { a: 'b' });
220+
st.end();
221+
});
222+
223+
t.test('allows prototype keys when depth = 0 and allowPrototypes is true', function (st) {
224+
st.deepEqual(qs.parse('toString=foo', { depth: 0, allowPrototypes: true }), { toString: 'foo' });
225+
st.end();
226+
});
227+
216228
t.test('uses original key when depth = false', function (st) {
217229
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { depth: false }), { 'a[0]': 'b', 'a[1]': 'c' });
218230
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' });
@@ -1074,6 +1086,15 @@ test('parse()', function (t) {
10741086
};
10751087

10761088
st.deepEqual(qs.parse('KeY=vAlUe', { decoder: decoder }), { key: 'VALUE' });
1089+
1090+
var noopDecoder = function () { return 'x'; };
1091+
noopDecoder();
1092+
st['throws'](
1093+
function () { decoder('x', noopDecoder, 'utf-8', 'unknown'); },
1094+
'this should never happen! type: unknown',
1095+
'decoder throws for unexpected type'
1096+
);
1097+
10771098
st.end();
10781099
});
10791100

@@ -1103,6 +1124,14 @@ test('parse()', function (t) {
11031124
new RangeError('Parameter limit exceeded. Only 3 parameters allowed.'),
11041125
'throws error when parameter limit is exceeded'
11051126
);
1127+
1128+
sst['throws'](
1129+
function () {
1130+
qs.parse('a=1&b=2', { parameterLimit: 1, throwOnLimitExceeded: true });
1131+
},
1132+
new RangeError('Parameter limit exceeded. Only 1 parameter allowed.'),
1133+
'throws error with singular "parameter" when parameterLimit is 1'
1134+
);
11061135
sst.end();
11071136
});
11081137

@@ -1189,6 +1218,14 @@ test('parse()', function (t) {
11891218
'throws error when a sparse index exceeds arrayLimit'
11901219
);
11911220

1221+
sst['throws'](
1222+
function () {
1223+
qs.parse('a[2]=b', { arrayLimit: 1, throwOnLimitExceeded: true });
1224+
},
1225+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1226+
'throws error with singular "element" when arrayLimit is 1'
1227+
);
1228+
11921229
sst.end();
11931230
});
11941231

@@ -1206,6 +1243,17 @@ test('parse()', function (t) {
12061243
sst.end();
12071244
});
12081245

1246+
st.test('throws when duplicate bracket keys exceed arrayLimit with throwOnLimitExceeded', function (sst) {
1247+
sst['throws'](
1248+
function () {
1249+
qs.parse('a[]=1&a[]=2&a[]=3&a[]=4&a[]=5&a[]=6', { arrayLimit: 5, throwOnLimitExceeded: true });
1250+
},
1251+
new RangeError('Array limit exceeded. Only 5 elements allowed in an array.'),
1252+
'throws error when duplicate bracket notation exceeds array limit'
1253+
);
1254+
sst.end();
1255+
});
1256+
12091257
st.end();
12101258
});
12111259

@@ -1462,6 +1510,14 @@ test('comma + arrayLimit', function (t) {
14621510
new RangeError('Array limit exceeded. Only 3 elements allowed in an array.'),
14631511
'throws error when comma-split exceeds array limit'
14641512
);
1513+
1514+
st['throws'](
1515+
function () {
1516+
qs.parse('a=1,2,3', { comma: true, arrayLimit: 1, throwOnLimitExceeded: true });
1517+
},
1518+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1519+
'throws error with singular "element" when arrayLimit is 1'
1520+
);
14651521
st.end();
14661522
});
14671523

@@ -1564,5 +1620,29 @@ test('mixed array and object notation', function (t) {
15641620
st.end();
15651621
});
15661622

1623+
t.test('uses existing array length for currentArrayLength when parsing object input with bracket keys', function (st) {
1624+
var input = {};
1625+
var arr = ['x', 'y'];
1626+
arr.a = ['z', 'w'];
1627+
input['a[]'] = arr;
1628+
st.deepEqual(qs.parse(input), { a: ['x', 'y'] }, 'parses object input with bracket keys using existing array values');
1629+
st.end();
1630+
});
1631+
1632+
t.test('throws with singular message when object input bracket key exceeds arrayLimit of 1', function (st) {
1633+
var input = {};
1634+
var arr = ['x'];
1635+
arr.a = ['z', 'w'];
1636+
input['a[]'] = arr;
1637+
st['throws'](
1638+
function () {
1639+
qs.parse(input, { throwOnLimitExceeded: true, arrayLimit: 1 });
1640+
},
1641+
new RangeError('Array limit exceeded. Only 1 element allowed in an array.'),
1642+
'throws singular error for object input exceeding arrayLimit 1'
1643+
);
1644+
st.end();
1645+
});
1646+
15671647
t.end();
15681648
});

test/stringify.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,15 @@ test('stringify()', function (t) {
11881188
};
11891189

11901190
st.deepEqual(qs.stringify({ KeY: 'vAlUe' }, { encoder: encoder }), 'key=VALUE');
1191+
1192+
var noopEncoder = function () { return 'x'; };
1193+
noopEncoder();
1194+
st['throws'](
1195+
function () { encoder('x', noopEncoder, 'utf-8', 'unknown'); },
1196+
'this should never happen! type: unknown',
1197+
'encoder throws for unexpected type'
1198+
);
1199+
11911200
st.end();
11921201
});
11931202

test/utils.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('merge()', function (t) {
3131
t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
3232

3333
var func = function f() {};
34+
func();
3435
t.deepEqual(
3536
utils.merge(func, { foo: 'bar' }),
3637
[func, { foo: 'bar' }],
@@ -112,6 +113,15 @@ test('merge()', function (t) {
112113
s2t.end();
113114
});
114115

116+
st.test('merges overflow object into primitive with plainObjects', function (s2t) {
117+
var overflow = utils.combine(['a'], 'b', 0, false);
118+
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
119+
var merged = utils.merge('c', overflow, { plainObjects: true });
120+
s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
121+
s2t.deepEqual(merged, { __proto__: null, 0: 'c', 1: 'a', 2: 'b' }, 'creates null-proto object with primitive at 0');
122+
s2t.end();
123+
});
124+
115125
st.test('merges overflow object with multiple values into primitive', function (s2t) {
116126
// Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
117127
var overflow = utils.combine(['b', 'c'], 'd', 0, false);
@@ -128,6 +138,21 @@ test('merge()', function (t) {
128138
s2t.end();
129139
});
130140

141+
st.test('merges primitive into array that exceeds arrayLimit', function (s2t) {
142+
var arr = ['a', 'b', 'c'];
143+
var merged = utils.merge(arr, 'd', { arrayLimit: 1 });
144+
s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
145+
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to overflow object with primitive appended');
146+
s2t.end();
147+
});
148+
149+
st.test('merges array into primitive that exceeds arrayLimit', function (s2t) {
150+
var merged = utils.merge('a', ['b', 'c'], { arrayLimit: 1 });
151+
s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
152+
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'converts to overflow object');
153+
s2t.end();
154+
});
155+
131156
st.end();
132157
});
133158

@@ -376,7 +401,9 @@ test('encode', function (t) {
376401
});
377402

378403
test('isBuffer()', function (t) {
379-
forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
404+
var fn = function () {};
405+
fn();
406+
forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], fn, /a/g], function (x) {
380407
t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
381408
});
382409

@@ -386,8 +413,9 @@ test('isBuffer()', function (t) {
386413
var saferBuffer = SaferBuffer.from('abc');
387414
t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
388415

389-
var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');
390-
t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
416+
var buffer = SaferBuffer.from('abc');
417+
t.notEqual(saferBuffer, buffer, 'different buffer instances');
418+
t.equal(utils.isBuffer(buffer), true, 'another Buffer instance is a buffer');
391419
t.end();
392420
});
393421

0 commit comments

Comments
 (0)