OLD | NEW |
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> | 1 <!DOCTYPE HTML> |
2 <html> | |
3 <head> | |
4 <script src="/js-test-resources/js-test.js"></script> | 2 <script src="/js-test-resources/js-test.js"></script> |
5 </head> | 3 <script src="resources/close-common.js"></script> |
6 <body> | 4 <script src="resources/mac-dropped-wakeup-workaround.js"></script> |
7 <div id="description"></div> | 5 <script> |
8 <div id="console"></div> | |
9 <script type="text/javascript"> | |
10 description("Verify WebSocket::close behaviors."); | 6 description("Verify WebSocket::close behaviors."); |
11 | 7 |
12 window.jsTestIsAsync = true; | 8 window.jsTestIsAsync = true; |
13 | 9 |
14 var exceptionName; | 10 testClose(); |
15 var exceptionMessage; | |
16 var exceptionProto; | |
17 var closeEvent; | |
18 var code; | |
19 var reason; | |
20 var result; | |
21 var invalidAccessErr = "InvalidAccessError"; | |
22 var syntaxErr = "SyntaxError"; | |
23 var normalClosure = 1000; | |
24 var abnormalClosure = 1006; | |
25 var url = "ws://127.0.0.1:8880/close"; | |
26 var ws; | |
27 var testId; | |
28 | |
29 var codeTestCodes = [ | |
30 999, 1001, 2999, 5000, 65536 + 1000, 0x100000000 + 1000, 2999.9, NaN, "0", "
100", 1/0, -1/0, 0/0, | |
31 1000.0 | |
32 ]; | |
33 | |
34 var reasonTestReasons = [ | |
35 "123456789012345678901234567890123456789012345678901234567890123456789012345
6789012345678901234567890123456789012345678901234", // 124 Byte | |
36 "123456789012345678901234567890123456789012345678901234567890123456789012345
67890123456789012345678901234567890123456789012\u00a9", // length is 123, but 12
4 Byte in UTF-8 | |
37 "123456789012345678901234567890123456789012345678901234567890123456789012345
678901234567890123456789012345678901234567890123", // 123 Byte | |
38 ]; | |
39 | |
40 var reasonTestResults = [ | |
41 false, | |
42 false, | |
43 true, | |
44 ]; | |
45 | |
46 function handleOpen() | |
47 { | |
48 testFailed("handleOpen() was called."); | |
49 } | |
50 | |
51 function handleError() | |
52 { | |
53 testFailed("handleError() was called."); | |
54 } | |
55 | |
56 function handleClose() | |
57 { | |
58 testFailed("handleClose() was called."); | |
59 } | |
60 | |
61 function handleMessage() | |
62 { | |
63 testFailed("handleMessage() was called."); | |
64 } | |
65 | |
66 function setDefaultHandlers(ws) | |
67 { | |
68 ws.onopen = handleOpen; | |
69 ws.onerror = handleError; | |
70 ws.onclose = handleClose; | |
71 ws.onmessage = handleMessage; | |
72 } | |
73 | |
74 function runCodeTest() | |
75 { | |
76 ws = new WebSocket(url); | |
77 setDefaultHandlers(ws); | |
78 for (var id = 0; id < codeTestCodes.length; id++) { | |
79 if (codeTestCodes[id] != normalClosure) { | |
80 debug("Invalid code test: " + id); | |
81 ws.onclose = handleClose; | |
82 } else { | |
83 ws.onclose = function (e) | |
84 { | |
85 debug("runCodeTest: onclose()."); | |
86 closeEvent = e; | |
87 shouldBe("closeEvent.code", "abnormalClosure"); | |
88 if (closeEvent.code == abnormalClosure) | |
89 runInvalidStringTest(); | |
90 }; | |
91 ws.onerror = function() | |
92 { | |
93 testPassed("onerror was called."); | |
94 }; | |
95 } | |
96 try { | |
97 ws.close(codeTestCodes[id]); | |
98 } catch (e) { | |
99 debug("Code " + codeTestCodes[id] + " must cause " + invalidAccessEr
r + '.'); | |
100 exceptionName = e.name; | |
101 exceptionMessage = e.message; | |
102 exceptionProto = Object.getPrototypeOf(e); | |
103 shouldBeTrue("exceptionProto === DOMException.prototype"); | |
104 shouldBe("exceptionName", "invalidAccessErr"); | |
105 var expectedCode = codeTestCodes[id]; | |
106 if (!expectedCode) | |
107 expectedCode = 0; | |
108 else if (expectedCode > 65535) | |
109 expectedCode = 65535; | |
110 else if (expectedCode < 0) | |
111 expectedCode = 0; | |
112 expectedCode = Math.floor(expectedCode); | |
113 shouldBe("exceptionMessage", '"Failed to execute \'close\' on \'WebS
ocket\': The code must be either 1000, or between 3000 and 4999. ' + expectedCod
e + ' is neither."'); | |
114 } | |
115 } | |
116 } | |
117 | |
118 function runInvalidStringTest() | |
119 { | |
120 // FIXME: unpaired surrogates throw SyntaxError | |
121 debug("Skip invalid string test."); | |
122 runReasonTest(); | |
123 } | |
124 | |
125 function runReasonTest() | |
126 { | |
127 ws = new WebSocket(url); | |
128 setDefaultHandlers(ws); | |
129 for (var id = 0; id < reasonTestReasons.length; id++) { | |
130 debug("Reason test: " + id); | |
131 if (!reasonTestResults[id]) { | |
132 debug(" with invalid reason: " + reasonTestReasons[id]); | |
133 ws.onclose = handleClose; | |
134 } else { | |
135 ws.onclose = function (e) | |
136 { | |
137 debug("runReasonTest: onclose()."); | |
138 closeEvent = e; | |
139 shouldBe("closeEvent.code", "abnormalClosure"); | |
140 if (closeEvent.code == abnormalClosure) | |
141 runCodeAndReasonTest(); | |
142 }; | |
143 ws.onerror = function() | |
144 { | |
145 testPassed("onerror was called."); | |
146 } | |
147 } | |
148 try { | |
149 ws.close(normalClosure, reasonTestReasons[id]); | |
150 } catch (e) { | |
151 debug("Reason " + reasonTestReasons[id] + " must cause " + syntaxErr
+ '.'); | |
152 result = reasonTestResults[id]; | |
153 exceptionName = e.name; | |
154 exceptionProto = Object.getPrototypeOf(e); | |
155 shouldBeFalse("result"); | |
156 shouldBeTrue("exceptionProto === DOMException.prototype"); | |
157 shouldBe("exceptionName", "syntaxErr"); | |
158 } | |
159 } | |
160 } | |
161 | |
162 function runCodeAndReasonTest() | |
163 { | |
164 var codes = [ | |
165 1000, | |
166 3000, | |
167 4000, | |
168 4999 | |
169 ]; | |
170 var reasons = [ | |
171 "OK, Bye!", | |
172 "3000", | |
173 "code is 4000", | |
174 "\u00a9 Google" | |
175 ]; | |
176 (function test (id) { | |
177 debug("Code and reason test: " + id); | |
178 ws = new WebSocket(url); | |
179 setDefaultHandlers(ws); | |
180 ws.onopen = function () | |
181 { | |
182 ws.close(codes[id], reasons[id]); | |
183 }; | |
184 ws.onclose = function (e) | |
185 { | |
186 closeEvent = e; | |
187 code = codes[id]; | |
188 reason = reasons[id]; | |
189 debug("Code and reason must be"); | |
190 debug(" code : " + code); | |
191 debug(" reason: " + reason); | |
192 shouldBeTrue("closeEvent.wasClean"); | |
193 shouldBe("closeEvent.code", "code"); | |
194 shouldBe("closeEvent.reason", "reason"); | |
195 if (++id != codes.length) | |
196 test(id); | |
197 else | |
198 finishJSTest(); | |
199 }; | |
200 })(0); | |
201 } | |
202 | |
203 runCodeTest(); | |
204 | 11 |
205 </script> | 12 </script> |
206 </body> | |
207 </html> | |
OLD | NEW |