Commit 875f34ff authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

[WPT] Reland with fix: Test script's base URL used in dynamic imports

This CL checks base URL used in resolving relative URL-like
specifiers in dynamic imports() from

- `<script>`s,
- worker top-level scripts, and
- worker scripts imported by `importScripts()`.

This is for https://github.com/whatwg/html/issues/5751.

This CL relands
https://chromium-review.googlesource.com/c/chromium/src/+/2198835/
with a fix:

`base-url-worker.sub.html` was flaky due to
race between two `fetch_tests_from_worker()`s.
In this reland, a `fetch_tests_from_worker()` is
moved to a new file `base-url-worker-importScripts.html`,
having at most one `fetch_tests_from_worker()` per file.

Bug: 1082086, 1112844
Change-Id: I59c9817a7829e950f40878d376ccb5216207b7e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2337194Reviewed-by: default avatarDomenic Denicola <domenic@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794865}
parent ff45b180
This is a testharness.js-based test.
FAIL Relative URL-like from same-origin importScripts() assert_equals: expected "gamma/import.js" but got "beta/import.js"
PASS Absolute URL-like from same-origin importScripts()
FAIL Relative URL-like from cross-origin importScripts() assert_equals: Relative URL-like specifier resolution should fail expected "(unreached)" but got "beta/import.js"
PASS Absolute URL-like from cross-origin importScripts()
Harness: the test ran to completion.
<!DOCTYPE html>
<title>Base URLs used in resolving specifiers in dynamic imports from importScripts()</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
fetch_tests_from_worker(new Worker("./worker-importScripts.sub.js"));
</script>
<!DOCTYPE html>
<title>Base URLs used in resolving specifiers in dynamic imports from workers</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
fetch_tests_from_worker(new Worker(
"../beta/redirect.py?location=http://{{host}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js"));
</script>
This is a testharness.js-based test.
FAIL Relative URL-like from same origin classic <script> assert_equals: expected "gamma/import.js" but got "beta/import.js"
PASS Absolute URL-like from same origin classic <script>
FAIL Relative URL-like from cross origin classic <script> without crossorigin attribute assert_equals: Relative URL-like specifier resolution should fail expected "(unreached)" but got "beta/import.js"
PASS Absolute URL-like from cross origin classic <script> without crossorigin attribute
FAIL Relative URL-like from cross origin classic <script> with crossorigin attribute assert_equals: expected "gamma/import.js" but got "beta/import.js"
PASS Absolute URL-like from cross origin classic <script> with crossorigin attribute
PASS Relative URL-like from cross origin module <script>
PASS Absolute URL-like from cross origin module <script>
Harness: the test ran to completion.
<!DOCTYPE html>
<title>Base URLs used in resolving specifiers in dynamic imports</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
self.testName = "same origin classic <script>";
self.baseUrlSanitized = false;
</script>
<script src="../beta/redirect.py?location=http://{{host}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js"></script>
<script>
self.testName = "cross origin classic <script> without crossorigin attribute";
self.baseUrlSanitized = true;
</script>
<script src="../beta/redirect.py?location=http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js"></script>
<script>
self.testName = "cross origin classic <script> with crossorigin attribute";
self.baseUrlSanitized = false;
</script>
<script src="../beta/redirect.py?location=http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js%3Fpipe=header(Access-Control-Allow-Origin,*)" crossorigin></script>
<script>
self.testName = "cross origin module <script>";
self.baseUrlSanitized = false;
</script>
<script src="../beta/redirect.py?location=http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js%3Fpipe=header(Access-Control-Allow-Origin,*)" type="module"></script>
"use strict";
importScripts("/resources/testharness.js");
// CORS-same-origin
self.testName = "same-origin importScripts()";
self.baseUrlSanitized = false;
importScripts("../beta/redirect.py?location=http://{{host}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js");
// CORS-cross-origin
self.testName = "cross-origin importScripts()";
self.baseUrlSanitized = true;
importScripts("../beta/redirect.py?location=http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/base-url.sub.js");
done();
def main(request, response):
"""Simple handler that causes redirection.
The request should typically have two query parameters:
status - The status to use for the redirection. Defaults to 302.
location - The resource to redirect to.
"""
status = 302
if b"status" in request.GET:
try:
status = int(request.GET.first(b"status"))
except ValueError:
pass
response.status = status
location = request.GET.first(b"location")
response.headers.set(b"Location", location)
"use strict";
// This script triggers import(), and thus the base URL of this script
// (either loaded by `<script>` or `importScripts()`) is used as the base URL
// of resolving relative URL-like specifiers in `import()`.
// The following fields should be set by the callers of this script
// (unless loaded as the worker top-level script):
// - self.testName (string)
// - self.baseUrlSanitized (boolean)
// When this script is loaded as the worker top-level script:
if ('DedicatedWorkerGlobalScope' in self &&
self instanceof DedicatedWorkerGlobalScope &&
!self.testName) {
importScripts("/resources/testharness.js");
self.testName = 'worker top-level script';
// Worker top-level scripts are always same-origin.
self.baseUrlSanitized = false;
}
{
// This could change by the time the test is executed, so we save it now.
// As this script is loaded multiple times, savedBaseUrlSanitized is scoped.
const savedBaseUrlSanitized = self.baseUrlSanitized;
promise_test(() => {
const promise = import("./import.js?pipe=header(Access-Control-Allow-Origin,*)&label=relative-" + self.testName);
if (savedBaseUrlSanitized) {
// The base URL is "about:blank" and thus import() here should fail.
return promise.then(module => {
// This code should be unreached, but assert_equals() is used here
// to log `module.A["from"]` in case of unexpected resolution.
assert_equals(module.A["from"], "(unreached)",
"Relative URL-like specifier resolution should fail");
assert_unreached();
},
() => {});
} else {
// The base URL is the response URL of this script, i.e.
// `.../gamma/base-url.sub.js`.
return promise.then(module => {
assert_equals(module.A["from"], "gamma/import.js");
});
}
},
"Relative URL-like from " + self.testName);
}
promise_test(() => {
return import("http://{{hosts[alt][]}}:{{ports[http][0]}}/html/semantics/scripting-1/the-script-element/module/dynamic-import/gamma/import.js?pipe=header(Access-Control-Allow-Origin,*)&label=absolute-" + self.testName)
.then(module => {
assert_equals(module.A["from"], "gamma/import.js");
})
},
"Absolute URL-like from " + self.testName);
done();
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