Commit ea88a8bd authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT] Add service worker cross-origin and redirect import tests

This CL adds WPT tests for:

- Cross-origin importScripts() from classic service workers
- Cross-origin static imports from module workers
- Redirecting static imports from module workers
    - Failing on service workers on Chromium, while
      redirecting importScripts() also fails on Chromium
      due to https://crbug.com/889798.

Bug: 1136767, 889798
Change-Id: I318275c7772ab2854c972774f5ba49ca22f0018a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2463046
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827534}
parent 158b87d2
This is a testharness.js-based test.
PASS Static import.
PASS Static import (cross-origin).
FAIL Static import (redirect). promise_test: Unhandled rejection with value: object "AbortError: Failed to register a ServiceWorker for scope ('https://web-platform.test:8444/workers/modules/resources/static-import-redirect-worker.js') with script ('https://web-platform.test:8444/workers/modules/resources/static-import-redirect-worker.js'): ServiceWorker cannot be started"
PASS Nested static import.
FAIL Static import and then dynamic import. assert_array_equals: value is "Failed to do dynamic import: TypeError: Module scripts are not supported on ServiceWorkerGlobalScope yet (see https://crbug.com/824647).", expected array
FAIL Dynamic import. assert_array_equals: value is "Failed to do dynamic import: TypeError: Module scripts are not supported on ServiceWorkerGlobalScope yet (see https://crbug.com/824647).", expected array
......
<!DOCTYPE html>
<meta charset="utf-8">
<title>Tests for importScripts: cross-origin</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
promise_test(async t => {
const scope = 'resources/import-scripts-cross-origin';
await service_worker_unregister(t, scope);
let reg = await navigator.serviceWorker.register(
'resources/import-scripts-cross-origin-worker.sub.js', { scope: scope });
t.add_cleanup(_ => reg.unregister());
assert_not_equals(reg.installing, null, 'worker is installing');
}, 'importScripts() supports cross-origin requests');
</script>
</body>
importScripts('https://{{domains[www1]}}:{{ports[https][0]}}/service-workers/service-worker/resources/import-scripts-version.py');
def main(request, response):
# This script serves both preflight and main GET request for cross-origin
# static imports from module service workers.
# According to https://w3c.github.io/ServiceWorker/#update-algorithm,
# `Service-Worker: script` request header is added, which triggers CORS
# preflight.
response_headers = [(b"Content-Type", b"text/javascript"),
(b"Access-Control-Allow-Origin", b"*"),
(b"Access-Control-Allow-Headers", b"Service-Worker")]
return (200, response_headers,
b"export const importedModules = ['export-on-load-script.js'];")
......@@ -4,6 +4,16 @@ const testCases = [
expectation: ['export-on-load-script.js'],
description: 'Static import.'
},
{
scriptURL: '/workers/modules/resources/static-import-remote-origin-script-worker.sub.js',
expectation: ['export-on-load-script.js'],
description: 'Static import (cross-origin).'
},
{
scriptURL: '/workers/modules/resources/static-import-redirect-worker.js',
expectation: ['export-on-load-script.js'],
description: 'Static import (redirect).'
},
{
scriptURL: '/workers/modules/resources/nested-static-import-worker.js',
expectation: [
......
......@@ -8,6 +8,7 @@ let worker;
// Creates a new dedicated worker for a given script url.
window.onmessage = e => {
worker = new Worker(e.data, { type: 'module' });
worker.postMessage('start');
worker.onmessage = msg => window.opener.postMessage(msg.data, '*');
worker.onerror = err => {
window.opener.postMessage(['ERROR'], '*');
......
def main(request, response):
"""Simple handler that causes redirection.
This is placed here to stay within the same directory during redirects,
to avoid issues like https://crbug.com/1136775.
"""
response.status = 302
location = request.GET.first(b"location")
response.headers.set(b"Location", location)
// This script is meant to be imported by a module worker. It receives a
// message from the worker and responds with the list of imported modules.
import * as module from './redirect.py?location=/workers/modules/resources/export-on-load-script.js';
if ('DedicatedWorkerGlobalScope' in self &&
self instanceof DedicatedWorkerGlobalScope) {
self.onmessage = e => {
e.target.postMessage(module.importedModules);
};
} else if (
'SharedWorkerGlobalScope' in self &&
self instanceof SharedWorkerGlobalScope) {
self.onconnect = e => {
e.ports[0].postMessage(module.importedModules);
};
} else if (
'ServiceWorkerGlobalScope' in self &&
self instanceof ServiceWorkerGlobalScope) {
self.onmessage = e => {
e.source.postMessage(module.importedModules);
};
}
// Import a remote origin script.
import * as module from 'https://{{domains[www1]}}:{{ports[https][0]}}/workers/modules/resources/export-on-load-script.js';
import * as module from 'https://{{domains[www1]}}:{{ports[https][0]}}/workers/modules/resources/export-on-load-script.py';
if ('DedicatedWorkerGlobalScope' in self &&
self instanceof DedicatedWorkerGlobalScope) {
postMessage(module.importedModules);
self.onmessage = e => {
e.target.postMessage(module.importedModules);
};
} else if (
'SharedWorkerGlobalScope' in self &&
self instanceof SharedWorkerGlobalScope) {
onconnect = e => {
self.onconnect = e => {
e.ports[0].postMessage(module.importedModules);
};
} else if (
'ServiceWorkerGlobalScope' in self &&
self instanceof ServiceWorkerGlobalScope) {
self.onmessage = e => {
e.source.postMessage(module.importedModules);
};
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment