eventsource_synthetic_response.html (2132B)
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>Bug 1182103 - Test EventSource scenarios with fetch interception</title> 9 <script type="text/javascript"> 10 11 var prefix = "http://mochi.test:8888/tests/dom/serviceworkers/test/eventsource/"; 12 13 function ok(aCondition, aMessage) { 14 parent.postMessage({status: "callback", data: "ok", condition: aCondition, message: aMessage}, "*"); 15 } 16 17 function doUnregister() { 18 navigator.serviceWorker.getRegistration().then(swr => { 19 swr.unregister().then(function(result) { 20 ok(result, "Unregister should return true."); 21 parent.postMessage({status: "callback", data: "done"}, "*"); 22 }, function(e) { 23 ok(false, "Unregistering the SW failed with " + e); 24 }); 25 }); 26 } 27 28 function doEventSource() { 29 var source = new EventSource(prefix + "eventsource.resource"); 30 source.onmessage = function(e) { 31 source.onmessage = null; 32 source.close(); 33 ok(true, "EventSource should work with synthetic responses"); 34 doUnregister(); 35 }; 36 source.onerror = function(error) { 37 source.onmessage = null; 38 source.close(); 39 ok(false, "Something went wrong"); 40 }; 41 } 42 43 function onLoad() { 44 if (!parent) { 45 dump("eventsource/eventsource_synthetic_response.html shouldn't be launched directly!"); 46 } 47 48 window.addEventListener("message", function onMessage(e) { 49 if (e.data.status === "callback") { 50 switch(e.data.data) { 51 case "eventsource": 52 doEventSource(); 53 window.removeEventListener("message", onMessage); 54 break; 55 default: 56 ok(false, "Something went wrong") 57 break 58 } 59 } 60 }); 61 62 navigator.serviceWorker.ready.then(function() { 63 parent.postMessage({status: "callback", data: "ready"}, "*"); 64 }); 65 66 navigator.serviceWorker.addEventListener("message", function(event) { 67 parent.postMessage(event.data, "*"); 68 }); 69 } 70 71 </script> 72 </head> 73 <body onload="onLoad()"> 74 </body> 75 </html>