OLD | NEW |
1 <html> | 1 <!DOCTYPE HTML> |
2 <body> | 2 <script src="/js-test-resources/js-test.js"></script> |
3 <p>Test EventSource reconnect after end of event stream. Should print a series o
f PASS messages followed by DONE.</p> | 3 <script src="script-tests/eventsource-reconnect.js"></script> |
4 <div id="result"></div> | |
5 <script> | |
6 function log(msg) { | |
7 document.getElementById("result").innerHTML += msg + "<br>"; | |
8 } | |
9 | |
10 var stateNames = ["CONNECTING", "OPEN", "CLOSED"]; | |
11 for (var i in stateNames) | |
12 eval("var " + stateNames[i] + " = " + i); | |
13 var retryTimeout; | |
14 | |
15 function checkReadyState(es, desiredState) { | |
16 var currState = es.readyState; | |
17 if (currState == desiredState) | |
18 log("PASS: state is " + stateNames[desiredState]); | |
19 else | |
20 log("FAIL: bad state (" + stateNames[currState] + ", should be " + state
Names[desiredState] + ")"); | |
21 } | |
22 | |
23 if (window.testRunner) { | |
24 testRunner.dumpAsText(); | |
25 testRunner.waitUntilDone(); | |
26 } | |
27 | |
28 var errCount = 0; | |
29 var es = new EventSource("resources/reconnect.php"); | |
30 | |
31 checkReadyState(es, CONNECTING); | |
32 | |
33 es.onopen = function (evt) { | |
34 checkReadyState(es, OPEN); | |
35 }; | |
36 | |
37 es.onmessage = function (evt) { | |
38 if (!errCount) { | |
39 if (evt.lastEventId == "77") | |
40 log("PASS: got lastEventId \"77\""); | |
41 else | |
42 log("FAIL: unexpected lastEventId \"" + evt.lastEventId + "\""); | |
43 } else { | |
44 if (evt.data != "77") | |
45 log("FAIL: Last-Event-ID header was incorrect/missing"); | |
46 else if (evt.lastEventId != "77") | |
47 log("FAIL: the lastEventId property was incorrect"); | |
48 else | |
49 log("PASS: Last-Event-ID header and the lastEventId property were co
rrect"); | |
50 } | |
51 }; | |
52 | |
53 es.onerror = function () { | |
54 errCount++; | |
55 if (errCount < 2) { | |
56 checkReadyState(es, CONNECTING); | |
57 retryTimeout = setTimeout(end, 1000); | |
58 return; | |
59 } | |
60 clearTimeout(retryTimeout); | |
61 retryTimeout = null; | |
62 end(); | |
63 }; | |
64 | |
65 function end() { | |
66 es.close(); | |
67 if (retryTimeout) | |
68 log("FAIL: did not reconnect in time"); | |
69 else { | |
70 checkReadyState(es, CLOSED); | |
71 log("DONE"); | |
72 } | |
73 | |
74 if (window.testRunner) | |
75 testRunner.notifyDone(); | |
76 } | |
77 </script> | |
78 </body> | |
79 </html> | |
OLD | NEW |