tor-browser

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

urlSearchParams_commons.js (10448B)


      1 /* import-globals-from urlSearchParams_worker.js */
      2 
      3 // This file gets included into a worker which doesn't have any
      4 // assertion methods besides `ok` and `is`.
      5 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
      6 
      7 function testSimpleURLSearchParams() {
      8  var u = new URLSearchParams();
      9  ok(u, "URLSearchParams created");
     10  is(u.has("foo"), false, "URLSearchParams.has(foo)");
     11  is(u.get("foo"), null, "URLSearchParams.get(foo)");
     12  is(u.getAll("foo").length, 0, "URLSearchParams.getAll(foo)");
     13 
     14  u.append("foo", "bar");
     15  is(u.has("foo"), true, "URLSearchParams.has(foo)");
     16  is(u.get("foo"), "bar", "URLSearchParams.get(foo)");
     17  is(u.getAll("foo").length, 1, "URLSearchParams.getAll(foo)");
     18 
     19  u.set("foo", "bar2");
     20  is(u.get("foo"), "bar2", "URLSearchParams.get(foo)");
     21  is(u.getAll("foo").length, 1, "URLSearchParams.getAll(foo)");
     22 
     23  is(u + "", "foo=bar2", "stringifier");
     24 
     25  u.delete("foo");
     26 
     27  runTest();
     28 }
     29 
     30 function testCopyURLSearchParams() {
     31  var u = new URLSearchParams();
     32  ok(u, "URLSearchParams created");
     33  u.append("foo", "bar");
     34 
     35  var uu = new URLSearchParams(u);
     36  is(uu.get("foo"), "bar", "uu.get()");
     37 
     38  u.append("foo", "bar2");
     39  is(u.getAll("foo").length, 2, "u.getAll()");
     40  is(uu.getAll("foo").length, 1, "uu.getAll()");
     41 
     42  runTest();
     43 }
     44 
     45 function testURL() {
     46  var url = new URL("http://www.example.net?a=b&c=d");
     47  ok(url.searchParams, "URL searchParams exists!");
     48  ok(url.searchParams.has("a"), "URL.searchParams.has('a')");
     49  is(url.searchParams.get("a"), "b", "URL.searchParams.get('a')");
     50  ok(url.searchParams.has("c"), "URL.searchParams.has('c')");
     51  is(url.searchParams.get("c"), "d", "URL.searchParams.get('c')");
     52 
     53  url.searchParams.set("e", "f");
     54  ok(url.href.indexOf("e=f") != 1, "URL right");
     55 
     56  url = new URL("mailto:a@b.com?subject=Hi");
     57  ok(url.searchParams, "URL searchParams exists!");
     58  ok(url.searchParams.has("subject"), "Hi");
     59 
     60  runTest();
     61 }
     62 
     63 function testParserURLSearchParams() {
     64  var checks = [
     65    { input: "", data: {} },
     66    { input: "a", data: { a: [""] } },
     67    { input: "a=b", data: { a: ["b"] } },
     68    { input: "a=", data: { a: [""] } },
     69    { input: "=b", data: { "": ["b"] } },
     70    { input: "&", data: {} },
     71    { input: "&a", data: { a: [""] } },
     72    { input: "a&", data: { a: [""] } },
     73    { input: "a&a", data: { a: ["", ""] } },
     74    { input: "a&b&c", data: { a: [""], b: [""], c: [""] } },
     75    { input: "a=b&c=d", data: { a: ["b"], c: ["d"] } },
     76    { input: "a=b&c=d&", data: { a: ["b"], c: ["d"] } },
     77    { input: "&&&a=b&&&&c=d&", data: { a: ["b"], c: ["d"] } },
     78    { input: "a=a&a=b&a=c", data: { a: ["a", "b", "c"] } },
     79    { input: "a==a", data: { a: ["=a"] } },
     80    { input: "a=a+b+c+d", data: { a: ["a b c d"] } },
     81    { input: "%=a", data: { "%": ["a"] } },
     82    { input: "%a=a", data: { "%a": ["a"] } },
     83    { input: "%a_=a", data: { "%a_": ["a"] } },
     84    { input: "%61=a", data: { a: ["a"] } },
     85    { input: "%=a", data: { "%": ["a"] } },
     86    { input: "%a=a", data: { "%a": ["a"] } },
     87    { input: "%a_=a", data: { "%a_": ["a"] } },
     88    { input: "%61=a", data: { a: ["a"] } },
     89    { input: "%61+%4d%4D=", data: { "a MM": [""] } },
     90    { input: "?a=1", data: { a: ["1"] } },
     91    { input: "?", data: {} },
     92    { input: "?=b", data: { "": ["b"] } },
     93  ];
     94 
     95  for (var i = 0; i < checks.length; ++i) {
     96    var u = new URLSearchParams(checks[i].input);
     97 
     98    var count = 0;
     99    for (var key in checks[i].data) {
    100      count = count + 1;
    101      ok(u.has(key), "key " + key + " found");
    102 
    103      var all = u.getAll(key);
    104      is(all.length, checks[i].data[key].length, "same number of elements");
    105 
    106      for (var k = 0; k < all.length; ++k) {
    107        is(all[k], checks[i].data[key][k], "value matches");
    108      }
    109    }
    110  }
    111 
    112  runTest();
    113 }
    114 
    115 function testEncoding() {
    116  var encoding = [
    117    ["1", "1"],
    118    ["a b", "a+b"],
    119    ["<>", "%3C%3E"],
    120    ["\u0541", "%D5%81"],
    121  ];
    122 
    123  for (var i = 0; i < encoding.length; ++i) {
    124    var url = new URL("http://www.example.net");
    125    url.searchParams.set("a", encoding[i][0]);
    126    is(url.href, "http://www.example.net/?a=" + encoding[i][1]);
    127 
    128    var url2 = new URL(url.href);
    129    is(url2.searchParams.get("a"), encoding[i][0], "a is still there");
    130  }
    131 
    132  runTest();
    133 }
    134 
    135 function testCopyConstructor() {
    136  var url = new URL("http://example.com/");
    137  var p = url.searchParams;
    138  var q = new URLSearchParams(p);
    139  q.set("a", "b");
    140  is(
    141    url.href,
    142    "http://example.com/",
    143    "Messing with copy of URLSearchParams should not affect URL"
    144  );
    145  p.set("c", "d");
    146  is(
    147    url.href,
    148    "http://example.com/?c=d",
    149    "Messing with URLSearchParams should affect URL"
    150  );
    151 
    152  runTest();
    153 }
    154 
    155 function testOrdering() {
    156  var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
    157  is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
    158  is(a.getAll("a").length, 3, "Correct length of getAll()");
    159 
    160  var b = new URLSearchParams();
    161  b.append("a", "1");
    162  b.append("b", "2");
    163  b.append("a", "3");
    164  is(b.toString(), "a=1&b=2&a=3", "Order is correct");
    165  is(b.getAll("a").length, 2, "Correct length of getAll()");
    166 
    167  runTest();
    168 }
    169 
    170 function testDelete() {
    171  var a = new URLSearchParams("a=1&a=2&b=3&c=4&c=5&a=6");
    172  is(a.toString(), "a=1&a=2&b=3&c=4&c=5&a=6", "Order is correct");
    173  is(a.getAll("a").length, 3, "Correct length of getAll()");
    174 
    175  a.delete("a");
    176  is(a.getAll("a").length, 0, "Correct length of getAll()");
    177  is(a.toString(), "b=3&c=4&c=5", "Order is correct");
    178 
    179  runTest();
    180 }
    181 
    182 function testGetNULL() {
    183  var u = new URLSearchParams();
    184  is(typeof u.get(""), "object", "typeof URL.searchParams.get('')");
    185  is(u.get(""), null, "URL.searchParams.get('') should be null");
    186 
    187  var url = new URL("http://www.example.net?a=b");
    188  is(
    189    url.searchParams.get("b"),
    190    null,
    191    "URL.searchParams.get('b') should be null"
    192  );
    193  is(url.searchParams.get("a"), "b", "URL.searchParams.get('a')");
    194 
    195  runTest();
    196 }
    197 
    198 function testSet() {
    199  var u = new URLSearchParams();
    200  u.set("a", "b");
    201  u.set("e", "c");
    202  u.set("i", "d");
    203  u.set("o", "f");
    204  u.set("u", "g");
    205 
    206  is(u.get("a"), "b", "URL.searchParams.get('a') should return b");
    207  is(u.getAll("a").length, 1, "URLSearchParams.getAll('a').length should be 1");
    208 
    209  u.set("a", "h1");
    210  u.set("a", "h2");
    211  u.set("a", "h3");
    212  u.set("a", "h4");
    213  is(u.get("a"), "h4", "URL.searchParams.get('a') should return h4");
    214  is(u.getAll("a").length, 1, "URLSearchParams.getAll('a').length should be 1");
    215 
    216  is(u.get("e"), "c", "URL.searchParams.get('e') should return c");
    217  is(u.get("i"), "d", "URL.searchParams.get('i') should return d");
    218  is(u.get("o"), "f", "URL.searchParams.get('o') should return f");
    219  is(u.get("u"), "g", "URL.searchParams.get('u') should return g");
    220 
    221  is(u.getAll("e").length, 1, "URLSearchParams.getAll('e').length should be 1");
    222  is(u.getAll("i").length, 1, "URLSearchParams.getAll('i').length should be 1");
    223  is(u.getAll("o").length, 1, "URLSearchParams.getAll('o').length should be 1");
    224  is(u.getAll("u").length, 1, "URLSearchParams.getAll('u').length should be 1");
    225 
    226  u = new URLSearchParams("name1=value1&name1=value2&name1=value3");
    227  is(
    228    u.get("name1"),
    229    "value1",
    230    "URL.searchParams.get('name1') should return value1"
    231  );
    232  is(
    233    u.getAll("name1").length,
    234    3,
    235    "URLSearchParams.getAll('name1').length should be 3"
    236  );
    237  u.set("name1", "firstPair");
    238  is(
    239    u.get("name1"),
    240    "firstPair",
    241    "URL.searchParams.get('name1') should return firstPair"
    242  );
    243  is(
    244    u.getAll("name1").length,
    245    1,
    246    "URLSearchParams.getAll('name1').length should be 1"
    247  );
    248 
    249  runTest();
    250 }
    251 
    252 function testIterable() {
    253  var u = new URLSearchParams();
    254  u.set("1", "2");
    255  u.set("2", "4");
    256  u.set("3", "6");
    257  u.set("4", "8");
    258  u.set("5", "10");
    259 
    260  var key_iter = u.keys();
    261  var value_iter = u.values();
    262  var entries_iter = u.entries();
    263  for (var i = 0; i < 5; ++i) {
    264    var v = i + 1;
    265    var key = key_iter.next();
    266    var value = value_iter.next();
    267    var entry = entries_iter.next();
    268    is(key.value, v.toString(), "Correct Key iterator: " + v.toString());
    269    ok(!key.done, "Key.done is false");
    270    is(
    271      value.value,
    272      (v * 2).toString(),
    273      "Correct Value iterator: " + (v * 2).toString()
    274    );
    275    ok(!value.done, "Value.done is false");
    276    is(
    277      entry.value[0],
    278      v.toString(),
    279      "Correct Entry 0 iterator: " + v.toString()
    280    );
    281    is(
    282      entry.value[1],
    283      (v * 2).toString(),
    284      "Correct Entry 1 iterator: " + (v * 2).toString()
    285    );
    286    ok(!entry.done, "Entry.done is false");
    287  }
    288 
    289  var last = key_iter.next();
    290  ok(last.done, "Nothing more to read.");
    291  is(last.value, undefined, "Undefined is the last key");
    292 
    293  last = value_iter.next();
    294  ok(last.done, "Nothing more to read.");
    295  is(last.value, undefined, "Undefined is the last value");
    296 
    297  last = entries_iter.next();
    298  ok(last.done, "Nothing more to read.");
    299 
    300  key_iter = u.keys();
    301  key_iter.next();
    302  key_iter.next();
    303  u.delete("1");
    304  u.delete("2");
    305  u.delete("3");
    306  u.delete("4");
    307  u.delete("5");
    308 
    309  last = key_iter.next();
    310  ok(last.done, "Nothing more to read.");
    311  is(last.value, undefined, "Undefined is the last key");
    312 
    313  runTest();
    314 }
    315 function testZeroHandling() {
    316  var u = new URLSearchParams();
    317  u.set("a", "b\0c");
    318  u.set("d\0e", "f");
    319  u.set("g\0h", "i\0j");
    320  is(
    321    u.toString(),
    322    "a=b%00c&d%00e=f&g%00h=i%00j",
    323    "Should encode U+0000 as %00"
    324  );
    325 
    326  runTest();
    327 }
    328 
    329 function testCTORs() {
    330  var a = new URLSearchParams("a=b");
    331  is(a.get("a"), "b", "CTOR with string");
    332 
    333  var b = new URLSearchParams([
    334    ["a", "b"],
    335    ["c", "d"],
    336  ]);
    337  is(b.get("a"), "b", "CTOR with sequence");
    338  is(b.get("c"), "d", "CTOR with sequence");
    339 
    340  ok(new URLSearchParams([]), "CTOR with empty sequence");
    341 
    342  let result;
    343  try {
    344    result = new URLSearchParams([[1]]);
    345  } catch (e) {
    346    result = 42;
    347  }
    348 
    349  is(
    350    result,
    351    42,
    352    "CTOR throws if the sequence doesn't contain exactly 2 elements"
    353  );
    354 
    355  try {
    356    result = new URLSearchParams([[1, 2, 3]]);
    357  } catch (e) {
    358    result = 43;
    359  }
    360  is(
    361    result,
    362    43,
    363    "CTOR throws if the sequence doesn't contain exactly 2 elements"
    364  );
    365 
    366  var c = new URLSearchParams({
    367    a: "b",
    368    c: 42,
    369    d: null,
    370    e: [1, 2, 3],
    371    f: { a: 42 },
    372  });
    373  is(c.get("a"), "b", "CTOR with record<>");
    374  is(c.get("c"), "42", "CTOR with record<>");
    375  is(c.get("d"), "null", "CTOR with record<>");
    376  is(c.get("e"), [1, 2, 3].toString(), "CTOR with record<>");
    377  is(c.get("f"), { a: 42 }.toString(), "CTOR with record<>");
    378 
    379  runTest();
    380 }