tor-browser

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

cache-abort.https.any.js (3102B)


      1 // META: title=Cache Storage: Abort
      2 // META: global=window,worker
      3 // META: script=./resources/test-helpers.js
      4 // META: script=/common/utils.js
      5 // META: timeout=long
      6 
      7 // We perform the same tests on put, add, addAll. Parameterise the tests to
      8 // reduce repetition.
      9 const methodsToTest = {
     10  put: async (cache, request) => {
     11    const response = await fetch(request);
     12    return cache.put(request, response);
     13  },
     14  add: async (cache, request) => cache.add(request),
     15  addAll: async (cache, request) => cache.addAll([request]),
     16 };
     17 
     18 for (const method in methodsToTest) {
     19  const perform = methodsToTest[method];
     20 
     21  cache_test(async (cache, test) => {
     22    const controller = new AbortController();
     23    const signal = controller.signal;
     24    controller.abort();
     25    const request = new Request('../resources/simple.txt', { signal });
     26    return promise_rejects_dom(test, 'AbortError', perform(cache, request),
     27                          `${method} should reject`);
     28  }, `${method}() on an already-aborted request should reject with AbortError`);
     29 
     30  cache_test(async (cache, test) => {
     31    const controller = new AbortController();
     32    const signal = controller.signal;
     33    const request = new Request('../resources/simple.txt', { signal });
     34    const promise = perform(cache, request);
     35    controller.abort();
     36    return promise_rejects_dom(test, 'AbortError', promise,
     37                          `${method} should reject`);
     38  }, `${method}() synchronously followed by abort should reject with ` +
     39     `AbortError`);
     40 
     41  cache_test(async (cache, test) => {
     42    const controller = new AbortController();
     43    const signal = controller.signal;
     44    const stateKey = token();
     45    const abortKey = token();
     46    const request = new Request(
     47        `../../../fetch/api/resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`,
     48        { signal });
     49 
     50    const promise = perform(cache, request);
     51 
     52    // Wait for the server to start sending the response body.
     53    let opened = false;
     54    do {
     55      // Normally only one fetch to 'stash-take' is needed, but the fetches
     56      // will be served in reverse order sometimes
     57      // (i.e., 'stash-take' gets served before 'infinite-slow-response').
     58 
     59      const response =
     60            await fetch(`../../../fetch/api/resources/stash-take.py?key=${stateKey}`);
     61      const body = await response.json();
     62      if (body === 'open') opened = true;
     63    } while (!opened);
     64 
     65    // Sadly the above loop cannot guarantee that the browser has started
     66    // processing the response body. This delay is needed to make the test
     67    // failures non-flaky in Chrome version 66. My deepest apologies.
     68    await new Promise(resolve => setTimeout(resolve, 250));
     69 
     70    controller.abort();
     71 
     72    await promise_rejects_dom(test, 'AbortError', promise,
     73                          `${method} should reject`);
     74 
     75    // infinite-slow-response.py doesn't know when to stop.
     76    return fetch(`../../../fetch/api/resources/stash-put.py?key=${abortKey}&value=close`);
     77  }, `${method}() followed by abort after headers received should reject ` +
     78     `with AbortError`);
     79 }
     80 
     81 done();