tor-browser

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

test_cache_delete.js (3640B)


      1 /* global context testDone:true */
      2 
      3 var name = "delete" + context;
      4 
      5 function setupTest(reqs) {
      6  return new Promise(function (resolve, reject) {
      7    var cache;
      8    caches
      9      .open(name)
     10      .then(function (c) {
     11        cache = c;
     12        return c.addAll(reqs);
     13      })
     14      .then(function () {
     15        resolve(cache);
     16      })
     17      .catch(function (err) {
     18        reject(err);
     19      });
     20  });
     21 }
     22 
     23 function testBasics() {
     24  var tests = [
     25    "//mochi.test:8888/?foo" + context,
     26    "//mochi.test:8888/?bar" + context,
     27  ];
     28  var cache;
     29  return setupTest(tests)
     30    .then(function (c) {
     31      cache = c;
     32      return cache.delete("//mochi.test:8888/?baz");
     33    })
     34    .then(function (deleted) {
     35      ok(!deleted, "Deleting a non-existing entry should fail");
     36      return cache.keys();
     37    })
     38    .then(function (keys) {
     39      is(keys.length, 2, "No entries from the cache should be deleted");
     40      return cache.delete(tests[0]);
     41    })
     42    .then(function (deleted) {
     43      ok(deleted, "Deleting an existing entry should succeed");
     44      return cache.keys();
     45    })
     46    .then(function (keys) {
     47      is(keys.length, 1, "Only one entry should exist now");
     48      ok(keys[0].url.includes(tests[1]), "The correct entry must be deleted");
     49    });
     50 }
     51 
     52 function testFragment() {
     53  var tests = [
     54    "//mochi.test:8888/?foo" + context,
     55    "//mochi.test:8888/?bar" + context,
     56    "//mochi.test:8888/?baz" + context + "#fragment",
     57  ];
     58  var cache;
     59  return setupTest(tests)
     60    .then(function (c) {
     61      cache = c;
     62      return cache.delete(tests[0] + "#fragment");
     63    })
     64    .then(function (deleted) {
     65      ok(deleted, "Deleting an existing entry should succeed");
     66      return cache.keys();
     67    })
     68    .then(function (keys) {
     69      is(keys.length, 2, "Only one entry should exist now");
     70      ok(keys[0].url.includes(tests[1]), "The correct entry must be deleted");
     71      ok(
     72        keys[1].url.includes(tests[2].replace("#fragment", "")),
     73        "The correct entry must be deleted"
     74      );
     75      // Now, delete a request that was added with a fragment
     76      return cache.delete("//mochi.test:8888/?baz" + context);
     77    })
     78    .then(function (deleted) {
     79      ok(deleted, "Deleting an existing entry should succeed");
     80      return cache.keys();
     81    })
     82    .then(function (keys) {
     83      is(keys.length, 1, "Only one entry should exist now");
     84      ok(keys[0].url.includes(tests[1]), "3The correct entry must be deleted");
     85    });
     86 }
     87 
     88 function testInterleaved() {
     89  var tests = [
     90    "//mochi.test:8888/?foo" + context,
     91    "//mochi.test:8888/?bar" + context,
     92  ];
     93  var newURL = "//mochi.test:8888/?baz" + context;
     94  var cache;
     95  return setupTest(tests)
     96    .then(function (c) {
     97      cache = c;
     98      // Simultaneously add and delete a request
     99      return Promise.all([cache.delete(newURL), cache.add(newURL)]);
    100    })
    101    .then(function (result) {
    102      ok(!result[1], "deletion should fail");
    103      return cache.keys();
    104    })
    105    .then(function (keys) {
    106      is(keys.length, 3, "Tree entries should still exist");
    107      ok(keys[0].url.includes(tests[0]), "The correct entry must be deleted");
    108      ok(keys[1].url.includes(tests[1]), "The correct entry must be deleted");
    109      ok(
    110        keys[2].url.includes(newURL),
    111        "The new entry should be correctly inserted"
    112      );
    113    });
    114 }
    115 
    116 // Make sure to clean up after each test step.
    117 function step(testPromise) {
    118  return testPromise.then(function () {
    119    caches.delete(name);
    120  });
    121 }
    122 
    123 step(testBasics())
    124  .then(function () {
    125    return step(testFragment());
    126  })
    127  .then(function () {
    128    return step(testInterleaved());
    129  })
    130  .then(function () {
    131    testDone();
    132  });