test_websocket_basic.html (9028B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Basic WebSocket test</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 </head> 8 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"> 16 17 const kUrl = "ws://mochi.test:8888/tests/dom/websocket/tests/file_websocket_basic"; 18 19 var gTestElement; 20 var ws; 21 22 function forcegc() { 23 SpecialPowers.forceGC(); 24 SpecialPowers.gc(); 25 } 26 27 function testWebSocket() { 28 gTestElement = document.getElementById("test"); 29 30 SimpleTest.executeSoon(testWebSocket1); 31 } 32 33 /** 34 * Sends message keywords, then receives their values. 35 */ 36 function testWebSocket1() { 37 gTestElement.textContent = "Running testWebSocket1()"; 38 39 var results = ["test", 40 "/tests/dom/websocket/tests/file_websocket_basic", 41 "http://mochi.test:8888", 42 "end"]; 43 44 ws = new WebSocket(kUrl, "test"); 45 is(ws.url, kUrl, "[1] WebSocket.url"); 46 ws.onopen = function() { 47 const params = ["protocol", "resource", "origin", "end"]; 48 49 gTestElement.textContent += "\nSending :"; 50 for (var i = 0; i < params.length; ++i) { 51 gTestElement.textContent += " " + params[i]; 52 ws.send(params[i]); 53 } 54 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"); 61 62 SimpleTest.executeSoon(testWebSocket2); 63 }; 64 ws.onerror = function() { 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 } 75 76 /** 77 * Sends 1000+1 test messages, then receives them. 78 */ 79 function testWebSocket2() { 80 gTestElement.textContent = "Running testWebSocket2()"; 81 82 const displayInterval = 100; 83 const testCount = 1000; 84 const testMessage = "test message 2."; 85 86 var messageCount = 0; 87 88 ws = new WebSocket(kUrl, "test"); 89 ws.onopen = function() { 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"); 99 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"); 106 107 SimpleTest.executeSoon(testWebSocket3); 108 }; 109 ws.onerror = function() { 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 } 125 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]"; 131 132 const displayInterval = 10; 133 const testCount = 10; 134 const testMessage = "test message 3."; 135 136 var messageCount = 0; 137 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() { 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"); 154 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"); 161 162 SimpleTest.executeSoon(testWebSocket4); 163 }; 164 ws.onerror = function() { 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 } 182 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()"; 188 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"; 191 192 ws = new WebSocket(kUrl, "test"); 193 ws.onopen = function() { 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"); 201 202 SimpleTest.executeSoon(testWebSocket5); 203 }; 204 ws.onerror = function() { 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 } 219 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()"; 225 226 ws = new WebSocket(kUrl, "test"); 227 ws.onopen = function() { 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"); 234 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"); 238 239 gTestElement.textContent += "\ntestWebSocket5() completed"; 240 241 SimpleTest.executeSoon(testWebSocket6); 242 }; 243 ws.onerror = function() { 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 } 249 250 function testWebSocket6() { 251 gTestElement.textContent = "Running testWebSocket6()"; 252 253 var msgReceived = false; 254 ws = new WebSocket(kUrl, "test"); 255 ws.onopen = function() { 256 gTestElement.textContent += "\nSending ©"; 257 ws.send("©"); 258 gTestElement.textContent += " end"; 259 ws.send("end"); 260 }; 261 ws.onclose = function(e) { 262 ok(msgReceived, "[6] Number of received messages"); 263 ok(e.wasClean, "[6] Connection closed cleanly"); 264 265 SimpleTest.executeSoon(SimpleTest.finish); 266 }; 267 ws.onerror = function() { 268 ok(false, "[6] onerror() should not have been called!"); 269 gTestElement.textContent += "\nonerror() should not have been called!"; 270 SimpleTest.executeSoon(SimpleTest.finish); 271 }; 272 273 ws.onmessage = function(e) { 274 if (msgReceived) { 275 is(e.data, "end", "[6] Received message"); 276 } else { 277 gTestElement.textContent += "\nReceived: " + e.data; 278 is(e.data, "©", "[6] Received message"); 279 msgReceived = true; 280 } 281 }; 282 } 283 284 SimpleTest.waitForExplicitFinish(); 285 286 </script> 287 </pre> 288 </body> 289 </html>