Use NetworkLoadChecker for navigation loads
[WebKit-https.git] / LayoutTests / http / wpt / service-workers / header-filtering.https.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Service Worker Header Filtering</title>
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7  <script src="/common/get-host-info.sub.js"></script>
8 </head>
9 <body>
10 <script>
11 var scope = "resources";
12 var registration;
13 var frame;
14
15 var url1 = "/WebKit/service-workers/resources/response-full-of-headers.py";
16 var url2 = get_host_info().HTTPS_REMOTE_ORIGIN + url1;
17
18 function withFrame(url)
19 {
20     return new Promise((resolve) => {
21         const frame = document.createElement('iframe');
22         frame.src = url;
23         frame.onload = function() { resolve(frame); };
24         document.body.appendChild(frame);
25     });
26 }
27
28 async function registerServiceWorker(scope)
29 {
30     const registration = await navigator.serviceWorker.register("header-filtering-worker.js", { scope : scope });
31     let activeWorker = registration.active;
32     if (activeWorker)
33         return registration;
34     activeWorker = registration.installing;
35     return new Promise(resolve => {
36         activeWorker.addEventListener('statechange', () => {
37             if (activeWorker.state === "activated")
38                 resolve(registration);
39         });
40     });
41 }
42
43 var processMessage;
44 promise_test(async (test) => {
45     registration = await registerServiceWorker(scope);
46     registration.active.postMessage("ready?");
47
48     navigator.serviceWorker.onmessage = (event) => {
49         processMessage(event.data);
50     };
51
52     return new Promise((resolve) => {
53         processMessage = (data) => {
54             assert_equals(event.data, "ready");
55             resolve();
56         }
57     });
58 }, "Prepare tests: setup worker and register the client");
59
60 promise_test(async (test) => {
61     const promise = new Promise((resolve) => {
62         processMessage = (data) => {
63             resolve(data);
64         };
65     });
66     frame = await withFrame(scope + "/header-filtering-iframe.html");
67     await promise;
68 }, "Prepare tests: Add a frame controlled by service worker");
69
70 promise_test(async (test) => {
71     const promise = new Promise((resolve) => {
72         processMessage = (data) => {
73             resolve(data);
74         };
75     });
76     frame.contentWindow.fetch(url1 + "?fetch");
77     assert_array_equals(await promise, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
78         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy","Server",
79         "SourceMap","Timing-Allow-Origin","X-SourceMap","x-header1","x-header2"]);
80 }, "Test same-origin fetch");
81
82 promise_test(async (test) => {
83     const data = new Promise((resolve) => {
84         processMessage = (data) => {
85             resolve(data);
86         };
87     });
88     frame.contentWindow.fetch(url2 + "?fetch-cors", { mode : "cors" });
89     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
90         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
91         "SourceMap","Timing-Allow-Origin","X-SourceMap","x-Header1"]);
92 }, "Test cors cross-origin fetch");
93
94 promise_test(async (test) => {
95     const data = new Promise((resolve) => {
96         processMessage = (data) => {
97             resolve(data);
98         };
99     });
100     frame.contentWindow.fetch(url2 + "?fetch-no-cors", { mode : "no-cors" });
101     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
102         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
103         "SourceMap","Timing-Allow-Origin","X-SourceMap"]);
104 }, "Test no-cors cross-origin fetch");
105
106 promise_test(async (test) => {
107     const data = new Promise((resolve) => {
108         processMessage = (data) => {
109             resolve(data);
110         };
111     });
112     frame.contentWindow.loadScript(url1 + "?script");
113     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
114         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
115         "Server","SourceMap","Timing-Allow-Origin","X-SourceMap","x-header1","x-header2"]);
116 }, "Test same-origin script load");
117
118 promise_test(async (test) => {
119     const data = new Promise((resolve) => {
120         processMessage = (data) => {
121             resolve(data);
122         };
123     });
124     frame.contentWindow.loadScript(url2 + "?script-nocors");
125     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
126         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
127         "SourceMap","Timing-Allow-Origin","X-SourceMap"]);
128 }, "Test no-cors script load");
129
130 promise_test(async (test) => {
131     const data = new Promise((resolve) => {
132         processMessage = (data) => {
133             resolve(data);
134         };
135     });
136     frame.contentWindow.loadScript(url2 + "?script-cors", "anonymous");
137     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
138         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
139         "SourceMap","Timing-Allow-Origin","X-SourceMap","x-Header1"]);
140 }, "Test cors script load");
141
142 promise_test(async (test) => {
143     const data = new Promise((resolve) => {
144         processMessage = (data) => {
145             resolve(data);
146         };
147     });
148     let frame = await withFrame(url1 + "?html");
149     assert_array_equals(await data, ["Access-Control-Allow-Credentials","Access-Control-Allow-Methods","Access-Control-Allow-Origin",
150         "Access-Control-Expose-Headers","Cache-Control","Content-Length","Content-Type","Date","Referrer-Policy",
151         "Server", "SourceMap","Timing-Allow-Origin","X-SourceMap","x-header1", "x-header2"]);
152     frame.remove();
153 }, "Test HTML load");
154
155 promise_test(async (test) => {
156     await registration.unregister();
157     frame.remove();
158 }, "After tests clean-up");
159
160 </script>
161 </body>
162 </html>