test_abort_controller_fetch.html (1783B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <!DOCTYPE HTML> 6 <html> 7 <head> 8 <title>Test AbortController in Fetch API</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 11 </head> 12 <body> 13 <script class="testbody" type="text/javascript"> 14 15 function testAbortedFetch() { 16 var ac = new AbortController(); 17 ac.abort(); 18 19 fetch("slow.sjs", { signal: ac.signal }).then(() => { 20 ok(false, "Fetch should not return a resolved promise"); 21 }, e => { 22 is(e.name, "AbortError", "We have an abort error"); 23 }).then(next); 24 } 25 26 function testFetchAndAbort() { 27 var ac = new AbortController(); 28 29 var p = fetch("slow.sjs", { signal: ac.signal }); 30 ac.abort(); 31 32 p.then(() => { 33 ok(false, "Fetch should not return a resolved promise"); 34 }, e => { 35 is(e.name, "AbortError", "We have an abort error"); 36 }).then(next); 37 } 38 39 function testWorkerAbortedFetch() { 40 var w = new Worker("worker_abort_controller_fetch.js"); 41 w.onmessage = function(e) { 42 ok(e.data, "Abort + Fetch works in workers"); 43 next(); 44 }; 45 w.postMessage("testWorkerAbortedFetch"); 46 } 47 48 function testWorkerFetchAndAbort() { 49 var w = new Worker("worker_abort_controller_fetch.js"); 50 w.onmessage = function(e) { 51 ok(e.data, "Abort + Fetch works in workers"); 52 next(); 53 }; 54 w.postMessage("testWorkerFetchAndAbort"); 55 } 56 57 var steps = [ 58 // fetch + signaling 59 testAbortedFetch, 60 testFetchAndAbort, 61 testWorkerAbortedFetch, 62 testWorkerFetchAndAbort, 63 ]; 64 65 function next() { 66 if (!steps.length) { 67 SimpleTest.finish(); 68 return; 69 } 70 71 var step = steps.shift(); 72 step(); 73 } 74 75 SimpleTest.waitForExplicitFinish(); 76 next(); 77 78 </script> 79 </body> 80 </html>