summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/fetch-event-async-respond-with.https.html
blob: d9147f85494c21e54d39a4fd7af16efa1a126384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<title>respondWith cannot be called asynchronously</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// This file has tests that call respondWith() asynchronously.

let frame;
let worker;
const script = 'resources/fetch-event-async-respond-with-worker.js';
const scope = 'resources/simple.html';

// Global setup: this must be the first promise_test.
promise_test(async (t) => {
  const registration =
      await service_worker_unregister_and_register(t, script, scope);
  worker = registration.installing;
  await wait_for_state(t, worker, 'activated');
  frame = await with_iframe(scope);
}, 'global setup');

// Waits for a single message from the service worker and then removes the
// message handler. Not safe for concurrent use.
function wait_for_message() {
  return new Promise((resolve) => {
    const handler = (event) => {
      navigator.serviceWorker.removeEventListener('message', handler);
      resolve(event.data);
    };
    navigator.serviceWorker.addEventListener('message', handler);
  });
}

// Does one test case. It fetches |url|. The service worker gets a fetch event
// for |url| and attempts to call respondWith() asynchronously. It reports back
// to the test whether an exception was thrown.
async function do_test(url) {
  // Send a message to tell the worker a new test case is starting.
  const message = wait_for_message();
  worker.postMessage('initializeMessageHandler');
  const response = await message;
  assert_equals(response, 'messageHandlerInitialized');

  // Start a fetch.
  const fetchPromise = frame.contentWindow.fetch(url);

  // Receive the test result from the service worker.
  const result = wait_for_message();
  await fetchPromise.then(()=> {}, () => {});
  return result;
};

promise_test(async (t) => {
  const result = await do_test('respondWith-in-task');
  assert_true(result.didThrow, 'should throw');
  assert_equals(result.error, 'InvalidStateError');
}, 'respondWith in a task throws InvalidStateError');

promise_test(async (t) => {
  const result = await do_test('respondWith-in-microtask');
  assert_equals(result.didThrow, false, 'should not throw');
}, 'respondWith in a microtask does not throw');

// Global cleanup: the final promise_test.
promise_test(async (t) => {
  if (frame)
    frame.remove();
  await service_worker_unregister(t, scope);
}, 'global cleanup');
</script>
</html>