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