Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>Basic WebSocket test</title>
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
7 </head>
9 <body onload="testWebSocket()">
10 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug 472529</a>
11 <p id="display"></p>
12 <div id="content" style="display: none">
13 </div>
14 <pre id="test">
15 <script class="testbody" type="text/javascript">
17 const kUrl = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic";
19 var gTestElement;
20 var ws;
22 function forcegc() {
23 SpecialPowers.forceGC();
24 SpecialPowers.gc();
25 }
27 function testWebSocket() {
28 gTestElement = document.getElementById("test");
30 SimpleTest.executeSoon(testWebSocket1);
31 }
33 /**
34 * Sends message keywords, then receives their values.
35 */
36 function testWebSocket1() {
37 gTestElement.textContent = "Running testWebSocket1()";
39 var results = ["test",
40 "/tests/content/base/test/file_websocket_basic",
41 "http://mochi.test:8888",
42 "end"];
44 ws = new WebSocket(kUrl, "test");
45 is(ws.url, kUrl, "[1] WebSocket.url");
46 ws.onopen = function(e) {
47 const params = ["protocol", "resource", "origin", "end"];
49 gTestElement.textContent += "\nSending :";
50 for (var i = 0; i < params.length; ++i) {
51 gTestElement.textContent += " " + params[i];
52 ws.send(params[i]);
53 }
55 // Set this before onmessage() is called, so it is displayed once only.
56 gTestElement.textContent += "\nReceived:";
57 };
58 ws.onclose = function(e) {
59 is(results.length, 0, "[1] Number of unreceived messages");
60 ok(e.wasClean, "[1] Connection closed cleanly");
62 SimpleTest.executeSoon(testWebSocket2);
63 };
64 ws.onerror = function(e) {
65 ok(false, "[1] onerror() should not have been called!");
66 gTestElement.textContent += "\nonerror() should not have been called!";
67 SimpleTest.executeSoon(SimpleTest.finish);
68 };
69 ws.onmessage = function(e) {
70 is(e.data, results[0], "[1] Received message");
71 gTestElement.textContent += " " + e.data;
72 results.shift();
73 };
74 }
76 /**
77 * Sends 1000+1 test messages, then receives them.
78 */
79 function testWebSocket2() {
80 gTestElement.textContent = "Running testWebSocket2()";
82 const displayInterval = 100;
83 const testCount = 1000;
84 const testMessage = "test message 2.";
86 var messageCount = 0;
88 ws = new WebSocket(kUrl, "test");
89 ws.onopen = function(e) {
90 gTestElement.textContent += "\nSending :";
91 for (var i = 1; i <= testCount; ++i) {
92 if (i % displayInterval == 1) {
93 gTestElement.textContent += " " + i;
94 }
95 ws.send(testMessage + i);
96 }
97 gTestElement.textContent += " end";
98 ws.send("end");
100 // Set this before onmessage() is called, so it is displayed once only.
101 gTestElement.textContent += "\nReceived:";
102 };
103 ws.onclose = function(e) {
104 is(messageCount, testCount + 1, "[2] Number of received messages");
105 ok(e.wasClean, "[2] Connection closed cleanly");
107 SimpleTest.executeSoon(testWebSocket3);
108 };
109 ws.onerror = function(e) {
110 ok(false, "[2] onerror() should not have been called!");
111 gTestElement.textContent += "\nonerror() should not have been called!";
112 SimpleTest.executeSoon(SimpleTest.finish);
113 };
114 ws.onmessage = function(e) {
115 ++messageCount;
116 if (messageCount > testCount)
117 is(e.data, "end", "[2] Received message");
118 else
119 is(e.data, testMessage + messageCount, "[2] Received message");
120 if (messageCount % displayInterval == 1) {
121 gTestElement.textContent += " " + messageCount;
122 }
123 };
124 }
126 /**
127 * Sends testcount+1 test messages, then receives them, calling forcegc() at each step.
128 */
129 function testWebSocket3() {
130 gTestElement.textContent = "Running testWebSocket3() [can take a little while]";
132 const displayInterval = 10;
133 const testCount = 10;
134 const testMessage = "test message 3.";
136 var messageCount = 0;
138 ws = new WebSocket(kUrl, "test");
139 // Set this before onopen() is called,
140 // otherwise its display would be delayed by forcegc() calls...
141 gTestElement.textContent += "\nSending :";
142 ws.onopen = function(e) {
143 for (var i = 1; i <= testCount; ++i) {
144 forcegc();
145 if (i % displayInterval == 1) {
146 // Actual display is delayed by forcegc() calls...
147 gTestElement.textContent += " " + i;
148 }
149 ws.send(testMessage + i);
150 }
151 forcegc();
152 gTestElement.textContent += " end";
153 ws.send("end");
155 // Set this before onmessage() is called, so it is displayed once only.
156 gTestElement.textContent += "\nReceived:";
157 };
158 ws.onclose = function(e) {
159 is(messageCount, testCount + 1, "[3] Number of received messages");
160 ok(e.wasClean, "[3] Connection closed cleanly");
162 SimpleTest.executeSoon(testWebSocket4);
163 };
164 ws.onerror = function(e) {
165 ok(false, "[3] onerror() should not have been called!");
166 gTestElement.textContent += "\nonerror() should not have been called!";
167 SimpleTest.executeSoon(SimpleTest.finish);
168 };
169 ws.onmessage = function(e) {
170 forcegc();
171 ++messageCount;
172 if (messageCount > testCount)
173 is(e.data, "end", "[3] Received message");
174 else
175 is(e.data, testMessage + messageCount, "[3] Received message");
176 if (messageCount % displayInterval == 1) {
177 // Actual display is delayed by forcegc() call(s)...
178 gTestElement.textContent += " " + messageCount;
179 }
180 };
181 }
183 /**
184 * Sends a huge test message, then receives it, then closes the WebSocket from client-side.
185 */
186 function testWebSocket4() {
187 gTestElement.textContent = "Running testWebSocket4()";
189 // String length = 13 + ((10,000 - 1) * 26) + 11 = 259,998 = almost 254 KiB.
190 const longString = "messageStart " + new Array(10000).join(" -huge WebSocket message- ") + " messageEnd";
192 ws = new WebSocket(kUrl, "test");
193 ws.onopen = function(e) {
194 is(this, ws, "[4, onopen()] 'this' should point to the WebSocket.");
195 gTestElement.textContent += "\nSending the huge message";
196 ws.send(longString);
197 };
198 ws.onclose = function(e) {
199 is(this, ws, "[4, onclose()] 'this' should point to the WebSocket.");
200 ok(e.wasClean, "[4] Connection closed cleanly");
202 SimpleTest.executeSoon(testWebSocket5);
203 };
204 ws.onerror = function(e) {
205 is(this, ws, "[4, onerror()] 'this' should point to the WebSocket.");
206 ok(false, "[4, onerror()] should not have been called!");
207 gTestElement.textContent += "\nonerror() should not have been called!";
208 SimpleTest.executeSoon(SimpleTest.finish);
209 };
210 ws.onmessage = function(e) {
211 is(this, ws, "[4, onmessage()] 'this' should point to the WebSocket.");
212 // Do not use |is(e.data, longString, "...");| that results in a _very_ long line.
213 is(e.data.length, longString.length, "[4] Length of received message");
214 ok(e.data == longString, "[4] Content of received message");
215 gTestElement.textContent += "\nReceived the huge message";
216 this.close();
217 };
218 }
220 /**
221 * Closes the WebSocket from client-side, then sends a test message that should be buffered.
222 */
223 function testWebSocket5() {
224 gTestElement.textContent = "Running testWebSocket5()";
226 ws = new WebSocket(kUrl, "test");
227 ws.onopen = function(e) {
228 is(this.bufferedAmount, 0, "[5] Length of empty buffer before closing");
229 this.close();
230 };
231 ws.onclose = function(e) {
232 ok(e.wasClean, "[5] Connection closed cleanly");
233 is(this.bufferedAmount, 0, "[5] Length of empty buffer after closing");
235 var msg = "test message to be buffered";
236 this.send(msg);
237 is(this.bufferedAmount, msg.length, "[5] Length of buffered message sent after closing");
239 gTestElement.textContent += "\ntestWebSocket5() completed";
241 SimpleTest.executeSoon(SimpleTest.finish);
242 };
243 ws.onerror = function(e) {
244 ok(false, "[5] onerror() should not have been called!");
245 gTestElement.textContent += "\nonerror() should not have been called!";
246 SimpleTest.executeSoon(SimpleTest.finish);
247 };
248 }
250 SimpleTest.waitForExplicitFinish();
252 </script>
253 </pre>
254 </body>
255 </html>