fetch-event-handled.https.html (3183B)
1 <!DOCTYPE html> 2 <html> 3 <title>Service Worker: FetchEvent.handled</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script> 8 9 let frame = null; 10 let worker = null; 11 const script = 'resources/fetch-event-handled-worker.js'; 12 const scope = 'resources/simple.html'; 13 const channel = new MessageChannel(); 14 15 // Wait for a message from the service worker and removes the message handler. 16 function wait_for_message_from_worker() { 17 return new Promise((resolve) => channel.port2.onmessage = (event) => resolve(event.data)); 18 } 19 20 // Global setup: this must be the first promise_test. 21 promise_test(async (t) => { 22 const registration = 23 await service_worker_unregister_and_register(t, script, scope); 24 worker = registration.installing; 25 if (!worker) 26 worker = registration.active; 27 worker.postMessage({port:channel.port1}, [channel.port1]); 28 await wait_for_state(t, worker, 'activated'); 29 }, 'global setup'); 30 31 promise_test(async (t) => { 32 const promise = with_iframe(scope); 33 const message = await wait_for_message_from_worker(); 34 frame = await promise; 35 assert_equals(message, 'RESOLVED'); 36 }, 'FetchEvent.handled should resolve when respondWith() is not called for a' + 37 ' navigation request'); 38 39 promise_test(async (t) => { 40 frame.contentWindow.fetch('sample.txt?respondWith-not-called'); 41 const message = await wait_for_message_from_worker(); 42 assert_equals(message, 'RESOLVED'); 43 }, 'FetchEvent.handled should resolve when respondWith() is not called for a' + 44 ' sub-resource request'); 45 46 promise_test(async (t) => { 47 frame.contentWindow.fetch( 48 'sample.txt?respondWith-not-called-and-event-canceled').catch((e) => {}); 49 const message = await wait_for_message_from_worker(); 50 assert_equals(message, 'REJECTED'); 51 }, 'FetchEvent.handled should reject when respondWith() is not called and the' + 52 ' event is canceled'); 53 54 promise_test(async (t) => { 55 frame.contentWindow.fetch( 56 'sample.txt?respondWith-called-and-promise-resolved'); 57 const message = await wait_for_message_from_worker(); 58 assert_equals(message, 'RESOLVED'); 59 }, 'FetchEvent.handled should resolve when the promise provided' + 60 ' to respondWith() is resolved'); 61 62 promise_test(async (t) => { 63 frame.contentWindow.fetch( 64 'sample.txt?respondWith-called-and-promise-resolved-to-invalid-response') 65 .catch((e) => {}); 66 const message = await wait_for_message_from_worker(); 67 assert_equals(message, 'REJECTED'); 68 }, 'FetchEvent.handled should reject when the promise provided' + 69 ' to respondWith() is resolved to an invalid response'); 70 71 promise_test(async (t) => { 72 frame.contentWindow.fetch( 73 'sample.txt?respondWith-called-and-promise-rejected').catch((e) => {}); 74 const message = await wait_for_message_from_worker(); 75 assert_equals(message, 'REJECTED'); 76 }, 'FetchEvent.handled should reject when the promise provided to' + 77 ' respondWith() is rejected'); 78 79 // Global cleanup: the final promise_test. 80 promise_test(async (t) => { 81 if (frame) 82 frame.remove(); 83 await service_worker_unregister(t, scope); 84 }, 'global cleanup'); 85 </script> 86 </html>