tor-browser

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

abort.https.window.js (2608B)


      1 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js
      2 // META: script=resources/utils.js
      3 'use strict';
      4 
      5 // Covers basic functionality provided by BackgroundFetchManager.abort().
      6 // https://wicg.github.io/background-fetch/#background-fetch-registration-abort
      7 
      8 backgroundFetchTest(async (test, backgroundFetch) => {
      9  const registration = await backgroundFetch.fetch(
     10      uniqueId(),
     11      ['resources/feature-name.txt', '/common/slow.py']);
     12 
     13  assert_true(await registration.abort());
     14  assert_false(await registration.abort());
     15 
     16 }, 'Aborting the same registration twice fails');
     17 
     18 backgroundFetchTest(async (test, backgroundFetch) => {
     19  const registration = await backgroundFetch.fetch(
     20      uniqueId(),
     21      ['resources/feature-name.txt', '/common/slow.py']);
     22 
     23  await new Promise(resolve => {
     24    let aborted = false;
     25    const expectedResultText = 'Background Fetch';
     26 
     27    registration.onprogress = async event => {
     28      if (event.target.downloaded < expectedResultText.length)
     29        return;
     30 
     31      if (aborted)
     32        return;
     33 
     34      // Abort after the first file has been downloaded and check the results.
     35 
     36      aborted = true;
     37      assert_true(await registration.abort());
     38 
     39      const {type, eventRegistration, results} = await getMessageFromServiceWorker();
     40 
     41      assert_equals(eventRegistration.result, 'failure');
     42      assert_equals(eventRegistration.failureReason, 'aborted');
     43      assert_equals(registration.result, 'failure');
     44      assert_equals(registration.failureReason, 'aborted');
     45 
     46      assert_equals(type, 'backgroundfetchabort');
     47 
     48      assert_equals(results.length, 2);
     49 
     50      const completedResult = results[0] || results[1];
     51      // The abort might have gone through before the first result was persisted.
     52      if (completedResult) {
     53        assert_true(completedResult.url.includes('resources/feature-name.txt'));
     54        assert_equals(completedResult.status, 200);
     55        assert_equals(completedResult.text, expectedResultText);
     56      }
     57 
     58      resolve();
     59    };
     60  });
     61 
     62 }, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');
     63 
     64 backgroundFetchTest(async (test, backgroundFetch) => {
     65  const registration = await backgroundFetch.fetch(
     66      uniqueId(), '/common/slow.py');
     67  assert_true(await registration.abort());
     68 
     69  const {results} = await getMessageFromServiceWorker();
     70  assert_equals(results.length, 1);
     71  assert_false(results[0].response);
     72  assert_equals(results[0].name, 'AbortError');
     73 
     74 }, 'An aborted fetch throws a DOM exception when accessing an incomplete record', 'sw-abort.js');