require-corp-sw.https.html (2200B)
1 <!doctype html> 2 <title>Cross Origin Embedder Policy: requests initiated from a service worker with 'require-corp'</title> 3 <script src=/resources/testharness.js></script> 4 <script src=/resources/testharnessreport.js></script> 5 <script src="/common/get-host-info.sub.js"></script> 6 <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> 7 <script> 8 'use strict'; 9 10 const SCRIPT = 'resources/require-corp-sw.js'; 11 const SCOPE = 'resources/in-scope'; 12 let worker = null; 13 14 promise_test(async t => { 15 const registration = 16 await service_worker_unregister_and_register(t, SCRIPT, SCOPE); 17 promise_test(async t => registration.unregister(), 'Clean up global state'); 18 worker = registration.installing; 19 await wait_for_state(t, worker, 'activated'); 20 }, 'Set up global state'); 21 22 promise_test(async t => { 23 const p = new Promise(resolve => 24 navigator.serviceWorker.addEventListener('message', resolve, 25 {once: true})); 26 worker.postMessage('WithCorp'); 27 assert_equals((await p).data, 'opaque'); 28 }, "fetch() to 'CORP: cross-origin' response should succeed."); 29 30 promise_test(async t => { 31 const p = new Promise(resolve => 32 navigator.serviceWorker.addEventListener('message', resolve, 33 {once: true})); 34 worker.postMessage('WithoutCorp'); 35 assert_equals((await p).data, 'Exception: TypeError'); 36 }, "fetch() to no CORP response should not succeed."); 37 38 promise_test(async t => { 39 const scope = `${SCOPE}-2`; 40 await service_worker_unregister(t, scope); 41 const promise = navigator.serviceWorker.register( 42 'resources/require-corp-sw-import-scripts.js', {scope}); 43 await promise_rejects_js(t, TypeError, promise, 'register() should fail.'); 44 }, 'importScripts() fails for a script with no corp.'); 45 46 promise_test(async t => { 47 const scope = `${SCOPE}-3`; 48 await service_worker_unregister(t, scope); 49 const registration = await navigator.serviceWorker.register( 50 'resources/require-corp-sw-import-scripts.js?corp=cross-origin', {scope}); 51 t.add_cleanup(() => registration.unregister()); 52 }, 'importScripts() succeeds for a script with corp: cross-origin.'); 53 </script>