summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js')
-rw-r--r--testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js b/testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js
new file mode 100644
index 0000000000..b768c815d0
--- /dev/null
+++ b/testing/web-platform/tests/workers/shared-worker-in-data-url-context.window.js
@@ -0,0 +1,65 @@
+// META: title=data URL shared worker in data URL context
+// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
+const mimeType = 'application/javascript';
+
+// Tests creating a data URL shared worker in a data URL iframe.
+promise_test(async t => {
+ const nestedWorkerScriptURL =
+ new URL('/workers/support/post-message-on-load-worker.js', location.href);
+
+ // This code will be executed in a data URL iframe. The iframe tries to create
+ // a shared worker from |nestedWorkerScriptURL|, but that should result in a
+ // failure. This is because the data URL iframe has an opaque origin, and
+ // script fetch is handled as a cross-origin request.
+ const frameCode = `
+ <script>
+ try {
+ const worker = new SharedWorker('${nestedWorkerScriptURL}');
+ worker.port.onmessage = e => {
+ window.parent.postMessage(
+ 'SharedWorker construction unexpectedly succeeded', '*');
+ };
+ worker.onerror = e => window.parent.postMessage('PASS', '*');
+ } catch (e) {
+ // Cross-origin request should asynchronously fail during worker script
+ // fetch because its request mode is 'same-origin'.
+ window.parent.postMessage(
+ 'SharedWorker construction unexpectedly synchronously failed', '*');
+ }
+ </script>
+ `;
+
+ const p = new Promise(r => window.onmessage = e => r(e.data));
+ const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
+ const result = await p;
+ assert_equals(result, 'PASS');
+}, 'Create a shared worker in a data url frame');
+
+// Tests creating a data URL shared worker in a data URL iframe.
+promise_test(async t => {
+ const workerCode = `onconnect = e => e.ports[0].postMessage("PASS");`;
+
+ // This code will be executed in a data URL iframe. The iframe tries to create
+ // a data URL shared worker. Fetching a data URL from the data URL shared
+ // worker is allowed, so the worker construction should succeed. The worker
+ // posts the result to the parent frame.
+ const frameCode = `
+ <script>
+ try {
+ const worker = new SharedWorker('data:${mimeType},${workerCode};');
+ worker.port.onmessage = e => window.parent.postMessage(e.data, '*');
+ worker.onerror = e => {
+ window.parent.postMessage('FAIL: ' + e.message, '*');
+ };
+ } catch (e) {
+ window.parent.postMessage(
+ 'SharedWorker construction unexpectedly synchronously failed', '*');
+ }
+ </script>
+ `;
+
+ const p = new Promise(r => window.onmessage = e => r(e.data));
+ const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
+ const result = await p;
+ assert_equals(result, 'PASS');
+}, 'Create a data url shared worker in a data url frame');