0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 20:29:23 +01:00
nodejs/test/parallel/test-whatwg-url-custom-searchparams-sort.js
raisinten 3dce4fb85f
test,benchmark: stop requiring URL and URLSearchParams
Since the URL and URLSearchParams classes are available in the
global object, there is no need to require them from 'url'.

PR-URL: https://github.com/nodejs/node/pull/36927
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2021-01-18 13:44:33 -08:00

52 lines
1.3 KiB
JavaScript

'use strict';
// Tests below are not from WPT.
require('../common');
const { test, assert_array_equals } = require('../common/wpt').harness;
// TODO(joyeecheung): upstream this to WPT, if possible - even
// just as a test for large inputs. Other implementations may
// have a similar cutoff anyway.
// Test bottom-up iterative stable merge sort because we only use that
// algorithm to sort > 100 search params.
const tests = [{ input: '', output: [] }];
const pairs = [];
for (let i = 10; i < 100; i++) {
pairs.push([`a${i}`, 'b']);
tests[0].output.push([`a${i}`, 'b']);
}
tests[0].input = pairs.sort(() => Math.random() > 0.5)
.map((pair) => pair.join('=')).join('&');
tests.push(
{
'input': 'z=a&=b&c=d',
'output': [['', 'b'], ['c', 'd'], ['z', 'a']]
}
);
tests.forEach((val) => {
test(() => {
const params = new URLSearchParams(val.input);
let i = 0;
params.sort();
for (const param of params) {
assert_array_equals(param, val.output[i]);
i++;
}
}, `Parse and sort: ${val.input}`);
test(() => {
const url = new URL(`?${val.input}`, 'https://example/');
url.searchParams.sort();
const params = new URLSearchParams(url.search);
let i = 0;
for (const param of params) {
assert_array_equals(param, val.output[i]);
i++;
}
}, `URL parse and sort: ${val.input}`);
});