diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/signed-exchange/resources/sxg-util.js | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/signed-exchange/resources/sxg-util.js')
-rw-r--r-- | testing/web-platform/tests/signed-exchange/resources/sxg-util.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/testing/web-platform/tests/signed-exchange/resources/sxg-util.js b/testing/web-platform/tests/signed-exchange/resources/sxg-util.js new file mode 100644 index 0000000000..fa2d200fb2 --- /dev/null +++ b/testing/web-platform/tests/signed-exchange/resources/sxg-util.js @@ -0,0 +1,89 @@ +// Opens |url| in an iframe, establish a message channel with it, and waits for +// a message from the frame content. Returns a promise that resolves with the +// data of the message, or rejects on 15000ms timeout. +// If the iframe load is expected to fail, the test should have +// <meta name="timeout" content="long"> tag. +function openSXGInIframeAndWaitForMessage(test_object, url, referrerPolicy) { + return new Promise(async (resolve, reject) => { + // We can't catch the network error on iframe. So we use the timer. + test_object.step_timeout(() => reject('timeout'), 15000); + + const frame = await withIframe(url, 'sxg_iframe', referrerPolicy); + const channel = new MessageChannel(); + channel.port1.onmessage = (event) => resolve(event.data); + frame.contentWindow.postMessage( + {port: channel.port2}, '*', [channel.port2]); + }); +} + +function withIframe(url, name, referrerPolicy) { + return new Promise((resolve, reject) => { + const frame = document.createElement('iframe'); + frame.src = url; + frame.name = name; + if (referrerPolicy !== undefined) { + frame.referrerPolicy = referrerPolicy; + } + frame.onload = () => resolve(frame); + frame.onerror = () => reject('failed to load ' + url); + document.body.appendChild(frame); + }); +} + +function loadScript(url) { + return new Promise((resolve, reject) => { + const scriptTag = document.createElement('script'); + scriptTag.src = url; + scriptTag.onload = () => resolve(); + scriptTag.onerror = () => reject('failed to load ' + url); + document.head.appendChild(scriptTag); + }); +} + +function innerURLOrigin() { + return 'https://127.0.0.1:8444'; +} + +function runReferrerTests(test_cases) { + for (const i in test_cases) { + const test_case = test_cases[i]; + promise_test(async (t) => { + const sxgUrl = test_case.origin + '/signed-exchange/resources/sxg/' + + test_case.sxg; + const message = + await openSXGInIframeAndWaitForMessage( + t, sxgUrl, test_case.referrerPolicy); + assert_false(message.is_fallback); + assert_equals(message.referrer, test_case.expectedReferrer); + + const invalidSxgUrl = + test_case.origin + '/signed-exchange/resources/sxg/invalid-' + + test_case.sxg; + const fallbackMessage = + await openSXGInIframeAndWaitForMessage( + t, invalidSxgUrl, test_case.referrerPolicy); + assert_true(fallbackMessage.is_fallback); + assert_equals(fallbackMessage.referrer, test_case.expectedReferrer); + }, 'Referrer of SignedHTTPExchange test : ' + JSON.stringify(test_case)); + } +} + +function addPrefetch(url) { + const link = document.createElement('link'); + link.rel = 'prefetch'; + link.href = url; + document.body.appendChild(link); +} + +async function registerServiceWorkerAndWaitUntilActivated(script, scope) { + const reg = await navigator.serviceWorker.register(script, {scope: scope}); + if (reg.active) + return; + const worker = reg.installing || reg.waiting; + await new Promise((resolve) => { + worker.addEventListener('statechange', (event) => { + if (event.target.state == 'activated') + resolve(); + }); + }); +} |