diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/infrastructure/assumptions/non-local-ports.sub.window.js | |
parent | Initial commit. (diff) | |
download | firefox-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/infrastructure/assumptions/non-local-ports.sub.window.js')
-rw-r--r-- | testing/web-platform/tests/infrastructure/assumptions/non-local-ports.sub.window.js | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/testing/web-platform/tests/infrastructure/assumptions/non-local-ports.sub.window.js b/testing/web-platform/tests/infrastructure/assumptions/non-local-ports.sub.window.js new file mode 100644 index 0000000000..06ecb3ab21 --- /dev/null +++ b/testing/web-platform/tests/infrastructure/assumptions/non-local-ports.sub.window.js @@ -0,0 +1,111 @@ +// Verifies that non-local HTTP(S) ports are open and serve correctly. +// +// See the corresponding WPT RFC: +// https://github.com/web-platform-tests/rfcs/blob/master/rfcs/address_space_overrides.md +// +// These ports are used to test the Local Network Access specification: +// https://wicg.github.io/local-network-access/ +// +// More tests can be found in `fetch/local-network-access/`. + +const alternatePorts = { + httpPrivate: "{{ports[http-private][0]}}", + httpsPrivate: "{{ports[https-private][0]}}", + httpPublic: "{{ports[http-public][0]}}", + httpsPublic: "{{ports[https-public][0]}}", +}; + +// Resolves a URL relative to the current location, returning an absolute URL. +// +// `url` specifies the relative URL, e.g. "foo.html" or "http://foo.example". +// `options.protocol` and `options.port`, if defined, override the respective +// properties of the returned URL object. +function resolveUrl(url, options) { + const result = new URL(url, window.location); + if (options === undefined) { + return result; + } + + const { port, protocol } = options; + if (port !== undefined) { + result.port = port; + } + if (protocol !== undefined) { + result.protocol = protocol; + } + + return result; +} + +const alternateOrigins = { + httpPrivate: { + protocol: "http:", + port: alternatePorts.httpPrivate, + }, + httpsPrivate: { + protocol: "https:", + port: alternatePorts.httpsPrivate, + }, + httpPublic: { + protocol: "http:", + port: alternatePorts.httpPublic, + }, + httpsPublic: { + protocol: "https:", + port: alternatePorts.httpsPublic, + }, +}; + +promise_test(async () => { + const url = + resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsPrivate); + const response = await fetch(url); + assert_true(response.ok); +}, "Fetch from https-private port works."); + +promise_test(async () => { + const url = + resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpPrivate); + const response = await fetch(url); + assert_true(response.ok); +}, "Fetch from http-private port works."); + +promise_test(async () => { + const url = + resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsPublic); + const response = await fetch(url); + assert_true(response.ok); +}, "Fetch from https-public port works."); + +promise_test(async () => { + const url = + resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpPublic); + const response = await fetch(url); + assert_true(response.ok); +}, "Fetch from http-public port works."); + +promise_test(async (t) => { + const futureMessage = new Promise((resolve) => { + window.addEventListener("message", resolve); + }); + + const iframe = await new Promise((resolve, reject) => { + const iframe = document.createElement("iframe"); + iframe.src = resolveUrl("resources/fetch-and-post-result.html", + alternateOrigins.httpPublic); + + iframe.onload = () => { resolve(iframe); }; + iframe.onerror = reject; + + document.body.appendChild(iframe); + t.add_cleanup(() => { + document.body.removeChild(iframe); + }); + }); + + iframe.contentWindow.postMessage( + resolveUrl("/common/blank-with-cors.html").toString(), "*"); + + const evt = await futureMessage; + assert_equals(evt.data, "failure: error = TypeError"); +}, "Fetch from http-public to local http fails."); |