OLD | NEW |
1 <html> | 1 <!DOCTYPE HTML> |
2 <body> | 2 <script src="/js-test-resources/js-test.js"></script> |
3 <p>Test EventSource states for different status codes. Should print a series of
PASS messages followed by DONE.</p> | 3 <script src="script-tests/eventsource-status-code-states.js"></script> |
4 <div id="result"></div> | |
5 <script> | |
6 function log(msg) { | |
7 document.getElementById("result").innerHTML += msg + "<br>"; | |
8 } | |
9 | |
10 function arrayCompare(a1, a2) { | |
11 if (a1.length != a2.length) | |
12 return false; | |
13 for (var i = 0; i < a1.length; i++) | |
14 if (a1[i] != a2[i]) | |
15 return false; | |
16 return true; | |
17 } | |
18 | |
19 var stateNames = ["CONNECTING", "OPEN", "CLOSED"]; | |
20 for (var i in stateNames) | |
21 eval("var " + stateNames[i] + " = " + i); | |
22 | |
23 var tests = [{"code": 200, "expectedStates": [CONNECTING, OPEN, OPEN, CONNECTING
, CLOSED]}, | |
24 {"code": 204, "expectedStates": [CONNECTING,,, CLOSED, CLOSED]}, | |
25 {"code": 205, "expectedStates": [CONNECTING,,, CLOSED, CLOSED]}, | |
26 {"code": 202, "expectedStates": [CONNECTING,,, CLOSED, CLOSED]}, //
other 2xx | |
27 {"code": 301, "expectedStates": [CONNECTING, OPEN, OPEN, CONNECTING
, CLOSED]}, | |
28 {"code": 302, "expectedStates": [CONNECTING, OPEN, OPEN, CONNECTING
, CLOSED]}, | |
29 {"code": 303, "expectedStates": [CONNECTING, OPEN, OPEN, CONNECTING
, CLOSED]}, | |
30 {"code": 307, "expectedStates": [CONNECTING, OPEN, OPEN, CONNECTING
, CLOSED]}, | |
31 {"code": 404, "expectedStates": [CONNECTING,,, CLOSED, CLOSED]}]; /
/ any other | |
32 var count = 0; | |
33 | |
34 function runTest() { | |
35 if (count >= tests.length) { | |
36 log("DONE"); | |
37 if (window.testRunner) | |
38 testRunner.notifyDone(); | |
39 return; | |
40 } | |
41 | |
42 var states = []; | |
43 var es = new EventSource("resources/status-codes.php?status-code=" + tests[c
ount].code); | |
44 states[0] = es.readyState; | |
45 | |
46 es.onopen = function () { | |
47 states[1] = es.readyState; | |
48 }; | |
49 | |
50 es.onmessage = function (evt) { | |
51 states[2] = es.readyState; | |
52 }; | |
53 | |
54 es.onerror = function () { | |
55 states[3] = es.readyState; | |
56 es.close(); | |
57 states[4] = es.readyState; | |
58 | |
59 var result = arrayCompare(states, tests[count].expectedStates) ? "PASS"
: "FAIL"; | |
60 result += ": status code " + tests[count].code + " resulted in states "; | |
61 for (var i in states) | |
62 result += (i != 0 ? ", " : "") + stateNames[states[i]]; | |
63 log(result); | |
64 | |
65 setTimeout(runTest, 0); | |
66 count++; | |
67 }; | |
68 } | |
69 | |
70 if (window.testRunner) { | |
71 testRunner.dumpAsText(); | |
72 testRunner.waitUntilDone(); | |
73 } | |
74 | |
75 runTest(); | |
76 </script> | |
77 </body> | |
78 </html> | |
OLD | NEW |