Browse Source

test: refactor test-querystring

- change URIError constructor to regular expression in assert.throws
- use block-scope for tests that spans multiple statements

PR-URL: https://github.com/nodejs/node/pull/12661
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: David Cai <davidcai1993@yahoo.com>
v6
Łukasz Szewczak 8 years ago
committed by Anna Henningsen
parent
commit
42958d1a75
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 24
      test/parallel/test-querystring.js

24
test/parallel/test-querystring.js

@ -254,7 +254,7 @@ qsWeirdObjects.forEach(function(testCase) {
// invalid surrogate pair throws URIError
assert.throws(function() {
qs.stringify({ foo: '\udc00' });
}, URIError);
}, /^URIError: URI malformed$/);
// coerce numbers to string
assert.strictEqual('foo=0', qs.stringify({ foo: 0 }));
@ -318,6 +318,7 @@ assert.strictEqual(
0);
// Test removing limit
{
function testUnlimitedKeys() {
const query = {};
@ -329,9 +330,11 @@ function testUnlimitedKeys() {
Object.keys(qs.parse(url, null, null, {maxKeys: 0})).length,
2000);
}
testUnlimitedKeys();
testUnlimitedKeys();
}
{
const b = qs.unescapeBuffer('%d3%f2Ug%1f6v%24%5e%98%cb' +
'%0d%ac%a2%2f%9d%eb%d8%a2%e6');
// <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6>
@ -355,6 +358,7 @@ assert.strictEqual(0xeb, b[16]);
assert.strictEqual(0xd8, b[17]);
assert.strictEqual(0xa2, b[18]);
assert.strictEqual(0xe6, b[19]);
}
assert.strictEqual(qs.unescapeBuffer('a+b', true).toString(), 'a b');
assert.strictEqual(qs.unescapeBuffer('a+b').toString(), 'a+b');
@ -368,29 +372,38 @@ assert.strictEqual(qs.unescapeBuffer('a%%').toString(), 'a%%');
check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
// Test custom decode
{
function demoDecode(str) {
return str + str;
}
check(qs.parse('a=a&b=b&c=c', null, null, {decodeURIComponent: demoDecode}),
{aa: 'aa', bb: 'bb', cc: 'cc'});
check(qs.parse('a=a&b=b&c=c', null, '==', {decodeURIComponent: (str) => str}),
{'a=a': '', 'b=b': '', 'c=c': ''});
}
// Test QueryString.unescape
{
function errDecode(str) {
throw new Error('To jump to the catch scope');
}
check(qs.parse('a=a', null, null, {decodeURIComponent: errDecode}),
{a: 'a'});
}
// Test custom encode
{
function demoEncode(str) {
return str[0];
}
const obj = {aa: 'aa', bb: 'bb', cc: 'cc'};
assert.strictEqual(
qs.stringify(obj, null, null, {encodeURIComponent: demoEncode}),
'a=a&b=b&c=c');
}
// Test QueryString.unescapeBuffer
qsUnescapeTestCases.forEach(function(testCase) {
@ -399,12 +412,15 @@ qsUnescapeTestCases.forEach(function(testCase) {
});
// test overriding .unescape
{
const prevUnescape = qs.unescape;
qs.unescape = function(str) {
return str.replace(/o/g, '_');
};
check(qs.parse('foo=bor'), createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
check(
qs.parse('foo=bor'),
createWithNoPrototype([{key: 'f__', value: 'b_r'}]));
qs.unescape = prevUnescape;
}
// test separator and "equals" parsing order
check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' });

Loading…
Cancel
Save