tor-browser

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

fetch-event-async-respond-with.https.html (2510B)


      1 <!DOCTYPE html>
      2 <html>
      3 <title>respondWith cannot be called asynchronously</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="resources/test-helpers.sub.js"></script>
      7 <script>
      8 // This file has tests that call respondWith() asynchronously.
      9 
     10 let frame;
     11 let worker;
     12 const script = 'resources/fetch-event-async-respond-with-worker.js';
     13 const scope = 'resources/simple.html';
     14 
     15 // Global setup: this must be the first promise_test.
     16 promise_test(async (t) => {
     17  const registration =
     18      await service_worker_unregister_and_register(t, script, scope);
     19  worker = registration.installing;
     20  await wait_for_state(t, worker, 'activated');
     21  frame = await with_iframe(scope);
     22 }, 'global setup');
     23 
     24 // Waits for a single message from the service worker and then removes the
     25 // message handler. Not safe for concurrent use.
     26 function wait_for_message() {
     27  return new Promise((resolve) => {
     28    const handler = (event) => {
     29      navigator.serviceWorker.removeEventListener('message', handler);
     30      resolve(event.data);
     31    };
     32    navigator.serviceWorker.addEventListener('message', handler);
     33  });
     34 }
     35 
     36 // Does one test case. It fetches |url|. The service worker gets a fetch event
     37 // for |url| and attempts to call respondWith() asynchronously. It reports back
     38 // to the test whether an exception was thrown.
     39 async function do_test(url) {
     40  // Send a message to tell the worker a new test case is starting.
     41  const message = wait_for_message();
     42  worker.postMessage('initializeMessageHandler');
     43  const response = await message;
     44  assert_equals(response, 'messageHandlerInitialized');
     45 
     46  // Start a fetch.
     47  const fetchPromise = frame.contentWindow.fetch(url);
     48 
     49  // Receive the test result from the service worker.
     50  const result = wait_for_message();
     51  await fetchPromise.then(()=> {}, () => {});
     52  return result;
     53 };
     54 
     55 promise_test(async (t) => {
     56  const result = await do_test('respondWith-in-task');
     57  assert_true(result.didThrow, 'should throw');
     58  assert_equals(result.error, 'InvalidStateError');
     59 }, 'respondWith in a task throws InvalidStateError');
     60 
     61 promise_test(async (t) => {
     62  const result = await do_test('respondWith-in-microtask');
     63  assert_equals(result.didThrow, false, 'should not throw');
     64 }, 'respondWith in a microtask does not throw');
     65 
     66 // Global cleanup: the final promise_test.
     67 promise_test(async (t) => {
     68  if (frame)
     69    frame.remove();
     70  await service_worker_unregister(t, scope);
     71 }, 'global cleanup');
     72 </script>
     73 </html>