tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

url-constructor.any.js (2057B)


      1 // META: script=/common/subset-tests-by-key.js
      2 // META: timeout=long
      3 // META: variant=?include=file
      4 // META: variant=?include=javascript
      5 // META: variant=?include=mailto
      6 // META: variant=?exclude=(file|javascript|mailto)
      7 
      8 function runURLTests(urlTests) {
      9  for (const expected of urlTests) {
     10    // Skip comments
     11    if (typeof expected === "string")
     12      continue;
     13 
     14    const base = expected.base !== null ? expected.base : undefined;
     15 
     16    function getKey(expected) {
     17      if (expected.protocol) {
     18        return expected.protocol.replace(":", "");
     19      }
     20      if (expected.failure) {
     21        return expected.input.split(":")[0];
     22      }
     23      return "other";
     24    }
     25 
     26    subsetTestByKey(getKey(expected), test, function() {
     27      if (expected.failure) {
     28        assert_throws_js(TypeError, function() {
     29          new URL(expected.input, base);
     30        });
     31        return;
     32      }
     33 
     34      const url = new URL(expected.input, base);
     35      assert_equals(url.href, expected.href, "href")
     36      assert_equals(url.protocol, expected.protocol, "protocol")
     37      assert_equals(url.username, expected.username, "username")
     38      assert_equals(url.password, expected.password, "password")
     39      assert_equals(url.host, expected.host, "host")
     40      assert_equals(url.hostname, expected.hostname, "hostname")
     41      assert_equals(url.port, expected.port, "port")
     42      assert_equals(url.pathname, expected.pathname, "pathname")
     43      assert_equals(url.search, expected.search, "search")
     44      if ("searchParams" in expected) {
     45        assert_true("searchParams" in url)
     46        assert_equals(url.searchParams.toString(), expected.searchParams, "searchParams")
     47      }
     48      assert_equals(url.hash, expected.hash, "hash")
     49    }, `Parsing: <${expected.input}> ${base ? "against <" + base + ">" : "without base"}`)
     50  }
     51 }
     52 
     53 promise_test(() => Promise.all([
     54  fetch("resources/urltestdata.json").then(res => res.json()),
     55  fetch("resources/urltestdata-javascript-only.json").then(res => res.json()),
     56 ]).then((tests) => tests.flat()).then(runURLTests), "Loading data…");