keepalive.html (2936B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/common/utils.js"></script> 6 <script> 7 // This controller must be on the window so it is visible to the iframe. 8 window.sharedController = new AbortController(); 9 10 async function fetchJson(url) { 11 const response = await fetch(url); 12 assert_true(response.ok, 'response should be ok'); 13 return response.json(); 14 } 15 16 promise_test(async () => { 17 const stateKey = token(); 18 const controller = new AbortController(); 19 await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}`, 20 { 21 signal: controller.signal, 22 keepalive: true 23 }); 24 const before = await fetchJson(`../resources/stash-take.py?key=${stateKey}`); 25 assert_equals(before, 'open', 'connection should be open'); 26 27 controller.abort(); 28 29 // Spin until the abort completes. 30 while (true) { 31 const after = await fetchJson(`../resources/stash-take.py?key=${stateKey}`); 32 if (after) { 33 // stateKey='open' was removed from the dictionary by the first fetch of 34 // stash-take.py, so we should only ever see the value 'closed' here. 35 assert_equals(after, 'closed', 'connection should have closed'); 36 break; 37 } 38 } 39 }, 'aborting a keepalive fetch should work'); 40 41 promise_test(async t => { 42 const key = token(); 43 const iframeEl = document.querySelector('iframe'); 44 45 // Tell the iframe to start the fetch, and wait until it says it has. 46 await new Promise(resolve => { 47 onmessage = t.step_func(event => { 48 assert_equals(event.data, 'started', 'event data should be "started"'); 49 resolve(); 50 }); 51 iframeEl.contentWindow.postMessage(key, '*'); 52 }); 53 54 // Detach the context of the fetch. 55 iframeEl.remove(); 56 57 sharedController.abort(); 58 59 // The abort should not do anything. The connection should stay open. Wait 1 60 // second to give time for the fetch to complete. 61 await new Promise(resolve => t.step_timeout(resolve, 1000)); 62 63 const after = await fetchJson(`../resources/stash-take.py?key=${key}`); 64 assert_equals(after, 'on', 'fetch should have completed'); 65 }, 'aborting a detached keepalive fetch should not do anything'); 66 </script> 67 68 <iframe srcdoc=" 69 <!DOCTYPE html> 70 <meta charset=utf-8> 71 <script> 72 onmessage = async event => { 73 const key = event.data; 74 await fetch( 75 `../resources/redirect.py?delay=500&location=` + 76 `../resources/stash-put.py%3fkey=${key}%26value=on`, 77 { 78 signal: parent.sharedController.signal, 79 keepalive: true 80 }); 81 parent.postMessage('started', '*'); 82 }; 83 </script> 84 "> 85 </iframe>