tor-browser

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

test_cache.js (5910B)


      1 /* global context testDone:true */
      2 
      3 var c = null;
      4 var request = "https://example.com/hmm?q=foobar" + context;
      5 var response = new Response("This is some Response!");
      6 var name = "snafu" + context;
      7 var foobar = "foobar" + context;
      8 
      9 ok(!!caches, "caches object should be available on global");
     10 caches
     11  .open(name)
     12  .then(function (openCache) {
     13    ok(
     14      openCache instanceof Cache,
     15      "cache object should be resolved from caches.open"
     16    );
     17    return caches.has(name);
     18  })
     19  .then(function (hasResult) {
     20    ok(hasResult, "caches.has() should resolve true");
     21    return caches.keys();
     22  })
     23  .then(function (keys) {
     24    ok(!!keys, "caches.keys() should resolve to a truthy value");
     25    ok(
     26      keys.length >= 1,
     27      "caches.keys() should resolve to an array of length at least 1"
     28    );
     29    ok(
     30      keys.includes(name),
     31      "caches.keys() should resolve to an array containing key"
     32    );
     33    return caches.delete(name);
     34  })
     35  .then(function (deleteResult) {
     36    ok(deleteResult, "caches.delete() should resolve true");
     37    return caches.has(name);
     38  })
     39  .then(function (hasMissingCache) {
     40    ok(!hasMissingCache, "missing key should return false from has");
     41  })
     42  .then(function () {
     43    return caches.open(name);
     44  })
     45  .then(function (snafu) {
     46    return snafu.keys();
     47  })
     48  .then(function (empty) {
     49    is(0, empty.length, "cache.keys() should resolve to an array of length 0");
     50  })
     51  .then(function () {
     52    return caches.open(name);
     53  })
     54  .then(function (snafu) {
     55    var req = "./cachekey";
     56    var res = new Response("Hello world");
     57    return snafu
     58      .put("ftp://invalid", res)
     59      .then(function () {
     60        ok(false, "This should fail");
     61      })
     62      .catch(function (err) {
     63        is(
     64          err.name,
     65          "TypeError",
     66          "put() should throw TypeError for invalid scheme"
     67        );
     68        return snafu.put(req, res);
     69      })
     70      .then(function () {
     71        return snafu;
     72      });
     73  })
     74  .then(function (snafu) {
     75    return Promise.all([snafu, snafu.keys()]);
     76  })
     77  .then(function (args) {
     78    var snafu = args[0];
     79    var keys = args[1];
     80    is(1, keys.length, "cache.keys() should resolve to an array of length 1");
     81    ok(keys[0] instanceof Request, "key should be a Request");
     82    ok(keys[0].url.match(/cachekey$/), "Request URL should match original");
     83    return Promise.all([
     84      snafu,
     85      snafu.match(keys[0]),
     86      snafu.match("ftp://invalid"),
     87    ]);
     88  })
     89  .then(function (args) {
     90    var snafu = args[0];
     91    var res = args[1];
     92    ok(res instanceof Response, "value should be a Response");
     93    is(res.status, 200, "Response status should be 200");
     94    is(
     95      undefined,
     96      args[2],
     97      "Match with invalid scheme should resolve undefined"
     98    );
     99    return Promise.all([snafu, snafu.put("./cachekey2", res)]);
    100  })
    101  .then(function (args) {
    102    var snafu = args[0];
    103    return snafu.match("./cachekey2");
    104  })
    105  .then(function (res) {
    106    return res.text().then(function (v) {
    107      is(v, "Hello world", "Response body should match original");
    108    });
    109  })
    110  .then(function () {
    111    // FIXME(nsm): Can't use a Request object for now since the operations
    112    // consume it's 'body'. See
    113    // https://github.com/slightlyoff/ServiceWorker/issues/510.
    114    return caches.open(foobar);
    115  })
    116  .then(function (openCache) {
    117    c = openCache;
    118    return c.put(request, response);
    119  })
    120  .then(function (putResponse) {
    121    is(putResponse, undefined, "The promise should resolve to undefined");
    122    return c.keys(request);
    123  })
    124  .then(function (keys) {
    125    ok(keys, "Valid keys object expected");
    126    is(keys.length, 1, "Only one key is expected");
    127    return c.keys();
    128  })
    129  .then(function (keys) {
    130    ok(keys, "Valid keys object expected");
    131    is(keys.length, 1, "Only one key is expected");
    132    return c.matchAll(request);
    133  })
    134  .then(function (matchAllResponses) {
    135    ok(matchAllResponses, "matchAll should succeed");
    136    is(matchAllResponses.length, 1, "Only one match is expected");
    137    return c.match(request);
    138  })
    139  .then(function (matchResponse) {
    140    ok(matchResponse, "match should succeed");
    141    return caches.match(request);
    142  })
    143  .then(function (storageMatchResponse) {
    144    ok(storageMatchResponse, "storage match should succeed");
    145    return caches.match(request, { cacheName: foobar });
    146  })
    147  .then(function (storageMatchResponse) {
    148    ok(storageMatchResponse, "storage match with cacheName should succeed");
    149    var request2 = new Request("https://example.com/hmm?q=snafu" + context);
    150    return c.match(request2, { ignoreSearch: true });
    151  })
    152  .then(function (match2Response) {
    153    ok(match2Response, "match should succeed");
    154    return c.delete(request);
    155  })
    156  .then(function (deleteResult) {
    157    ok(deleteResult, "delete should succeed");
    158    return c.keys();
    159  })
    160  .then(function (keys) {
    161    ok(keys, "Valid keys object expected");
    162    is(keys.length, 0, "Zero keys is expected");
    163    return c.matchAll(request);
    164  })
    165  .then(function (matchAll2Responses) {
    166    ok(matchAll2Responses, "matchAll should succeed");
    167    is(matchAll2Responses.length, 0, "Zero matches is expected");
    168    return caches.has(foobar);
    169  })
    170  .then(function (hasResult) {
    171    ok(hasResult, "has should succeed");
    172    return caches.keys();
    173  })
    174  .then(function (keys) {
    175    ok(keys, "Valid keys object expected");
    176    ok(keys.length >= 2, "At least two keys are expected");
    177    ok(keys.includes(name), "snafu should exist");
    178    ok(
    179      keys.indexOf(foobar) >= keys.indexOf(name),
    180      "foobar should come after it"
    181    );
    182    return caches.delete(foobar);
    183  })
    184  .then(function (deleteResult) {
    185    ok(deleteResult, "delete should succeed");
    186    return caches.has(foobar);
    187  })
    188  .then(function (hasMissingCache) {
    189    ok(!hasMissingCache, "has should have a result");
    190    return caches.delete(name);
    191  })
    192  .then(function (deleteResult) {
    193    ok(deleteResult, "delete should succeed");
    194    testDone();
    195  });