1 // Common code for both the plain and Worker variants of close-code-and-reason.
5 var codeNormalClosure = 1000;
6 var codeNoStatusRcvd = 1005;
7 var codeAbnormalClosure = 1006;
10 function closeDuringOpen()
12 var ws = new WebSocket("ws://127.0.0.1:8880/echo");
14 ws.onopen = function(event)
16 testFailed("ws.onopen() was called. (message = \"" + event.data + "\")");
19 ws.onclose = function(event)
21 debug("ws.onclose() was called.");
23 shouldBeFalse("closeEvent.wasClean");
24 shouldBe("closeEvent.code", "codeAbnormalClosure");
25 shouldBe("closeEvent.reason", "emptyString");
34 "-", // request close frame without code and reason
35 "--", // request close frame with invalid body which size is 1
36 "1000 ok", // request close frame with code 1000 and reason
37 "1005 foo", // request close frame with forbidden code 1005 and reason
38 "1006 bar", // request close frame with forbidden code 1006 and reason
39 "1015 baz", // request close frame with forbidden code 1015 and reason
40 "0 good bye", // request close frame with specified code and reason
41 "10 good bye", // request close frame with specified code and reason
42 "65535 good bye", // request close frame with specified code and reason
55 var expectedReason = [
66 var expectedWasClean = [
78 WebSocketTest = function()
80 this.ws = new WebSocket("ws://127.0.0.1:8880/close-code-and-reason");
81 this.ws.onopen = this.onopen;
82 this.ws.onmessage = this.onmessage;
83 this.ws.onclose = this.onclose.bind(this);
84 this.timeoutID = setTimeout(this.ontimeout.bind(this), 400);
87 WebSocketTest.prototype.onopen = function()
89 debug("WebSocketTest.onopen() was called with testId = " + testId + ".");
90 this.send(sendData[testId]);
93 WebSocketTest.prototype.onmessage = function(event)
95 testFailed("WebSocketTest.onmessage() was called. (message = \"" + event.data + "\")");
98 WebSocketTest.prototype.onclose = function(event)
101 debug("WebSocketTest.onclose() was called with testId = " + testId + ".");
103 shouldEvaluateTo("closeEvent.wasClean", expectedWasClean[testId]);
104 shouldEvaluateTo("closeEvent.code", expectedCode[testId]);
105 shouldEvaluateTo("closeEvent.reason", expectedReason[testId]);
107 // Test that the attributes of the CloseEvent are readonly.
109 closeEvent.reason = "readonly";
110 closeEvent.wasClean = !closeEvent.wasClean;
111 shouldEvaluateTo("closeEvent.wasClean", expectedWasClean[testId]);
112 shouldEvaluateTo("closeEvent.code", expectedCode[testId]);
113 shouldEvaluateTo("closeEvent.reason", expectedReason[testId]);
115 clearTimeout(this.timeoutID);
118 if (testId < testNum)
119 test = new WebSocketTest();
124 WebSocketTest.prototype.ontimeout = function()
126 testFailed("WebSocketTest.ontimeout() was called. (testId = " + testId + ")");
127 // Ensure that none of the WebSocket handlers run after finishJSTest().
128 var ignoreEvent = function(event) {};
129 this.ws.onopen = ignoreEvent;
130 this.ws.onmessage = ignoreEvent;
131 this.ws.onclose = ignoreEvent;
137 function testCloseCodeAndReason()
140 test = new WebSocketTest();