summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/fetch-event-handled.https.html
blob: 08b88ce3773d036cf1ea3a9f321f87b26d54fffd (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
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<title>Service Worker: FetchEvent.handled</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

let frame = null;
let worker = null;
const script = 'resources/fetch-event-handled-worker.js';
const scope = 'resources/simple.html';
const channel = new MessageChannel();

// Wait for a message from the service worker and removes the message handler.
function wait_for_message_from_worker() {
  return new Promise((resolve) => channel.port2.onmessage = (event) => resolve(event.data));
}

// 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;
  if (!worker)
      worker = registration.active;
  worker.postMessage({port:channel.port1}, [channel.port1]);
  await wait_for_state(t, worker, 'activated');
}, 'global setup');

promise_test(async (t) => {
  const promise = with_iframe(scope);
  const message = await wait_for_message_from_worker();
  frame = await promise;
  assert_equals(message, 'RESOLVED');
}, 'FetchEvent.handled should resolve when respondWith() is not called for a' +
    ' navigation request');

promise_test(async (t) => {
  frame.contentWindow.fetch('sample.txt?respondWith-not-called');
  const message = await wait_for_message_from_worker();
  assert_equals(message, 'RESOLVED');
}, 'FetchEvent.handled should resolve when respondWith() is not called for a' +
    ' sub-resource request');

promise_test(async (t) => {
  frame.contentWindow.fetch(
      'sample.txt?respondWith-not-called-and-event-canceled').catch((e) => {});
  const message = await wait_for_message_from_worker();
  assert_equals(message, 'REJECTED');
}, 'FetchEvent.handled should reject when respondWith() is not called and the' +
    ' event is canceled');

promise_test(async (t) => {
  frame.contentWindow.fetch(
      'sample.txt?respondWith-called-and-promise-resolved');
  const message = await wait_for_message_from_worker();
  assert_equals(message, 'RESOLVED');
}, 'FetchEvent.handled should resolve when the promise provided' +
    ' to respondWith() is resolved');

promise_test(async (t) => {
  frame.contentWindow.fetch(
      'sample.txt?respondWith-called-and-promise-resolved-to-invalid-response')
      .catch((e) => {});
  const message = await wait_for_message_from_worker();
  assert_equals(message, 'REJECTED');
}, 'FetchEvent.handled should reject when the promise provided' +
    ' to respondWith() is resolved to an invalid response');

promise_test(async (t) => {
  frame.contentWindow.fetch(
      'sample.txt?respondWith-called-and-promise-rejected').catch((e) => {});
  const message = await wait_for_message_from_worker();
  assert_equals(message, 'REJECTED');
}, 'FetchEvent.handled should reject when the promise provided to' +
    ' respondWith() is rejected');

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