tor-browser

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

test_cache_keys.js (3058B)


      1 /* global context testDone:true */
      2 
      3 var name = "keys" + context;
      4 var c;
      5 
      6 var tests = [
      7  "//mochi.test:8888/?page" + context,
      8  "//mochi.test:8888/?another" + context,
      9 ];
     10 
     11 caches
     12  .open(name)
     13  .then(function (cache) {
     14    c = cache;
     15    return c.addAll(tests);
     16  })
     17  .then(function () {
     18    // Add another cache entry using Cache.add
     19    var another = "//mochi.test:8888/?yetanother" + context;
     20    tests.push(another);
     21    return c.add(another);
     22  })
     23  .then(function () {
     24    // Add another cache entry with URL fragment using Cache.add
     25    var anotherWithFragment =
     26      "//mochi.test:8888/?fragment" + context + "#fragment";
     27    tests.push(anotherWithFragment);
     28    return c.add(anotherWithFragment);
     29  })
     30  .then(function () {
     31    return c.keys();
     32  })
     33  .then(function (keys) {
     34    is(keys.length, tests.length, "Same number of elements");
     35    // Verify both the insertion order of the requests and their validity.
     36    keys.forEach(function (r, i) {
     37      ok(r instanceof Request, "Valid request object");
     38      ok(r.url.includes(tests[i]), "Valid URL");
     39    });
     40    // Try searching for just one request
     41    return c.keys(tests[1]);
     42  })
     43  .then(function (keys) {
     44    is(keys.length, 1, "One match should be found");
     45    ok(keys[0].url.includes(tests[1]), "Valid URL");
     46    // Try to see if ignoreSearch works as expected.
     47    return c.keys(new Request("//mochi.test:8888/?foo"), {
     48      ignoreSearch: true,
     49    });
     50  })
     51  .then(function (key_arr) {
     52    is(key_arr.length, tests.length, "Same number of elements");
     53    key_arr.forEach(function (r, i) {
     54      ok(r instanceof Request, "Valid request object");
     55      ok(r.url.includes(tests[i]), "Valid URL");
     56    });
     57    // Try to see if ignoreMethod works as expected
     58    return Promise.all(
     59      ["POST", "PUT", "DELETE", "OPTIONS"].map(function (method) {
     60        var req = new Request(tests[2], { method });
     61        return c
     62          .keys(req)
     63          .then(function (keys) {
     64            is(
     65              keys.length,
     66              0,
     67              "No request should be matched without ignoreMethod"
     68            );
     69            return c.keys(req, { ignoreMethod: true });
     70          })
     71          .then(function (keys) {
     72            is(keys.length, 1, "One match should be found");
     73            ok(keys[0].url.includes(tests[2]), "Valid URL");
     74          });
     75      })
     76    );
     77  })
     78  .then(function () {
     79    // But HEAD should be allowed only when ignoreMethod is ture
     80    return c.keys(new Request(tests[0], { method: "HEAD" }), {
     81      ignoreMethod: true,
     82    });
     83  })
     84  .then(function (keys) {
     85    is(keys.length, 1, "One match should be found");
     86    ok(keys[0].url.includes(tests[0]), "Valid URL");
     87    // Make sure cacheName is ignored.
     88    return c.keys(tests[0], { cacheName: "non-existing-cache" });
     89  })
     90  .then(function (keys) {
     91    is(keys.length, 1, "One match should be found");
     92    ok(keys[0].url.includes(tests[0]), "Valid URL");
     93    return caches.delete(name);
     94  })
     95  .then(function (deleted) {
     96    ok(deleted, "The cache should be successfully deleted");
     97    testDone();
     98  });