• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Tests below are not from WPT.
4
5require('../common');
6const { test, assert_array_equals } = require('../common/wpt').harness;
7
8// TODO(joyeecheung): upstream this to WPT, if possible - even
9// just as a test for large inputs. Other implementations may
10// have a similar cutoff anyway.
11
12// Test bottom-up iterative stable merge sort because we only use that
13// algorithm to sort > 100 search params.
14const tests = [{ input: '', output: [] }];
15const pairs = [];
16for (let i = 10; i < 100; i++) {
17  pairs.push([`a${i}`, 'b']);
18  tests[0].output.push([`a${i}`, 'b']);
19}
20tests[0].input = pairs.sort(() => Math.random() > 0.5)
21  .map((pair) => pair.join('=')).join('&');
22
23tests.push(
24  {
25    'input': 'z=a&=b&c=d',
26    'output': [['', 'b'], ['c', 'd'], ['z', 'a']]
27  }
28);
29
30tests.forEach((val) => {
31  test(() => {
32    const params = new URLSearchParams(val.input);
33    let i = 0;
34    params.sort();
35    for (const param of params) {
36      assert_array_equals(param, val.output[i]);
37      i++;
38    }
39  }, `Parse and sort: ${val.input}`);
40
41  test(() => {
42    const url = new URL(`?${val.input}`, 'https://example/');
43    url.searchParams.sort();
44    const params = new URLSearchParams(url.search);
45    let i = 0;
46    for (const param of params) {
47      assert_array_equals(param, val.output[i]);
48      i++;
49    }
50  }, `URL parse and sort: ${val.input}`);
51});
52