Skip to content

Commit 52b7d5e

Browse files
lukaszewczakMylesBorins
authored andcommitted
test: refactor test-querystring
- change URIError constructor to regular expression in assert.throws - use block-scope for tests that spans multiple statements PR-URL: #12661 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Cai <[email protected]>
1 parent 28add41 commit 52b7d5e

File tree

1 file changed

+70
-54
lines changed

1 file changed

+70
-54
lines changed

test/parallel/test-querystring.js

+70-54
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ qsWeirdObjects.forEach(function(testCase) {
197197
// invalid surrogate pair throws URIError
198198
assert.throws(function() {
199199
qs.stringify({ foo: '\udc00' });
200-
}, URIError);
200+
}, /^URIError: URI malformed$/);
201201

202202
// coerce numbers to string
203203
assert.strictEqual('foo=0', qs.stringify({ foo: 0 }));
@@ -256,43 +256,47 @@ assert.strictEqual(
256256
1);
257257

258258
// Test removing limit
259-
function testUnlimitedKeys() {
260-
const query = {};
259+
{
260+
function testUnlimitedKeys() {
261+
const query = {};
261262

262-
for (let i = 0; i < 2000; i++) query[i] = i;
263+
for (let i = 0; i < 2000; i++) query[i] = i;
263264

264-
const url = qs.stringify(query);
265+
const url = qs.stringify(query);
265266

266-
assert.strictEqual(
267-
Object.keys(qs.parse(url, null, null, { maxKeys: 0 })).length,
267+
assert.strictEqual(
268+
Object.keys(qs.parse(url, null, null, {maxKeys: 0})).length,
268269
2000);
269-
}
270-
testUnlimitedKeys();
270+
}
271271

272+
testUnlimitedKeys();
273+
}
272274

273-
const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
274-
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
275+
{
276+
const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
277+
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
275278
// <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6>
276-
assert.strictEqual(0xd3, b[0]);
277-
assert.strictEqual(0xf2, b[1]);
278-
assert.strictEqual(0x55, b[2]);
279-
assert.strictEqual(0x67, b[3]);
280-
assert.strictEqual(0x1f, b[4]);
281-
assert.strictEqual(0x36, b[5]);
282-
assert.strictEqual(0x76, b[6]);
283-
assert.strictEqual(0x24, b[7]);
284-
assert.strictEqual(0x5e, b[8]);
285-
assert.strictEqual(0x98, b[9]);
286-
assert.strictEqual(0xcb, b[10]);
287-
assert.strictEqual(0x0d, b[11]);
288-
assert.strictEqual(0xac, b[12]);
289-
assert.strictEqual(0xa2, b[13]);
290-
assert.strictEqual(0x2f, b[14]);
291-
assert.strictEqual(0x9d, b[15]);
292-
assert.strictEqual(0xeb, b[16]);
293-
assert.strictEqual(0xd8, b[17]);
294-
assert.strictEqual(0xa2, b[18]);
295-
assert.strictEqual(0xe6, b[19]);
279+
assert.strictEqual(0xd3, b[0]);
280+
assert.strictEqual(0xf2, b[1]);
281+
assert.strictEqual(0x55, b[2]);
282+
assert.strictEqual(0x67, b[3]);
283+
assert.strictEqual(0x1f, b[4]);
284+
assert.strictEqual(0x36, b[5]);
285+
assert.strictEqual(0x76, b[6]);
286+
assert.strictEqual(0x24, b[7]);
287+
assert.strictEqual(0x5e, b[8]);
288+
assert.strictEqual(0x98, b[9]);
289+
assert.strictEqual(0xcb, b[10]);
290+
assert.strictEqual(0x0d, b[11]);
291+
assert.strictEqual(0xac, b[12]);
292+
assert.strictEqual(0xa2, b[13]);
293+
assert.strictEqual(0x2f, b[14]);
294+
assert.strictEqual(0x9d, b[15]);
295+
assert.strictEqual(0xeb, b[16]);
296+
assert.strictEqual(0xd8, b[17]);
297+
assert.strictEqual(0xa2, b[18]);
298+
assert.strictEqual(0xe6, b[19]);
299+
}
296300

297301
assert.strictEqual(qs.unescapeBuffer('a+b', true).toString(), 'a b');
298302
assert.strictEqual(qs.unescapeBuffer('a%').toString(), 'a%');
@@ -303,29 +307,38 @@ assert.strictEqual(qs.unescapeBuffer('a%%').toString(), 'a%%');
303307

304308

305309
// Test custom decode
306-
function demoDecode(str) {
307-
return str + str;
310+
{
311+
function demoDecode(str) {
312+
return str + str;
313+
}
314+
315+
check(qs.parse('a=a&b=b&c=c', null, null, {decodeURIComponent: demoDecode}),
316+
{aa: 'aa', bb: 'bb', cc: 'cc'});
317+
check(qs.parse('a=a&b=b&c=c', null, '==', {decodeURIComponent: (str) => str}),
318+
{'a=a': '', 'b=b': '', 'c=c': ''});
308319
}
309-
check(qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }),
310-
{ aa: 'aa', bb: 'bb', cc: 'cc' });
311-
check(qs.parse('a=a&b=b&c=c', null, '==', { decodeURIComponent: (str) => str }),
312-
{ 'a=a': '', 'b=b': '', 'c=c': '' });
313320

314321
// Test QueryString.unescape
315-
function errDecode(str) {
316-
throw new Error('To jump to the catch scope');
322+
{
323+
function errDecode(str) {
324+
throw new Error('To jump to the catch scope');
325+
}
326+
327+
check(qs.parse('a=a', null, null, {decodeURIComponent: errDecode}),
328+
{a: 'a'});
317329
}
318-
check(qs.parse('a=a', null, null, { decodeURIComponent: errDecode }),
319-
{ a: 'a' });
320330

321331
// Test custom encode
322-
function demoEncode(str) {
323-
return str[0];
332+
{
333+
function demoEncode(str) {
334+
return str[0];
335+
}
336+
337+
const obj = {aa: 'aa', bb: 'bb', cc: 'cc'};
338+
assert.strictEqual(
339+
qs.stringify(obj, null, null, {encodeURIComponent: demoEncode}),
340+
'a=a&b=b&c=c');
324341
}
325-
const obj = { aa: 'aa', bb: 'bb', cc: 'cc' };
326-
assert.strictEqual(
327-
qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }),
328-
'a=a&b=b&c=c');
329342

330343
// Test QueryString.unescapeBuffer
331344
qsUnescapeTestCases.forEach(function(testCase) {
@@ -334,12 +347,15 @@ qsUnescapeTestCases.forEach(function(testCase) {
334347
});
335348

336349
// test overriding .unescape
337-
const prevUnescape = qs.unescape;
338-
qs.unescape = function(str) {
339-
return str.replace(/o/g, '_');
340-
};
341-
check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
342-
qs.unescape = prevUnescape;
343-
350+
{
351+
const prevUnescape = qs.unescape;
352+
qs.unescape = function(str) {
353+
return str.replace(/o/g, '_');
354+
};
355+
check(
356+
qs.parse('foo=bor'),
357+
createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
358+
qs.unescape = prevUnescape;
359+
}
344360
// test separator and "equals" parsing order
345361
check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' });

0 commit comments

Comments
 (0)