tor-browser

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

test_cache_matchAll_request.js (7930B)


      1 /* global context testDone:true */
      2 
      3 var req1 = new Request("//mochi.test:8888/?1&" + context + "#fragment");
      4 var req2 = new Request("//mochi.test:8888/?2&" + context);
      5 var req3 = new Request("//mochi.test:8888/?3&" + context);
      6 var requestWithAltQS = new Request("//mochi.test:8888/?queryString");
      7 var unknownRequest = new Request(
      8  "//mochi.test:8888/non/existing/path?" + context
      9 );
     10 var response1, response3;
     11 var c;
     12 var response1Text, response3Text;
     13 var name = "matchAll-request" + context;
     14 
     15 function checkResponse(r, response, responseText) {
     16  ok(r !== response, "The objects should not be the same");
     17  is(
     18    r.url,
     19    response.url.replace("#fragment", ""),
     20    "The URLs should be the same"
     21  );
     22  is(r.status, response.status, "The status codes should be the same");
     23  is(r.type, response.type, "The response types should be the same");
     24  is(r.ok, response.ok, "Both responses should have succeeded");
     25  is(
     26    r.statusText,
     27    response.statusText,
     28    "Both responses should have the same status text"
     29  );
     30  return r.text().then(function (text) {
     31    // Avoid dumping out the large response text to the log if they're equal.
     32    if (text !== responseText) {
     33      is(text, responseText, "The response body should be correct");
     34    }
     35  });
     36 }
     37 
     38 fetch(new Request(req1))
     39  .then(function (r) {
     40    response1 = r;
     41    return response1.text();
     42  })
     43  .then(function (text) {
     44    response1Text = text;
     45    return fetch(new Request(req3));
     46  })
     47  .then(function (r) {
     48    response3 = r;
     49    return response3.text();
     50  })
     51  .then(function (text) {
     52    response3Text = text;
     53    return testRequest(
     54      req1,
     55      req2,
     56      req3,
     57      unknownRequest,
     58      requestWithAltQS,
     59      req1.url.replace("#fragment", "#other")
     60    );
     61  })
     62  .then(function () {
     63    return testRequest(
     64      req1.url,
     65      req2.url,
     66      req3.url,
     67      unknownRequest.url,
     68      requestWithAltQS.url,
     69      req1.url.replace("#fragment", "#other")
     70    );
     71  })
     72  .then(function () {
     73    testDone();
     74  });
     75 
     76 // The request arguments can either be a URL string, or a Request object.
     77 function testRequest(
     78  request1,
     79  request2,
     80  request3,
     81  unknownReq,
     82  requestWithAlternateQueryString,
     83  requestWithDifferentFragment
     84 ) {
     85  return caches
     86    .open(name)
     87    .then(function (cache) {
     88      c = cache;
     89      return c.add(request1);
     90    })
     91    .then(function () {
     92      return c.add(request3);
     93    })
     94    .then(function () {
     95      return Promise.all(
     96        ["HEAD", "POST", "PUT", "DELETE", "OPTIONS"].map(function (method) {
     97          var r = new Request(request1, { method });
     98          return c.add(r).then(
     99            function () {
    100              ok(false, "Promise should be rejected");
    101            },
    102            function (err) {
    103              is(
    104                err.name,
    105                "TypeError",
    106                "Adding a request with type '" + method + "' should fail"
    107              );
    108            }
    109          );
    110        })
    111      );
    112    })
    113    .then(function () {
    114      return c.matchAll(request1);
    115    })
    116    .then(function (r) {
    117      is(r.length, 1, "Should only find 1 item");
    118      return checkResponse(r[0], response1, response1Text);
    119    })
    120    .then(function () {
    121      return c.matchAll(new Request(request1, { method: "HEAD" }));
    122    })
    123    .then(function (r) {
    124      is(r.length, 0, "Should be an empty array when match a HEAD request.");
    125      return c.matchAll(new Request(request1, { method: "HEAD" }), {
    126        ignoreMethod: true,
    127      });
    128    })
    129    .then(function (r) {
    130      is(r.length, 1, "Should only find 1 item");
    131      return checkResponse(r[0], response1, response1Text);
    132    })
    133    .then(function () {
    134      return Promise.all(
    135        ["HEAD", "POST", "PUT", "DELETE", "OPTIONS"].map(function (method) {
    136          var req = new Request(request1, { method });
    137          return c
    138            .matchAll(req)
    139            .then(function (r) {
    140              is(
    141                r.length,
    142                0,
    143                "Searching for a request with a non-GET method should not succeed"
    144              );
    145              return c.matchAll(req, { ignoreMethod: true });
    146            })
    147            .then(function (r) {
    148              is(r.length, 1, "Should only find 1 item");
    149              return checkResponse(r[0], response1, response1Text);
    150            });
    151        })
    152      );
    153    })
    154    .then(function () {
    155      return c.matchAll(requestWithDifferentFragment);
    156    })
    157    .then(function (r) {
    158      is(r.length, 1, "Should only find 1 item");
    159      return checkResponse(r[0], response1, response1Text);
    160    })
    161    .then(function () {
    162      return c.matchAll(requestWithAlternateQueryString, {
    163        ignoreSearch: true,
    164      });
    165    })
    166    .then(function (r) {
    167      is(r.length, 2, "Should find 2 items");
    168      return Promise.all([
    169        checkResponse(r[0], response1, response1Text),
    170        checkResponse(r[1], response3, response3Text),
    171      ]);
    172    })
    173    .then(function () {
    174      return c.matchAll(request3);
    175    })
    176    .then(function (r) {
    177      is(r.length, 1, "Should only find 1 item");
    178      return checkResponse(r[0], response3, response3Text);
    179    })
    180    .then(function () {
    181      return c.matchAll();
    182    })
    183    .then(function (r) {
    184      is(r.length, 2, "Should find 2 items");
    185      return Promise.all([
    186        checkResponse(r[0], response1, response1Text),
    187        checkResponse(r[1], response3, response3Text),
    188      ]);
    189    })
    190    .then(function () {
    191      return caches
    192        .match(request1, { cacheName: name + "mambojambo" })
    193        .then(function () {
    194          is(
    195            typeof r,
    196            "undefined",
    197            "Searching in the wrong cache should resolve to undefined"
    198          );
    199          return caches.has(name + "mambojambo");
    200        })
    201        .then(function (hasCache) {
    202          ok(!hasCache, "The wrong cache should still not exist");
    203        });
    204    })
    205    .then(function () {
    206      return c.matchAll(unknownReq);
    207    })
    208    .then(function (r) {
    209      is(r.length, 0, "Searching for an unknown request should not succeed");
    210      return caches.match(unknownReq, { cacheName: name });
    211    })
    212    .then(function (r) {
    213      is(
    214        typeof r,
    215        "undefined",
    216        "Searching for an unknown request should not succeed"
    217      );
    218      // Make sure that cacheName is ignored on Cache
    219      return c.matchAll(request1, { cacheName: name + "mambojambo" });
    220    })
    221    .then(function (r) {
    222      is(r.length, 1, "Should only find 1 item");
    223      return checkResponse(r[0], response1, response1Text);
    224    })
    225    .then(function () {
    226      return caches.delete(name);
    227    })
    228    .then(function (success) {
    229      ok(success, "We should be able to delete the cache successfully");
    230      // Make sure that the cache is still usable after deletion.
    231      return c.matchAll(request1);
    232    })
    233    .then(function (r) {
    234      is(r.length, 1, "Should only find 1 item");
    235      return checkResponse(r[0], response1, response1Text);
    236    })
    237    .then(function () {
    238      return c.matchAll(request3);
    239    })
    240    .then(function (r) {
    241      is(r.length, 1, "Should only find 1 item");
    242      return checkResponse(r[0], response3, response3Text);
    243    })
    244    .then(function () {
    245      return c.matchAll();
    246    })
    247    .then(function (r) {
    248      is(r.length, 2, "Should find 2 items");
    249      return Promise.all([
    250        checkResponse(r[0], response1, response1Text),
    251        checkResponse(r[1], response3, response3Text),
    252      ]);
    253    })
    254    .then(function () {
    255      // Now, drop the cache, reopen and verify that we can't find the request any more.
    256      c = null;
    257      return caches.open(name);
    258    })
    259    .then(function (cache) {
    260      return cache.matchAll();
    261    })
    262    .then(function (r) {
    263      is(
    264        r.length,
    265        0,
    266        "Searching in the cache after deletion should not succeed"
    267      );
    268      return caches.delete(name);
    269    })
    270    .then(function (deleted) {
    271      ok(deleted, "The cache should be deleted successfully");
    272    });
    273 }