Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WPT] Test script's base URL used in dynamic imports #23560

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!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"));
fetch_tests_from_worker(new Worker(
"./worker-importScripts.sub.js"));
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!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>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const A = { "from": "alpha/import.js" };
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"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();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const A = { "from": "beta/import.js" };
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"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();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const A = { "from": "gamma/import.js" };