summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-bundle/subresource-loading/service-worker-controlled.https.tentative.html
blob: d5c2a06837163d3416a9c4f7fcd083b024b12fba (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<title>
  Web Bundle fetching and the inner resouirce fetching should skip service
  worker
</title>
<link
  rel="help"
  href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<body>
  <script>
    setup(() => {
      assert_true(HTMLScriptElement.supports("webbundle"));
    });

    async function registerServiceWorkerAndReturnActiveWorker(
      t,
      script,
      scope
    ) {
      const reg = await navigator.serviceWorker.register(script, {
        scope: scope,
      });
      t.add_cleanup(() => reg.unregister());
      if (reg.active) return reg.active;
      const worker = reg.installing || reg.waiting;
      await new Promise((resolve) => {
        worker.addEventListener("statechange", (event) => {
          if (event.target.state == "activated") resolve();
        });
      });
      return worker;
    }

    async function getRequestedUrls(worker) {
      return new Promise((resolve) => {
        navigator.serviceWorker.addEventListener(
          "message",
          (e) => {
            resolve(e.data);
          },
          { once: true }
        );
        worker.postMessage(null);
      });
    }

    promise_test(async (t) => {
      const iframe_path = "./resources/service-worker-controlled-iframe.html";
      const iframe_url = new URL(iframe_path, location).href;

      // Register a service worker.
      const worker = await registerServiceWorkerAndReturnActiveWorker(
        t,
        "./resources/service-worker-for-request-monitor.js",
        iframe_path
      );

      // Load an iframe which is controlled by the service worker.
      const iframe = await new Promise((resolve) => {
        const frame = document.createElement("iframe");
        t.add_cleanup(() => frame.remove());
        frame.src = iframe_url;
        frame.onload = () => {
          resolve(frame);
        };
        document.body.appendChild(frame);
      });
      // The iframe request should be intercepted by the service worker.
      assert_array_equals(await getRequestedUrls(worker), [iframe_url]);

      // Add a web bundle element in the service worker controlled iframe.
      const frame_id = "uuid-in-package:429fcc4e-0696-4bad-b099-ee9175f023ae";
      const script_id = "uuid-in-package:020111b3-437a-4c5c-ae07-adb6bbffb720";

      const element = createWebBundleElement(
        "../../resources/wbn/uuid-in-package.wbn",
        /*resources=*/ [frame_id, script_id]
      );

      const element_load_promise = new Promise((resolve) => {
        element.addEventListener("load", () => {
          resolve();
        });
      });
      iframe.contentDocument.body.appendChild(element);
      await element_load_promise;
      // The web bundle request should not be intercepted by the service worker.
      assert_array_equals(await getRequestedUrls(worker), []);

      // Add a uuid-in-package URL script element in the service worker
      // controlled iframe.
      const result_promise = new Promise((resolve) => {
        // window.report_result() method will be called by the injected script.
        iframe.contentWindow.report_result = resolve;
      });
      const script = iframe.contentDocument.createElement("script");
      script.src = script_id;
      iframe.contentDocument.body.appendChild(script);
      assert_equals(await result_promise, "OK");
      // The urn uuld URL script request should not be intercepted by the
      // service worker.
      assert_array_equals(await getRequestedUrls(worker), []);

      // Add a uuid-in-package URL iframe element in the service worker controlled
      // iframe.
      const inner_iframe = iframe.contentDocument.createElement("iframe");
      inner_iframe.src = frame_id;
      const load_promise = new Promise((resolve) => {
        inner_iframe.addEventListener("load", () => {
          resolve();
        });
      });
      iframe.contentDocument.body.appendChild(inner_iframe);
      await load_promise;
      // The urn uuld URL iframe request should not intercepted by the service
      // worker.
      assert_array_equals(await getRequestedUrls(worker), []);

      // Check if the uuid-in-package URL iframe element is loaded correctly.
      const message_promise = new Promise((resolve) => {
        window.addEventListener(
          "message",
          (e) => {
            resolve(e.data);
          },
          { once: true }
        );
      });
      // location.href is evaluated in the uuid-in-package URL iframe element.
      inner_iframe.contentWindow.postMessage("location.href", "*");
      assert_equals(await message_promise, frame_id);
    }, "Both Web Bundle request and Subresource fetch requests inside the Web " + "Bundle should skip the service worker.");
  </script>
</body>