tor-browser

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

test_caches.js (4821B)


      1 /* global context testDone:true */
      2 
      3 function arraysHaveSameContent(arr1, arr2) {
      4  if (arr1.length != arr2.length) {
      5    return false;
      6  }
      7  return arr1.every(function (value, index) {
      8    return arr2[index] === value;
      9  });
     10 }
     11 
     12 function testHas() {
     13  var name = "caches-has" + context;
     14  return caches
     15    .has(name)
     16    .then(function (has) {
     17      ok(!has, name + " should not exist yet");
     18      return caches.open(name);
     19    })
     20    .then(function () {
     21      return caches.has(name);
     22    })
     23    .then(function (has) {
     24      ok(has, name + " should now exist");
     25      return caches.delete(name);
     26    })
     27    .then(function (deleted) {
     28      ok(deleted, "The deletion should finish successfully");
     29      return caches.has(name);
     30    })
     31    .then(function (has) {
     32      ok(!has, name + " should not exist any more");
     33    });
     34 }
     35 
     36 function testKeys() {
     37  var names = [
     38    // The names here are intentionally unsorted, to ensure the insertion order
     39    // and make sure we don't confuse it with an alphabetically sorted list.
     40    "caches-keys4" + context,
     41    "caches-keys0" + context,
     42    "caches-keys1" + context,
     43    "caches-keys3" + context,
     44  ];
     45  return caches
     46    .keys()
     47    .then(function (keys) {
     48      is(keys.length, 0, "No keys should exist yet");
     49      return Promise.all(
     50        names.map(function (name) {
     51          return caches.open(name);
     52        })
     53      );
     54    })
     55    .then(function () {
     56      return caches.keys();
     57    })
     58    .then(function (keys) {
     59      ok(
     60        arraysHaveSameContent(keys, names),
     61        "Keys must match in insertion order"
     62      );
     63      return Promise.all(
     64        names.map(function (name) {
     65          return caches.delete(name);
     66        })
     67      );
     68    })
     69    .then(function (deleted) {
     70      ok(
     71        arraysHaveSameContent(deleted, [true, true, true, true]),
     72        "All deletions must succeed"
     73      );
     74      return caches.keys();
     75    })
     76    .then(function (keys) {
     77      is(keys.length, 0, "No keys should exist any more");
     78    });
     79 }
     80 
     81 function testMatchAcrossCaches() {
     82  var tests = [
     83    // The names here are intentionally unsorted, to ensure the insertion order
     84    // and make sure we don't confuse it with an alphabetically sorted list.
     85    {
     86      name: "caches-xmatch5" + context,
     87      request: "//mochi.test:8888/?5" + context,
     88    },
     89    {
     90      name: "caches-xmatch2" + context,
     91      request:
     92        "//mochi.test:8888/tests/dom/cache/test/mochitest/test_caches.js?2" +
     93        context,
     94    },
     95    {
     96      name: "caches-xmatch4" + context,
     97      request: "//mochi.test:8888/?4" + context,
     98    },
     99  ];
    100  return Promise.all(
    101    tests.map(function (test) {
    102      return caches.open(test.name).then(function (c) {
    103        return c.add(test.request);
    104      });
    105    })
    106  )
    107    .then(function () {
    108      return caches.match("//mochi.test:8888/?5" + context, {
    109        ignoreSearch: true,
    110      });
    111    })
    112    .then(function (match) {
    113      ok(match.url.indexOf("?5") > 0, "Match should come from the first cache");
    114      return caches.delete("caches-xmatch2" + context); // This should not change anything!
    115    })
    116    .then(function (deleted) {
    117      ok(deleted, "Deletion should finish successfully");
    118      return caches.match("//mochi.test:8888/?" + context, {
    119        ignoreSearch: true,
    120      });
    121    })
    122    .then(function (match) {
    123      ok(
    124        match.url.indexOf("?5") > 0,
    125        "Match should still come from the first cache"
    126      );
    127      return caches.delete("caches-xmatch5" + context); // This should eliminate the first match!
    128    })
    129    .then(function (deleted) {
    130      ok(deleted, "Deletion should finish successfully");
    131      return caches.match("//mochi.test:8888/?" + context, {
    132        ignoreSearch: true,
    133      });
    134    })
    135    .then(function (match) {
    136      ok(match.url.indexOf("?4") > 0, "Match should come from the third cache");
    137      return caches.delete("caches-xmatch4" + context); // Game over!
    138    })
    139    .then(function (deleted) {
    140      ok(deleted, "Deletion should finish successfully");
    141      return caches.match("//mochi.test:8888/?" + context, {
    142        ignoreSearch: true,
    143      });
    144    })
    145    .then(function (match) {
    146      is(typeof match, "undefined", "No matches should be found");
    147    });
    148 }
    149 
    150 function testDelete() {
    151  return caches
    152    .delete("delete" + context)
    153    .then(function (deleted) {
    154      ok(!deleted, "Attempting to delete a non-existing cache should fail");
    155      return caches.open("delete" + context);
    156    })
    157    .then(function () {
    158      return caches.delete("delete" + context);
    159    })
    160    .then(function (deleted) {
    161      ok(deleted, "Delete should now succeed");
    162    });
    163 }
    164 
    165 testHas()
    166  .then(function () {
    167    return testKeys();
    168  })
    169  .then(function () {
    170    return testMatchAcrossCaches();
    171  })
    172  .then(function () {
    173    return testDelete();
    174  })
    175  .then(function () {
    176    testDone();
    177  });