Skip to content

Commit 42958d1

Browse files
lukaszewczakaddaleax
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 cbd6fde commit 42958d1

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
@@ -254,7 +254,7 @@ qsWeirdObjects.forEach(function(testCase) {
254254
// invalid surrogate pair throws URIError
255255
assert.throws(function() {
256256
qs.stringify({ foo: '\udc00' });
257-
}, URIError);
257+
}, /^URIError: URI malformed$/);
258258

259259
// coerce numbers to string
260260
assert.strictEqual('foo=0', qs.stringify({ foo: 0 }));
@@ -318,43 +318,47 @@ assert.strictEqual(
318318
0);
319319

320320
// Test removing limit
321-
function testUnlimitedKeys() {
322-
const query = {};
321+
{
322+
function testUnlimitedKeys() {
323+
const query = {};
323324

324-
for (let i = 0; i < 2000; i++) query[i] = i;
325+
for (let i = 0; i < 2000; i++) query[i] = i;
325326

326-
const url = qs.stringify(query);
327+
const url = qs.stringify(query);
327328

328-
assert.strictEqual(
329-
Object.keys(qs.parse(url, null, null, { maxKeys: 0 })).length,
329+
assert.strictEqual(
330+
Object.keys(qs.parse(url, null, null, {maxKeys: 0})).length,
330331
2000);
331-
}
332-
testUnlimitedKeys();
332+
}
333333

334+
testUnlimitedKeys();
335+
}
334336

335-
const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
336-
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
337+
{
338+
const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
339+
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
337340
// <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6>
338-
assert.strictEqual(0xd3, b[0]);
339-
assert.strictEqual(0xf2, b[1]);
340-
assert.strictEqual(0x55, b[2]);
341-
assert.strictEqual(0x67, b[3]);
342-
assert.strictEqual(0x1f, b[4]);
343-
assert.strictEqual(0x36, b[5]);
344-
assert.strictEqual(0x76, b[6]);
345-
assert.strictEqual(0x24, b[7]);
346-
assert.strictEqual(0x5e, b[8]);
347-
assert.strictEqual(0x98, b[9]);
348-
assert.strictEqual(0xcb, b[10]);
349-
assert.strictEqual(0x0d, b[11]);
350-
assert.strictEqual(0xac, b[12]);
351-
assert.strictEqual(0xa2, b[13]);
352-
assert.strictEqual(0x2f, b[14]);
353-
assert.strictEqual(0x9d, b[15]);
354-
assert.strictEqual(0xeb, b[16]);
355-
assert.strictEqual(0xd8, b[17]);
356-
assert.strictEqual(0xa2, b[18]);
357-
assert.strictEqual(0xe6, b[19]);
341+
assert.strictEqual(0xd3, b[0]);
342+
assert.strictEqual(0xf2, b[1]);
343+
assert.strictEqual(0x55, b[2]);
344+
assert.strictEqual(0x67, b[3]);
345+
assert.strictEqual(0x1f, b[4]);
346+
assert.strictEqual(0x36, b[5]);
347+
assert.strictEqual(0x76, b[6]);
348+
assert.strictEqual(0x24, b[7]);
349+
assert.strictEqual(0x5e, b[8]);
350+
assert.strictEqual(0x98, b[9]);
351+
assert.strictEqual(0xcb, b[10]);
352+
assert.strictEqual(0x0d, b[11]);
353+
assert.strictEqual(0xac, b[12]);
354+
assert.strictEqual(0xa2, b[13]);
355+
assert.strictEqual(0x2f, b[14]);
356+
assert.strictEqual(0x9d, b[15]);
357+
assert.strictEqual(0xeb, b[16]);
358+
assert.strictEqual(0xd8, b[17]);
359+
assert.strictEqual(0xa2, b[18]);
360+
assert.strictEqual(0xe6, b[19]);
361+
}
358362

359363
assert.strictEqual(qs.unescapeBuffer('a+b', true).toString(), 'a b');
360364
assert.strictEqual(qs.unescapeBuffer('a+b').toString(), 'a+b');
@@ -368,29 +372,38 @@ assert.strictEqual(qs.unescapeBuffer('a%%').toString(), 'a%%');
368372
check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
369373

370374
// Test custom decode
371-
function demoDecode(str) {
372-
return str + str;
375+
{
376+
function demoDecode(str) {
377+
return str + str;
378+
}
379+
380+
check(qs.parse('a=a&b=b&c=c', null, null, {decodeURIComponent: demoDecode}),
381+
{aa: 'aa', bb: 'bb', cc: 'cc'});
382+
check(qs.parse('a=a&b=b&c=c', null, '==', {decodeURIComponent: (str) => str}),
383+
{'a=a': '', 'b=b': '', 'c=c': ''});
373384
}
374-
check(qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }),
375-
{ aa: 'aa', bb: 'bb', cc: 'cc' });
376-
check(qs.parse('a=a&b=b&c=c', null, '==', { decodeURIComponent: (str) => str }),
377-
{ 'a=a': '', 'b=b': '', 'c=c': '' });
378385

379386
// Test QueryString.unescape
380-
function errDecode(str) {
381-
throw new Error('To jump to the catch scope');
387+
{
388+
function errDecode(str) {
389+
throw new Error('To jump to the catch scope');
390+
}
391+
392+
check(qs.parse('a=a', null, null, {decodeURIComponent: errDecode}),
393+
{a: 'a'});
382394
}
383-
check(qs.parse('a=a', null, null, { decodeURIComponent: errDecode }),
384-
{ a: 'a' });
385395

386396
// Test custom encode
387-
function demoEncode(str) {
388-
return str[0];
397+
{
398+
function demoEncode(str) {
399+
return str[0];
400+
}
401+
402+
const obj = {aa: 'aa', bb: 'bb', cc: 'cc'};
403+
assert.strictEqual(
404+
qs.stringify(obj, null, null, {encodeURIComponent: demoEncode}),
405+
'a=a&b=b&c=c');
389406
}
390-
const obj = { aa: 'aa', bb: 'bb', cc: 'cc' };
391-
assert.strictEqual(
392-
qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }),
393-
'a=a&b=b&c=c');
394407

395408
// Test QueryString.unescapeBuffer
396409
qsUnescapeTestCases.forEach(function(testCase) {
@@ -399,12 +412,15 @@ qsUnescapeTestCases.forEach(function(testCase) {
399412
});
400413

401414
// test overriding .unescape
402-
const prevUnescape = qs.unescape;
403-
qs.unescape = function(str) {
404-
return str.replace(/o/g, '_');
405-
};
406-
check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
407-
qs.unescape = prevUnescape;
408-
415+
{
416+
const prevUnescape = qs.unescape;
417+
qs.unescape = function(str) {
418+
return str.replace(/o/g, '_');
419+
};
420+
check(
421+
qs.parse('foo=bor'),
422+
createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
423+
qs.unescape = prevUnescape;
424+
}
409425
// test separator and "equals" parsing order
410426
check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' });

0 commit comments

Comments
 (0)