tor-browser

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

test_cache_match_request.js (6468B)


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