tor-browser

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

test_URLPattern_parseURLPattern.js (8222B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const { parseURLPattern } = ChromeUtils.importESModule(
      6  "chrome://remote/content/shared/webdriver/URLPattern.sys.mjs"
      7 );
      8 
      9 add_task(async function test_parseURLPattern_stringPatterns() {
     10  const STRING_PATTERN_TESTS = [
     11    {
     12      input: "http://example.com",
     13      protocol: "http",
     14      hostname: "example.com",
     15      port: "",
     16      pathname: "/",
     17      search: "",
     18    },
     19    {
     20      input: "http://example.com/",
     21      protocol: "http",
     22      hostname: "example.com",
     23      port: "",
     24      pathname: "/",
     25      search: "",
     26    },
     27    {
     28      input: "http://EXAMPLE.com",
     29      protocol: "http",
     30      hostname: "example.com",
     31      port: "",
     32      pathname: "/",
     33      search: "",
     34    },
     35    {
     36      input: "http://example%2Ecom",
     37      protocol: "http",
     38      hostname: "example.com",
     39      port: "",
     40      pathname: "/",
     41      search: "",
     42    },
     43 
     44    {
     45      input: "http://example.com:80",
     46      protocol: "http",
     47      hostname: "example.com",
     48      port: "",
     49      pathname: "/",
     50      search: "",
     51    },
     52    {
     53      input: "http://example.com:8888",
     54      protocol: "http",
     55      hostname: "example.com",
     56      port: "8888",
     57      pathname: "/",
     58      search: "",
     59    },
     60    {
     61      input: "http://example.com/a////b",
     62      protocol: "http",
     63      hostname: "example.com",
     64      port: "",
     65      pathname: "/a////b",
     66      search: "",
     67    },
     68    {
     69      input: "http://example.com/?",
     70      protocol: "http",
     71      hostname: "example.com",
     72      port: "",
     73      pathname: "/",
     74      search: "",
     75    },
     76    {
     77      input: "http://example.com/??",
     78      protocol: "http",
     79      hostname: "example.com",
     80      port: "",
     81      pathname: "/",
     82      search: "?",
     83    },
     84    {
     85      input: "http://example.com/?/",
     86      protocol: "http",
     87      hostname: "example.com",
     88      port: "",
     89      pathname: "/",
     90      search: "/",
     91    },
     92    {
     93      input: "file:///testfolder/test.zip",
     94      protocol: "file",
     95      hostname: "",
     96      port: null,
     97      pathname: "/testfolder/test.zip",
     98      search: "",
     99    },
    100    {
    101      input: "http://example\\{.com/",
    102      protocol: "http",
    103      hostname: "example{.com",
    104      port: "",
    105      pathname: "/",
    106      search: "",
    107    },
    108    {
    109      input: "http://[2001:db8::1]/",
    110      protocol: "http",
    111      hostname: "[2001:db8::1]",
    112      port: "",
    113      pathname: "/",
    114      search: "",
    115    },
    116    {
    117      input: "http://127.0.0.1/",
    118      protocol: "http",
    119      hostname: "127.0.0.1",
    120      port: "",
    121      pathname: "/",
    122      search: "",
    123    },
    124  ];
    125 
    126  for (const test of STRING_PATTERN_TESTS) {
    127    const pattern = parseURLPattern({
    128      type: "string",
    129      pattern: test.input,
    130    });
    131 
    132    equal(pattern.protocol, "protocol" in test ? test.protocol : null);
    133    equal(pattern.hostname, "hostname" in test ? test.hostname : null);
    134    equal(pattern.port, "port" in test ? test.port : null);
    135    equal(pattern.pathname, "pathname" in test ? test.pathname : null);
    136    equal(pattern.search, "search" in test ? test.search : null);
    137  }
    138 });
    139 
    140 add_task(async function test_parseURLPattern_patternPatterns() {
    141  const PATTERN_PATTERN_TESTS = [
    142    {
    143      pattern: {
    144        protocol: "http",
    145      },
    146      protocol: "http",
    147      hostname: null,
    148      port: null,
    149      pathname: null,
    150      search: null,
    151    },
    152    {
    153      pattern: {
    154        protocol: "HTTP",
    155      },
    156      protocol: "http",
    157      hostname: null,
    158      port: null,
    159      pathname: null,
    160      search: null,
    161    },
    162    {
    163      pattern: {
    164        hostname: "example.com",
    165      },
    166      protocol: null,
    167      hostname: "example.com",
    168      port: null,
    169      pathname: null,
    170      search: null,
    171    },
    172    {
    173      pattern: {
    174        hostname: "EXAMPLE.com",
    175      },
    176      protocol: null,
    177      hostname: "example.com",
    178      port: null,
    179      pathname: null,
    180      search: null,
    181    },
    182    {
    183      pattern: {
    184        hostname: "127.0.0.1",
    185      },
    186      protocol: null,
    187      hostname: "127.0.0.1",
    188      port: null,
    189      pathname: null,
    190      search: null,
    191    },
    192    {
    193      pattern: {
    194        hostname: "[2001:db8::1]",
    195      },
    196      protocol: null,
    197      hostname: "[2001:db8::1]",
    198      port: null,
    199      pathname: null,
    200      search: null,
    201    },
    202    {
    203      pattern: {
    204        port: "80",
    205      },
    206      protocol: null,
    207      hostname: null,
    208      port: "",
    209      pathname: null,
    210      search: null,
    211    },
    212    {
    213      pattern: {
    214        port: "1234",
    215      },
    216      protocol: null,
    217      hostname: null,
    218      port: "1234",
    219      pathname: null,
    220      search: null,
    221    },
    222    {
    223      pattern: {
    224        pathname: "path/to",
    225      },
    226      protocol: null,
    227      hostname: null,
    228      port: null,
    229      pathname: "/path/to",
    230      search: null,
    231    },
    232    {
    233      pattern: {
    234        pathname: "/path/to",
    235      },
    236      protocol: null,
    237      hostname: null,
    238      port: null,
    239      pathname: "/path/to",
    240      search: null,
    241    },
    242    {
    243      pattern: {
    244        pathname: "/path/to/",
    245      },
    246      protocol: null,
    247      hostname: null,
    248      port: null,
    249      pathname: "/path/to/",
    250      search: null,
    251    },
    252    {
    253      pattern: {
    254        search: "?search",
    255      },
    256      protocol: null,
    257      hostname: null,
    258      port: null,
    259      pathname: null,
    260      search: "search",
    261    },
    262    {
    263      pattern: {
    264        search: "search",
    265      },
    266      protocol: null,
    267      hostname: null,
    268      port: null,
    269      pathname: null,
    270      search: "search",
    271    },
    272    {
    273      pattern: {
    274        search: "?search=something",
    275      },
    276      protocol: null,
    277      hostname: null,
    278      port: null,
    279      pathname: null,
    280      search: "search=something",
    281    },
    282    {
    283      pattern: {
    284        search: "search=something",
    285      },
    286      protocol: null,
    287      hostname: null,
    288      port: null,
    289      pathname: null,
    290      search: "search=something",
    291    },
    292  ];
    293 
    294  for (const test of PATTERN_PATTERN_TESTS) {
    295    const pattern = parseURLPattern({
    296      type: "pattern",
    297      ...test.pattern,
    298    });
    299 
    300    equal(pattern.protocol, "protocol" in test ? test.protocol : null);
    301    equal(pattern.hostname, "hostname" in test ? test.hostname : null);
    302    equal(pattern.port, "port" in test ? test.port : null);
    303    equal(pattern.pathname, "pathname" in test ? test.pathname : null);
    304    equal(pattern.search, "search" in test ? test.search : null);
    305  }
    306 });
    307 
    308 add_task(async function test_parseURLPattern_invalid_type() {
    309  const values = [null, undefined, 1, [], "string"];
    310  for (const value of values) {
    311    Assert.throws(() => parseURLPattern(value), /InvalidArgumentError/);
    312  }
    313 });
    314 
    315 add_task(async function test_parseURLPattern_invalid_type_type() {
    316  const values = [null, undefined, 1, {}, []];
    317  for (const type of values) {
    318    Assert.throws(() => parseURLPattern({ type }), /InvalidArgumentError/);
    319  }
    320 });
    321 
    322 add_task(async function test_parseURLPattern_invalid_type_value() {
    323  const values = ["", "unknownType"];
    324  for (const type of values) {
    325    Assert.throws(() => parseURLPattern({ type }), /InvalidArgumentError/);
    326  }
    327 });
    328 
    329 add_task(async function test_parseURLPattern_invalid_stringPatternType() {
    330  const values = [null, undefined, 1, {}, []];
    331  for (const pattern of values) {
    332    Assert.throws(
    333      () => parseURLPattern({ type: "string", pattern }),
    334      /InvalidArgumentError/
    335    );
    336  }
    337 });
    338 
    339 add_task(async function test_parseURLPattern_invalid_stringPattern() {
    340  const values = [
    341    "foo",
    342    "*",
    343    "(",
    344    ")",
    345    "{",
    346    "}",
    347    "http\\{s\\}://example.com",
    348    "https://example.com:port/",
    349  ];
    350  for (const pattern of values) {
    351    Assert.throws(
    352      () => parseURLPattern({ type: "string", pattern }),
    353      /InvalidArgumentError/
    354    );
    355  }
    356 });
    357 
    358 add_task(async function test_parseURLPattern_invalid_patternPattern_type() {
    359  const properties = ["protocol", "hostname", "port", "pathname", "search"];
    360  const values = [false, 42, [], {}];
    361  for (const property of properties) {
    362    for (const value of values) {
    363      Assert.throws(
    364        () => parseURLPattern({ type: "pattern", [property]: value }),
    365        /InvalidArgumentError/
    366      );
    367    }
    368  }
    369 });