content/base/test/test_websocket.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"></meta>
michael@0 5 <title>WebSocket test</title>
michael@0 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body onload="testWebSocket()">
michael@0 10 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug </a>
michael@0 11 <p id="display">
michael@0 12 <input id="fileList" type="file"></input>
michael@0 13 </p>
michael@0 14 <div id="content">
michael@0 15 </div>
michael@0 16 <pre id="test">
michael@0 17 <script class="testbody" type="text/javascript">
michael@0 18
michael@0 19 /*
michael@0 20 * tests:
michael@0 21 * 1. client tries to connect to a http scheme location;
michael@0 22 * 2. assure serialization of the connections;
michael@0 23 * 3. client tries to connect to an non-existent ws server;
michael@0 24 * 4. client tries to connect using a relative url;
michael@0 25 * 5. client uses an invalid protocol value;
michael@0 26 * 6. counter and encoding check;
michael@0 27 * 7. onmessage event origin property check
michael@0 28 * 8. client calls close() and the server sends the close frame (with no code
michael@0 29 * or reason) in acknowledgement;
michael@0 30 * 9. client closes the connection before the ws connection is established;
michael@0 31 * 10. client sends a message before the ws connection is established;
michael@0 32 * 11. a simple hello echo;
michael@0 33 * 12. client sends a message containing unpaired surrogates
michael@0 34 * 13. server sends an invalid message;
michael@0 35 * 14. server sends the close frame, it doesn't close the tcp connection and
michael@0 36 * it keeps sending normal ws messages;
michael@0 37 * 15. server closes the tcp connection, but it doesn't send the close frame;
michael@0 38 * 16. client calls close() and tries to send a message;
michael@0 39 * 17. see bug 572975 - all event listeners set
michael@0 40 * 18. client tries to connect to an http resource;
michael@0 41 * 19. server closes the tcp connection before establishing the ws connection;
michael@0 42 * 20. see bug 572975 - only on error and onclose event listeners set
michael@0 43 * 21. see bug 572975 - same as test 17, but delete strong event listeners when
michael@0 44 * receiving the message event;
michael@0 45 * 22. server takes too long to establish the ws connection;
michael@0 46 * 23. should detect WebSocket on window object;
michael@0 47 * 24. server rejects sub-protocol string
michael@0 48 * 25. ctor with valid empty sub-protocol array
michael@0 49 * 26. ctor with invalid sub-protocol array containing 1 empty element
michael@0 50 * 27. ctor with invalid sub-protocol array containing an empty element in list
michael@0 51 * 28. ctor using valid 1 element sub-protocol array
michael@0 52 * 29. ctor using all valid 5 element sub-protocol array
michael@0 53 * 30. ctor using valid 1 element sub-protocol array with element server will
michael@0 54 * reject
michael@0 55 * 31. ctor using valid 2 element sub-protocol array with 1 element server
michael@0 56 * will reject and one server will accept.
michael@0 57 * 32. ctor using invalid sub-protocol array that contains duplicate items
michael@0 58 * 33. test for sending/receiving custom close code (but no close reason)
michael@0 59 * 34. test for receiving custom close code and reason
michael@0 60 * 35. test for sending custom close code and reason
michael@0 61 * 36. negative test for sending out of range close code
michael@0 62 * 37. negative test for too long of a close reason
michael@0 63 * 38. ensure extensions attribute is defined
michael@0 64 * 39. a basic wss:// connectivity test
michael@0 65 * 40. negative test for wss:// with no cert
michael@0 66 * 41. HSTS
michael@0 67 * 42. non-char utf-8 sequences
michael@0 68 * 43. Test setting binaryType attribute
michael@0 69 * 44. Test sending/receving binary ArrayBuffer
michael@0 70 * 45. Test sending/receving binary Blob
michael@0 71 * 46. Test that we don't dispatch incoming msgs once in CLOSING state
michael@0 72 * 47. Make sure onerror/onclose aren't called during close()
michael@0 73 */
michael@0 74
michael@0 75 var first_test = 1;
michael@0 76 var last_test = 47;
michael@0 77
michael@0 78
michael@0 79 // Set this to >1 if you want to run the suite multiple times to probe for
michael@0 80 // random orange failures.
michael@0 81 // - Do NOT check into mozilla-central with a value != 1.
michael@0 82 // - Too large a count will wind up causing tryserver to timeout the test (which
michael@0 83 // is ok, but means all testruns will be orange). If I set first_test to >22
michael@0 84 // (i.e don't run any of the tests that require waiting) I can get ~250-300
michael@0 85 // iterations of the remaining tests w/o a timeout.
michael@0 86 var testsuite_iterations = 1;
michael@0 87
michael@0 88
michael@0 89 var current_test = first_test;
michael@0 90 var testsuite_iteration = 1;
michael@0 91
michael@0 92 var test_started = new Array(last_test);
michael@0 93 var all_ws = [];
michael@0 94
michael@0 95 function shouldNotOpen(e)
michael@0 96 {
michael@0 97 var ws = e.target;
michael@0 98 ok(false, "onopen shouldn't be called on test " + ws._testNumber + "!");
michael@0 99 }
michael@0 100
michael@0 101 function shouldNotReceiveCloseEvent(e)
michael@0 102 {
michael@0 103 var ws = e.target;
michael@0 104 var extendedErrorInfo = "";
michael@0 105 if (!ws._testNumber) {
michael@0 106 extendedErrorInfo += "\nws members:\n";
michael@0 107 for (var i in ws) {
michael@0 108 extendedErrorInfo += (i + ": " + ws[i] + "\n");
michael@0 109 }
michael@0 110
michael@0 111 extendedErrorInfo += "\ne members:\n";
michael@0 112 for (var i in e) {
michael@0 113 extendedErrorInfo += (i + ": " + e[i] + "\n");
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 // FIXME: see bug 578276. This should be a test failure, but it's too flaky on the tbox.
michael@0 118 ok(true, "onclose shouldn't be called on test " + ws._testNumber + "!" + extendedErrorInfo);
michael@0 119 }
michael@0 120
michael@0 121 function shouldCloseCleanly(e)
michael@0 122 {
michael@0 123 var ws = e.target;
michael@0 124 ok(e.wasClean, "the ws connection in test " + ws._testNumber + " should be closed cleanly");
michael@0 125 }
michael@0 126
michael@0 127 function shouldCloseNotCleanly(e)
michael@0 128 {
michael@0 129 var ws = e.target;
michael@0 130 ok(!e.wasClean, "the ws connection in test " + ws._testNumber + " shouldn't be closed cleanly");
michael@0 131 }
michael@0 132
michael@0 133 function ignoreError(e)
michael@0 134 {
michael@0 135 }
michael@0 136
michael@0 137 function CreateTestWS(ws_location, ws_protocol, no_increment_test)
michael@0 138 {
michael@0 139 var ws;
michael@0 140
michael@0 141 try {
michael@0 142 if (ws_protocol == undefined) {
michael@0 143 ws = new WebSocket(ws_location);
michael@0 144 } else {
michael@0 145 ws = new WebSocket(ws_location, ws_protocol);
michael@0 146 }
michael@0 147
michael@0 148
michael@0 149 ws._testNumber = current_test;
michael@0 150 ws._receivedCloseEvent = false;
michael@0 151 ok(true, "Created websocket for test " + ws._testNumber +"\n");
michael@0 152
michael@0 153 ws.onerror = function(e)
michael@0 154 {
michael@0 155 ok(false, "onerror called on test " + e.target._testNumber + "!");
michael@0 156 };
michael@0 157 ws.addEventListener("close", function(e)
michael@0 158 {
michael@0 159 ws._receivedCloseEvent = true;
michael@0 160 }, false);
michael@0 161 }
michael@0 162 catch (e) {
michael@0 163 throw e;
michael@0 164 }
michael@0 165 finally {
michael@0 166 if (!no_increment_test) {
michael@0 167 current_test++;
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 all_ws.push(ws);
michael@0 172 return ws;
michael@0 173 }
michael@0 174
michael@0 175 function forcegc()
michael@0 176 {
michael@0 177 SpecialPowers.forceGC();
michael@0 178 SpecialPowers.gc();
michael@0 179 setTimeout(function()
michael@0 180 {
michael@0 181 SpecialPowers.gc();
michael@0 182 }, 1);
michael@0 183 }
michael@0 184
michael@0 185 function doTest(number)
michael@0 186 {
michael@0 187 if (number > last_test) {
michael@0 188 ranAllTests = true;
michael@0 189 maybeFinished();
michael@0 190 return;
michael@0 191 }
michael@0 192
michael@0 193 if (testsuite_iteration > 1) {
michael@0 194 $("feedback").innerHTML = "test suite iteration #" + testsuite_iteration + " of " + testsuite_iterations +
michael@0 195 ": executing test: " + number + " of " + last_test + " tests.";
michael@0 196 } else {
michael@0 197 $("feedback").innerHTML = "executing test: " + number + " of " + last_test + " tests.";
michael@0 198 }
michael@0 199
michael@0 200 var fnTest = eval("test" + number + "");
michael@0 201
michael@0 202 if (test_started[number] === true) {
michael@0 203 doTest(number + 1);
michael@0 204 return;
michael@0 205 }
michael@0 206
michael@0 207 test_started[number] = true;
michael@0 208 fnTest();
michael@0 209 }
michael@0 210 doTest.timeoutId = null;
michael@0 211
michael@0 212 function test1()
michael@0 213 {
michael@0 214 try {
michael@0 215 var ws = CreateTestWS("http://mochi.test:8888/tests/content/base/test/file_websocket");
michael@0 216 ok(false, "test1 failed");
michael@0 217 }
michael@0 218 catch (e) {
michael@0 219 ok(true, "test1 failed");
michael@0 220 }
michael@0 221 doTest(2);
michael@0 222 }
michael@0 223
michael@0 224 // this test expects that the serialization list to connect to the proxy
michael@0 225 // is empty. Use different domain so we can run this in the background
michael@0 226 // and not delay other tests.
michael@0 227
michael@0 228 var waitTest2Part1 = false;
michael@0 229 var waitTest2Part2 = false;
michael@0 230
michael@0 231 function test2()
michael@0 232 {
michael@0 233 waitTest2Part1 = true;
michael@0 234 waitTest2Part2 = true;
michael@0 235
michael@0 236 var ws1 = CreateTestWS("ws://sub2.test2.example.com/tests/content/base/test/file_websocket", "test-2.1");
michael@0 237 current_test--; // CreateTestWS incremented this
michael@0 238 var ws2 = CreateTestWS("ws://sub2.test2.example.com/tests/content/base/test/file_websocket", "test-2.2");
michael@0 239
michael@0 240 var ws2CanConnect = false;
michael@0 241
michael@0 242 // the server will delay ws1 for 5 seconds, but the other tests can
michael@0 243 // proceed in parallel
michael@0 244 doTest(3);
michael@0 245
michael@0 246 ws1.onopen = function()
michael@0 247 {
michael@0 248 ok(true, "ws1 open in test 2");
michael@0 249 ws2CanConnect = true;
michael@0 250 ws1.close();
michael@0 251 }
michael@0 252
michael@0 253 ws1.onclose = function(e)
michael@0 254 {
michael@0 255 waitTest2Part1 = false;
michael@0 256 maybeFinished();
michael@0 257 };
michael@0 258
michael@0 259 ws2.onopen = function()
michael@0 260 {
michael@0 261 ok(ws2CanConnect, "shouldn't connect yet in test-2!");
michael@0 262 ws2.close();
michael@0 263 }
michael@0 264
michael@0 265 ws2.onclose = function(e)
michael@0 266 {
michael@0 267 waitTest2Part2 = false;
michael@0 268 maybeFinished();
michael@0 269 };
michael@0 270 }
michael@0 271
michael@0 272 function test3()
michael@0 273 {
michael@0 274 var hasError = false;
michael@0 275 var ws = CreateTestWS("ws://this.websocket.server.probably.does.not.exist");
michael@0 276 ws.onopen = shouldNotOpen;
michael@0 277 ws.onerror = function (e)
michael@0 278 {
michael@0 279 hasError = true;
michael@0 280 }
michael@0 281
michael@0 282 ws.onclose = function(e)
michael@0 283 {
michael@0 284 shouldCloseNotCleanly(e);
michael@0 285 ok(hasError, "rcvd onerror event");
michael@0 286 ok(e.code == 1006, "test-3 close code should be 1006 but is:" + e.code);
michael@0 287 doTest(4);
michael@0 288 };
michael@0 289 }
michael@0 290
michael@0 291 function test4()
michael@0 292 {
michael@0 293 try {
michael@0 294 var ws = CreateTestWS("file_websocket");
michael@0 295 ok(false, "test-4 failed");
michael@0 296 }
michael@0 297 catch (e) {
michael@0 298 ok(true, "test-4 failed");
michael@0 299 }
michael@0 300 doTest(5);
michael@0 301 }
michael@0 302
michael@0 303 function test5()
michael@0 304 {
michael@0 305 try {
michael@0 306 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "");
michael@0 307 ok(false, "couldn't accept an empty string in the protocol parameter");
michael@0 308 }
michael@0 309 catch (e) {
michael@0 310 ok(true, "couldn't accept an empty string in the protocol parameter");
michael@0 311 }
michael@0 312 current_test--; // CreateTestWS incremented this
michael@0 313 try {
michael@0 314 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "\n");
michael@0 315 ok(false, "couldn't accept any not printable ASCII character in the protocol parameter");
michael@0 316 }
michael@0 317 catch (e) {
michael@0 318 ok(true, "couldn't accept any not printable ASCII character in the protocol parameter");
michael@0 319 }
michael@0 320 current_test--; // CreateTestWS incremented this
michael@0 321 try {
michael@0 322 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test 5");
michael@0 323 ok(false, "U+0020 not acceptable in protocol parameter");
michael@0 324 }
michael@0 325 catch (e) {
michael@0 326 ok(true, "U+0020 not acceptable in protocol parameter");
michael@0 327 }
michael@0 328 doTest(6);
michael@0 329 }
michael@0 330
michael@0 331 function test6()
michael@0 332 {
michael@0 333 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-6");
michael@0 334 var counter = 1;
michael@0 335 ws.onopen = function()
michael@0 336 {
michael@0 337 ws.send(counter);
michael@0 338 }
michael@0 339 ws.onmessage = function(e)
michael@0 340 {
michael@0 341 if (counter == 5) {
michael@0 342 ok(e.data == "あいうえお", "test-6 counter 5 data ok");
michael@0 343 ws.close();
michael@0 344 } else {
michael@0 345 ok(e.data == counter+1, "bad counter");
michael@0 346 counter += 2;
michael@0 347 ws.send(counter);
michael@0 348 }
michael@0 349 }
michael@0 350 ws.onclose = function(e)
michael@0 351 {
michael@0 352 shouldCloseCleanly(e);
michael@0 353 doTest(7);
michael@0 354 };
michael@0 355 }
michael@0 356
michael@0 357 function test7()
michael@0 358 {
michael@0 359 var ws = CreateTestWS("ws://sub2.test2.example.org/tests/content/base/test/file_websocket", "test-7");
michael@0 360 var gotmsg = false;
michael@0 361
michael@0 362 ws.onopen = function()
michael@0 363 {
michael@0 364 ok(true, "test 7 open");
michael@0 365 }
michael@0 366 ws.onmessage = function(e)
michael@0 367 {
michael@0 368 ok(true, "test 7 message");
michael@0 369 ok(e.origin == "ws://sub2.test2.example.org", "onmessage origin set to ws:// host");
michael@0 370 gotmsg = true;
michael@0 371 ws.close();
michael@0 372 }
michael@0 373 ws.onclose = function(e)
michael@0 374 {
michael@0 375 ok(gotmsg, "recvd message in test 7 before close");
michael@0 376 shouldCloseCleanly(e);
michael@0 377 doTest(8);
michael@0 378 };
michael@0 379 }
michael@0 380
michael@0 381 function test8()
michael@0 382 {
michael@0 383 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-8");
michael@0 384 ws.onopen = function()
michael@0 385 {
michael@0 386 ok(ws.protocol == "test-8", "test-8 subprotocol selection");
michael@0 387 ws.close();
michael@0 388 }
michael@0 389 ws.onclose = function(e)
michael@0 390 {
michael@0 391 shouldCloseCleanly(e);
michael@0 392 // We called close() with no close code: so pywebsocket will also send no
michael@0 393 // close code, which translates to code 1005
michael@0 394 ok(e.code == 1005, "test-8 close code has wrong value:" + e.code);
michael@0 395 ok(e.reason == "", "test-8 close reason has wrong value:" + e.reason);
michael@0 396 doTest(9);
michael@0 397 };
michael@0 398 }
michael@0 399
michael@0 400 var waitTest9 = false;
michael@0 401
michael@0 402 function test9()
michael@0 403 {
michael@0 404 waitTest9 = true;
michael@0 405
michael@0 406 var ws = CreateTestWS("ws://test2.example.org/tests/content/base/test/file_websocket", "test-9");
michael@0 407 ws._receivedErrorEvent = false;
michael@0 408 ws.onopen = shouldNotOpen;
michael@0 409 ws.onerror = function(e)
michael@0 410 {
michael@0 411 ws._receivedErrorEvent = true;
michael@0 412 };
michael@0 413 ws.onclose = function(e)
michael@0 414 {
michael@0 415 ok(ws._receivedErrorEvent, "Didn't received the error event in test 9.");
michael@0 416 shouldCloseNotCleanly(e);
michael@0 417 waitTest9 = false;
michael@0 418 maybeFinished();
michael@0 419 };
michael@0 420
michael@0 421 ws.close();
michael@0 422
michael@0 423 // the server injects a delay, so proceed with this in the background
michael@0 424 doTest(10);
michael@0 425 }
michael@0 426
michael@0 427 var waitTest10 = false;
michael@0 428
michael@0 429 function test10()
michael@0 430 {
michael@0 431 waitTest10 = true;
michael@0 432
michael@0 433 var ws = CreateTestWS("ws://sub1.test1.example.com/tests/content/base/test/file_websocket", "test-10");
michael@0 434 ws.onclose = function(e)
michael@0 435 {
michael@0 436 shouldCloseCleanly(e);
michael@0 437 waitTest10 = false;
michael@0 438 maybeFinished();
michael@0 439 }
michael@0 440
michael@0 441 try {
michael@0 442 ws.send("client data");
michael@0 443 ok(false, "Couldn't send data before connecting!");
michael@0 444 }
michael@0 445 catch (e) {
michael@0 446 ok(true, "Couldn't send data before connecting!");
michael@0 447 }
michael@0 448 ws.onopen = function()
michael@0 449 {
michael@0 450 ok(true, "test 10 opened");
michael@0 451 ws.close();
michael@0 452 }
michael@0 453
michael@0 454 // proceed with this test in the background
michael@0 455 doTest(11);
michael@0 456 }
michael@0 457
michael@0 458 function test11()
michael@0 459 {
michael@0 460 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-11");
michael@0 461 ok(ws.readyState == 0, "create bad readyState in test-11!");
michael@0 462 ws.onopen = function()
michael@0 463 {
michael@0 464 ok(ws.readyState == 1, "open bad readyState in test-11!");
michael@0 465 ws.send("client data");
michael@0 466 }
michael@0 467 ws.onmessage = function(e)
michael@0 468 {
michael@0 469 ok(e.data == "server data", "bad received message in test-11!");
michael@0 470 ws.close(1000, "Have a nice day");
michael@0 471
michael@0 472 // this ok() is disabled due to a race condition - it state may have
michael@0 473 // advanced through 2 (closing) and into 3 (closed) before it is evald
michael@0 474 // ok(ws.readyState == 2, "onmessage bad readyState in test-11!");
michael@0 475 }
michael@0 476 ws.onclose = function(e)
michael@0 477 {
michael@0 478 ok(ws.readyState == 3, "onclose bad readyState in test-11!");
michael@0 479 shouldCloseCleanly(e);
michael@0 480 ok(e.code == 1000, "test 11 got wrong close code: " + e.code);
michael@0 481 ok(e.reason == "Have a nice day", "test 11 got wrong close reason: " + e.reason);
michael@0 482 doTest(12);
michael@0 483 }
michael@0 484 }
michael@0 485
michael@0 486 function test12()
michael@0 487 {
michael@0 488 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-12");
michael@0 489 ws.onopen = function()
michael@0 490 {
michael@0 491 try {
michael@0 492 // send an unpaired surrogate
michael@0 493 ws._gotMessage = false;
michael@0 494 ws.send("a\ud800b");
michael@0 495 ok(true, "ok to send an unpaired surrogate");
michael@0 496 }
michael@0 497 catch (e) {
michael@0 498 ok(false, "shouldn't fail any more when sending an unpaired surrogate!");
michael@0 499 }
michael@0 500 }
michael@0 501
michael@0 502 ws.onmessage = function(msg)
michael@0 503 {
michael@0 504 ok(msg.data == "SUCCESS", "Unpaired surrogate in UTF-16 not converted in test-12");
michael@0 505 ws._gotMessage = true;
michael@0 506 // Must support unpaired surrogates in close reason, too
michael@0 507 ws.close(1000, "a\ud800b");
michael@0 508 }
michael@0 509
michael@0 510 ws.onclose = function(e)
michael@0 511 {
michael@0 512 ok(ws.readyState == 3, "onclose bad readyState in test-12!");
michael@0 513 ok(ws._gotMessage, "didn't receive message!");
michael@0 514 shouldCloseCleanly(e);
michael@0 515 ok(e.code == 1000, "test 12 got wrong close code: " + e.code);
michael@0 516 ok(e.reason == "a\ufffdb", "test 11 didn't get replacement char in close reason: " + e.reason);
michael@0 517 doTest(13);
michael@0 518 }
michael@0 519 }
michael@0 520
michael@0 521 function test13()
michael@0 522 {
michael@0 523 // previous versions of this test counted the number of protocol errors returned, but the
michael@0 524 // protocol stack typically closes down after reporting a protocol level error - trying
michael@0 525 // to resync is too dangerous
michael@0 526
michael@0 527 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-13");
michael@0 528 ws._timesCalledOnError = 0;
michael@0 529 ws.onerror = function()
michael@0 530 {
michael@0 531 ws._timesCalledOnError++;
michael@0 532 }
michael@0 533 ws.onclose = function(e)
michael@0 534 {
michael@0 535 ok(ws._timesCalledOnError > 0, "no error events");
michael@0 536 doTest(14);
michael@0 537 }
michael@0 538 }
michael@0 539
michael@0 540 function test14()
michael@0 541 {
michael@0 542 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-14");
michael@0 543 ws.onmessage = function()
michael@0 544 {
michael@0 545 ok(false, "shouldn't received message after the server sent the close frame");
michael@0 546 }
michael@0 547 ws.onclose = function(e)
michael@0 548 {
michael@0 549 shouldCloseCleanly(e);
michael@0 550 // Skip test 15 for now: broken
michael@0 551 doTest(16);
michael@0 552 };
michael@0 553 }
michael@0 554
michael@0 555 /*
michael@0 556 * DISABLED: see comments for test-15 case in file_websocket_wsh.py
michael@0 557 *
michael@0 558 function test15()
michael@0 559 {
michael@0 560 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-15");
michael@0 561 ws.onclose = function(e)
michael@0 562 {
michael@0 563 shouldCloseNotCleanly(e);
michael@0 564 doTest(16);
michael@0 565 };
michael@0 566
michael@0 567 // termination of the connection might cause an error event if it happens in OPEN
michael@0 568 ws.onerror = function()
michael@0 569 {
michael@0 570 }
michael@0 571
michael@0 572 }
michael@0 573 */
michael@0 574
michael@0 575 function test16()
michael@0 576 {
michael@0 577 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-16");
michael@0 578 ws.onopen = function()
michael@0 579 {
michael@0 580 ws.close();
michael@0 581 ok(!ws.send("client data"), "shouldn't send message after calling close()");
michael@0 582 }
michael@0 583 ws.onmessage = function()
michael@0 584 {
michael@0 585 ok(false, "shouldn't send message after calling close()");
michael@0 586 }
michael@0 587
michael@0 588 ws.onerror = function()
michael@0 589 {
michael@0 590 }
michael@0 591 ws.onclose = function()
michael@0 592 {
michael@0 593 doTest(17);
michael@0 594 }
michael@0 595 }
michael@0 596
michael@0 597 var status_test17 = "not started";
michael@0 598
michael@0 599 var waitTest17 = false;
michael@0 600
michael@0 601 var test17func = function()
michael@0 602 {
michael@0 603 waitTest17 = true;
michael@0 604
michael@0 605 var local_ws = new WebSocket("ws://sub1.test2.example.org/tests/content/base/test/file_websocket", "test-17");
michael@0 606 local_ws._testNumber = "local17";
michael@0 607 local_ws._testNumber = current_test++;
michael@0 608
michael@0 609 status_test17 = "started";
michael@0 610
michael@0 611 local_ws.onopen = function(e)
michael@0 612 {
michael@0 613 status_test17 = "opened";
michael@0 614 e.target.send("client data");
michael@0 615 forcegc();
michael@0 616 };
michael@0 617
michael@0 618 local_ws.onerror = function()
michael@0 619 {
michael@0 620 ok(false, "onerror called on test " + e.target._testNumber + "!");
michael@0 621 };
michael@0 622
michael@0 623 local_ws.onmessage = function(e)
michael@0 624 {
michael@0 625 ok(e.data == "server data", "Bad message in test-17");
michael@0 626 status_test17 = "got message";
michael@0 627 forcegc();
michael@0 628 };
michael@0 629
michael@0 630 local_ws.onclose = function(e)
michael@0 631 {
michael@0 632 ok(status_test17 == "got message", "Didn't got message in test-17!");
michael@0 633 shouldCloseCleanly(e);
michael@0 634 status_test17 = "closed";
michael@0 635 forcegc();
michael@0 636 waitTest17 = false;
michael@0 637 maybeFinished();
michael@0 638 };
michael@0 639
michael@0 640 local_ws = null;
michael@0 641 window._test17 = null;
michael@0 642 forcegc();
michael@0 643
michael@0 644 // do this in the background
michael@0 645 doTest(18);
michael@0 646 forcegc();
michael@0 647 }
michael@0 648
michael@0 649 function test17()
michael@0 650 {
michael@0 651 window._test17 = test17func;
michael@0 652 window._test17();
michael@0 653 }
michael@0 654
michael@0 655 // The tests that expects that their websockets neither open nor close MUST
michael@0 656 // be in the end of the tests, i.e. HERE, in order to prevent blocking the other
michael@0 657 // tests.
michael@0 658
michael@0 659 function test18()
michael@0 660 {
michael@0 661 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket_http_resource.txt");
michael@0 662 ws.onopen = shouldNotOpen;
michael@0 663 ws.onerror = ignoreError;
michael@0 664 ws.onclose = function(e)
michael@0 665 {
michael@0 666 shouldCloseNotCleanly(e);
michael@0 667 doTest(19);
michael@0 668 };
michael@0 669 }
michael@0 670
michael@0 671 function test19()
michael@0 672 {
michael@0 673 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-19");
michael@0 674 ws.onopen = shouldNotOpen;
michael@0 675 ws.onerror = ignoreError;
michael@0 676 ws.onclose = function(e)
michael@0 677 {
michael@0 678 shouldCloseNotCleanly(e);
michael@0 679 doTest(20);
michael@0 680 };
michael@0 681 }
michael@0 682
michael@0 683 var waitTest20 = false;
michael@0 684
michael@0 685 var test20func = function()
michael@0 686 {
michael@0 687 waitTest20 = true;
michael@0 688
michael@0 689 var local_ws = new WebSocket("ws://sub1.test1.example.org/tests/content/base/test/file_websocket", "test-20");
michael@0 690 local_ws._testNumber = "local20";
michael@0 691 local_ws._testNumber = current_test++;
michael@0 692
michael@0 693 local_ws.onerror = function()
michael@0 694 {
michael@0 695 ok(false, "onerror called on test " + e.target._testNumber + "!");
michael@0 696 };
michael@0 697
michael@0 698 local_ws.onclose = function(e)
michael@0 699 {
michael@0 700 ok(true, "test 20 closed despite gc");
michael@0 701 waitTest20 = false;
michael@0 702 maybeFinished();
michael@0 703 };
michael@0 704
michael@0 705 local_ws = null;
michael@0 706 window._test20 = null;
michael@0 707 forcegc();
michael@0 708
michael@0 709 // let test run in the background
michael@0 710 doTest(21);
michael@0 711 }
michael@0 712
michael@0 713 function test20()
michael@0 714 {
michael@0 715 window._test20 = test20func;
michael@0 716 window._test20();
michael@0 717 }
michael@0 718
michael@0 719 var waitTest21 = false;
michael@0 720
michael@0 721 test21func = function()
michael@0 722 {
michael@0 723 waitTest21 = true;
michael@0 724
michael@0 725 var local_ws = new WebSocket("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-21");
michael@0 726 local_ws._testNumber = current_test++;
michael@0 727 var received_message = false;
michael@0 728
michael@0 729 local_ws.onopen = function(e)
michael@0 730 {
michael@0 731 e.target.send("client data");
michael@0 732 forcegc();
michael@0 733 e.target.onopen = null;
michael@0 734 forcegc();
michael@0 735 };
michael@0 736
michael@0 737 local_ws.onerror = function()
michael@0 738 {
michael@0 739 ok(false, "onerror called on test " + e.target._testNumber + "!");
michael@0 740 };
michael@0 741
michael@0 742 local_ws.onmessage = function(e)
michael@0 743 {
michael@0 744 ok(e.data == "server data", "Bad message in test-21");
michael@0 745 received_message = true;
michael@0 746 forcegc();
michael@0 747 e.target.onmessage = null;
michael@0 748 forcegc();
michael@0 749 };
michael@0 750
michael@0 751 local_ws.onclose = function(e)
michael@0 752 {
michael@0 753 shouldCloseCleanly(e);
michael@0 754 ok(received_message, "close transitioned through onmessage");
michael@0 755 waitTest21 = false;
michael@0 756 maybeFinished();
michael@0 757 };
michael@0 758
michael@0 759 local_ws = null;
michael@0 760 window._test21 = null;
michael@0 761 forcegc();
michael@0 762
michael@0 763 doTest(22);
michael@0 764
michael@0 765 }
michael@0 766
michael@0 767 function test21()
michael@0 768 {
michael@0 769 window._test21 = test21func;
michael@0 770 window._test21();
michael@0 771 }
michael@0 772
michael@0 773 var waitTest22 = false;
michael@0 774
michael@0 775 function test22()
michael@0 776 {
michael@0 777 waitTest22 = true;
michael@0 778
michael@0 779 const pref_open = "network.websocket.timeout.open";
michael@0 780 SpecialPowers.setIntPref(pref_open, 5);
michael@0 781
michael@0 782 var ws = CreateTestWS("ws://sub2.test2.example.org/tests/content/base/test/file_websocket", "test-22");
michael@0 783 ws.onopen = shouldNotOpen;
michael@0 784 ws.onerror = ignoreError;
michael@0 785 ws.onclose = function(e)
michael@0 786 {
michael@0 787 shouldCloseNotCleanly(e);
michael@0 788 waitTest22 = false;
michael@0 789 maybeFinished();
michael@0 790 };
michael@0 791
michael@0 792 SpecialPowers.clearUserPref(pref_open);
michael@0 793 doTest(23);
michael@0 794 }
michael@0 795
michael@0 796 function test23()
michael@0 797 {
michael@0 798 current_test++;
michael@0 799 is(true, "WebSocket" in window, "WebSocket should be available on window object");
michael@0 800 doTest(24);
michael@0 801 }
michael@0 802
michael@0 803 function test24()
michael@0 804 {
michael@0 805 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-does-not-exist");
michael@0 806 ws.onopen = shouldNotOpen;
michael@0 807 ws.onclose = function(e)
michael@0 808 {
michael@0 809 shouldCloseNotCleanly(e);
michael@0 810 doTest(25);
michael@0 811 };
michael@0 812 ws.onerror = function()
michael@0 813 {
michael@0 814 }
michael@0 815 }
michael@0 816
michael@0 817 function test25()
michael@0 818 {
michael@0 819 var prots=[];
michael@0 820
michael@0 821 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 822
michael@0 823 // This test errors because the server requires a sub-protocol, but
michael@0 824 // the test just wants to ensure that the ctor doesn't generate an
michael@0 825 // exception
michael@0 826 ws.onerror = ignoreError;
michael@0 827 ws.onopen = shouldNotOpen;
michael@0 828
michael@0 829 ws.onclose = function(e)
michael@0 830 {
michael@0 831 ok(ws.protocol == "", "test25 subprotocol selection");
michael@0 832 ok(true, "test 25 protocol array close");
michael@0 833 doTest(26);
michael@0 834 };
michael@0 835 }
michael@0 836
michael@0 837 function test26()
michael@0 838 {
michael@0 839 var prots=[""];
michael@0 840
michael@0 841 try {
michael@0 842 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 843 ok(false, "testing empty element sub protocol array");
michael@0 844 }
michael@0 845 catch (e) {
michael@0 846 ok(true, "testing empty sub element protocol array");
michael@0 847 }
michael@0 848 doTest(27);
michael@0 849 }
michael@0 850
michael@0 851 function test27()
michael@0 852 {
michael@0 853 var prots=["test27", ""];
michael@0 854
michael@0 855 try {
michael@0 856 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 857 ok(false, "testing empty element mixed sub protocol array");
michael@0 858 }
michael@0 859 catch (e) {
michael@0 860 ok(true, "testing empty element mixed sub protocol array");
michael@0 861 }
michael@0 862 doTest(28);
michael@0 863 }
michael@0 864
michael@0 865 function test28()
michael@0 866 {
michael@0 867 var prots=["test28"];
michael@0 868
michael@0 869 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 870 ws.onopen = function(e)
michael@0 871 {
michael@0 872 ok(true, "test 28 protocol array open");
michael@0 873 ws.close();
michael@0 874 };
michael@0 875
michael@0 876 ws.onclose = function(e)
michael@0 877 {
michael@0 878 ok(ws.protocol == "test28", "test28 subprotocol selection");
michael@0 879 ok(true, "test 28 protocol array close");
michael@0 880 doTest(29);
michael@0 881 };
michael@0 882 }
michael@0 883
michael@0 884 function test29()
michael@0 885 {
michael@0 886 var prots=["test29a", "test29b"];
michael@0 887
michael@0 888 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 889 ws.onopen = function(e)
michael@0 890 {
michael@0 891 ok(true, "test 29 protocol array open");
michael@0 892 ws.close();
michael@0 893 };
michael@0 894
michael@0 895 ws.onclose = function(e)
michael@0 896 {
michael@0 897 ok(true, "test 29 protocol array close");
michael@0 898 doTest(30);
michael@0 899 };
michael@0 900 }
michael@0 901
michael@0 902 function test30()
michael@0 903 {
michael@0 904 var prots=["test-does-not-exist"];
michael@0 905 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 906
michael@0 907 ws.onopen = shouldNotOpen;
michael@0 908 ws.onclose = function(e)
michael@0 909 {
michael@0 910 shouldCloseNotCleanly(e);
michael@0 911 doTest(31);
michael@0 912 };
michael@0 913 ws.onerror = function()
michael@0 914 {
michael@0 915 }
michael@0 916 }
michael@0 917
michael@0 918 function test31()
michael@0 919 {
michael@0 920 var prots=["test-does-not-exist", "test31"];
michael@0 921 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 922
michael@0 923 ws.onopen = function(e)
michael@0 924 {
michael@0 925 ok(true, "test 31 protocol array open");
michael@0 926 ws.close();
michael@0 927 };
michael@0 928
michael@0 929 ws.onclose = function(e)
michael@0 930 {
michael@0 931 ok(ws.protocol == "test31", "test31 subprotocol selection");
michael@0 932 ok(true, "test 31 protocol array close");
michael@0 933 doTest(32);
michael@0 934 };
michael@0 935 }
michael@0 936
michael@0 937 function test32()
michael@0 938 {
michael@0 939 var prots=["test32","test32"];
michael@0 940
michael@0 941 try {
michael@0 942 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 943 ok(false, "testing duplicated element sub protocol array");
michael@0 944 }
michael@0 945 catch (e) {
michael@0 946 ok(true, "testing duplicated sub element protocol array");
michael@0 947 }
michael@0 948 doTest(33);
michael@0 949 }
michael@0 950
michael@0 951 function test33()
michael@0 952 {
michael@0 953 var prots=["test33"];
michael@0 954
michael@0 955 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 956 ws.onopen = function(e)
michael@0 957 {
michael@0 958 ok(true, "test 33 open");
michael@0 959 ws.close(3131); // pass code but not reason
michael@0 960 };
michael@0 961
michael@0 962 ws.onclose = function(e)
michael@0 963 {
michael@0 964 ok(true, "test 33 close");
michael@0 965 shouldCloseCleanly(e);
michael@0 966 ok(e.code == 3131, "test 33 got wrong close code: " + e.code);
michael@0 967 ok(e.reason === "", "test 33 got wrong close reason: " + e.reason);
michael@0 968 doTest(34);
michael@0 969 };
michael@0 970 }
michael@0 971
michael@0 972 function test34()
michael@0 973 {
michael@0 974 var prots=["test-34"];
michael@0 975
michael@0 976 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 977 ws.onopen = function(e)
michael@0 978 {
michael@0 979 ok(true, "test 34 open");
michael@0 980 ws.close();
michael@0 981 };
michael@0 982
michael@0 983 ws.onclose = function(e)
michael@0 984 {
michael@0 985 ok(true, "test 34 close");
michael@0 986 ok(e.wasClean, "test 34 closed cleanly");
michael@0 987 ok(e.code == 1001, "test 34 custom server code");
michael@0 988 ok(e.reason == "going away now", "test 34 custom server reason");
michael@0 989 doTest(35);
michael@0 990 };
michael@0 991 }
michael@0 992
michael@0 993 function test35()
michael@0 994 {
michael@0 995 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-35a");
michael@0 996
michael@0 997 ws.onopen = function(e)
michael@0 998 {
michael@0 999 ok(true, "test 35a open");
michael@0 1000 ws.close(3500, "my code");
michael@0 1001 };
michael@0 1002
michael@0 1003 ws.onclose = function(e)
michael@0 1004 {
michael@0 1005 ok(true, "test 35a close");
michael@0 1006 ok(e.wasClean, "test 35a closed cleanly");
michael@0 1007 current_test--; // CreateTestWS for 35a incremented this
michael@0 1008 var wsb = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-35b");
michael@0 1009
michael@0 1010 wsb.onopen = function(e)
michael@0 1011 {
michael@0 1012 ok(true, "test 35b open");
michael@0 1013 wsb.close();
michael@0 1014 };
michael@0 1015
michael@0 1016 wsb.onclose = function(e)
michael@0 1017 {
michael@0 1018 ok(true, "test 35b close");
michael@0 1019 ok(e.wasClean, "test 35b closed cleanly");
michael@0 1020 ok(e.code == 3501, "test 35 custom server code");
michael@0 1021 ok(e.reason == "my code", "test 35 custom server reason");
michael@0 1022 doTest(36);
michael@0 1023 };
michael@0 1024 }
michael@0 1025 }
michael@0 1026
michael@0 1027 function test36()
michael@0 1028 {
michael@0 1029 var prots=["test-36"];
michael@0 1030
michael@0 1031 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 1032 ws.onopen = function(e)
michael@0 1033 {
michael@0 1034 ok(true, "test 36 open");
michael@0 1035
michael@0 1036 try {
michael@0 1037 ws.close(13200);
michael@0 1038 ok(false, "testing custom close code out of range");
michael@0 1039 }
michael@0 1040 catch (e) {
michael@0 1041 ok(true, "testing custom close code out of range");
michael@0 1042 ws.close(3200);
michael@0 1043 }
michael@0 1044 };
michael@0 1045
michael@0 1046 ws.onclose = function(e)
michael@0 1047 {
michael@0 1048 ok(true, "test 36 close");
michael@0 1049 ok(e.wasClean, "test 36 closed cleanly");
michael@0 1050 doTest(37);
michael@0 1051 };
michael@0 1052 }
michael@0 1053
michael@0 1054 function test37()
michael@0 1055 {
michael@0 1056 var prots=["test-37"];
michael@0 1057
michael@0 1058 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 1059 ws.onopen = function(e)
michael@0 1060 {
michael@0 1061 ok(true, "test 37 open");
michael@0 1062
michael@0 1063 try {
michael@0 1064 ws.close(3100,"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123");
michael@0 1065 ok(false, "testing custom close reason out of range");
michael@0 1066 }
michael@0 1067 catch (e) {
michael@0 1068 ok(true, "testing custom close reason out of range");
michael@0 1069 ws.close(3100,"012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012");
michael@0 1070 }
michael@0 1071 };
michael@0 1072
michael@0 1073 ws.onclose = function(e)
michael@0 1074 {
michael@0 1075 ok(true, "test 37 close");
michael@0 1076 ok(e.wasClean, "test 37 closed cleanly");
michael@0 1077
michael@0 1078 current_test--; // CreateTestWS for 37 incremented this
michael@0 1079 var wsb = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-37b");
michael@0 1080
michael@0 1081 wsb.onopen = function(e)
michael@0 1082 {
michael@0 1083 // now test that a rejected close code and reason dont persist
michael@0 1084 ok(true, "test 37b open");
michael@0 1085 try {
michael@0 1086 wsb.close(3101,"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123");
michael@0 1087 ok(false, "testing custom close reason out of range 37b");
michael@0 1088 }
michael@0 1089 catch (e) {
michael@0 1090 ok(true, "testing custom close reason out of range 37b");
michael@0 1091 wsb.close();
michael@0 1092 }
michael@0 1093 }
michael@0 1094
michael@0 1095 wsb.onclose = function(e)
michael@0 1096 {
michael@0 1097 ok(true, "test 37b close");
michael@0 1098 ok(e.wasClean, "test 37b closed cleanly");
michael@0 1099
michael@0 1100 current_test--; // CreateTestWS for 37 incremented this
michael@0 1101 var wsc = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-37c");
michael@0 1102
michael@0 1103 wsc.onopen = function(e)
michael@0 1104 {
michael@0 1105 ok(true, "test 37c open");
michael@0 1106 wsc.close();
michael@0 1107 }
michael@0 1108
michael@0 1109 wsc.onclose = function(e)
michael@0 1110 {
michael@0 1111 ok(e.code != 3101, "test 37c custom server code not present");
michael@0 1112 ok(e.reason == "", "test 37c custom server reason not present");
michael@0 1113 doTest(38);
michael@0 1114 }
michael@0 1115 }
michael@0 1116 }
michael@0 1117 }
michael@0 1118
michael@0 1119 function test38()
michael@0 1120 {
michael@0 1121 var prots=["test-38"];
michael@0 1122
michael@0 1123 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 1124 ws.onopen = function(e)
michael@0 1125 {
michael@0 1126 ok(true, "test 38 open");
michael@0 1127 ok(ws.extensions != undefined, "extensions attribute defined");
michael@0 1128 // ok(ws.extensions == "deflate-stream", "extensions attribute deflate-stream");
michael@0 1129 ws.close();
michael@0 1130 };
michael@0 1131
michael@0 1132 ws.onclose = function(e)
michael@0 1133 {
michael@0 1134 ok(true, "test 38 close");
michael@0 1135 doTest(39);
michael@0 1136 };
michael@0 1137 }
michael@0 1138
michael@0 1139 function test39()
michael@0 1140 {
michael@0 1141 var prots=["test-39"];
michael@0 1142
michael@0 1143 var ws = CreateTestWS("wss://example.com/tests/content/base/test/file_websocket", prots);
michael@0 1144 status_test39 = "started";
michael@0 1145 ws.onopen = function(e)
michael@0 1146 {
michael@0 1147 status_test39 = "opened";
michael@0 1148 ok(true, "test 39 open");
michael@0 1149 ws.close();
michael@0 1150 };
michael@0 1151
michael@0 1152 ws.onclose = function(e)
michael@0 1153 {
michael@0 1154 ok(true, "test 39 close");
michael@0 1155 ok(status_test39 == "opened", "test 39 did open");
michael@0 1156 doTest(40);
michael@0 1157 };
michael@0 1158 }
michael@0 1159
michael@0 1160 function test40()
michael@0 1161 {
michael@0 1162 var prots=["test-40"];
michael@0 1163
michael@0 1164 var ws = CreateTestWS("wss://nocert.example.com/tests/content/base/test/file_websocket", prots);
michael@0 1165
michael@0 1166 status_test40 = "started";
michael@0 1167 ws.onerror = ignoreError;
michael@0 1168
michael@0 1169 ws.onopen = function(e)
michael@0 1170 {
michael@0 1171 status_test40 = "opened";
michael@0 1172 ok(false, "test 40 open");
michael@0 1173 ws.close();
michael@0 1174 };
michael@0 1175
michael@0 1176 ws.onclose = function(e)
michael@0 1177 {
michael@0 1178 ok(true, "test 40 close");
michael@0 1179 ok(status_test40 == "started", "test 40 did not open");
michael@0 1180 doTest(41);
michael@0 1181 };
michael@0 1182 }
michael@0 1183
michael@0 1184 function test41()
michael@0 1185 {
michael@0 1186 var ws = CreateTestWS("ws://example.com/tests/content/base/test/file_websocket", "test-41a", 1);
michael@0 1187
michael@0 1188 ws.onopen = function(e)
michael@0 1189 {
michael@0 1190 ok(true, "test 41a open");
michael@0 1191 ok(ws.url == "ws://example.com/tests/content/base/test/file_websocket",
michael@0 1192 "test 41a initial ws should not be redirected");
michael@0 1193 ws.close();
michael@0 1194 };
michael@0 1195
michael@0 1196 ws.onclose = function(e)
michael@0 1197 {
michael@0 1198 ok(true, "test 41a close");
michael@0 1199
michael@0 1200 // establish a hsts policy for example.com
michael@0 1201 var wsb = CreateTestWS("wss://example.com/tests/content/base/test/file_websocket", "test-41b", 1);
michael@0 1202 wsb.onopen = function(e)
michael@0 1203 {
michael@0 1204 ok(true, "test 41b open");
michael@0 1205 wsb.close();
michael@0 1206 }
michael@0 1207
michael@0 1208 wsb.onclose = function(e)
michael@0 1209 {
michael@0 1210 ok(true, "test 41b close");
michael@0 1211
michael@0 1212 // try ws:// again, it should be done over wss:// now due to hsts
michael@0 1213 var wsc = CreateTestWS("ws://example.com/tests/content/base/test/file_websocket", "test-41c");
michael@0 1214
michael@0 1215 wsc.onopen = function(e)
michael@0 1216 {
michael@0 1217 ok(true, "test 41c open");
michael@0 1218 ok(wsc.url == "wss://example.com/tests/content/base/test/file_websocket",
michael@0 1219 "test 41c ws should be redirected by hsts to wss");
michael@0 1220 wsc.close();
michael@0 1221 }
michael@0 1222
michael@0 1223 wsc.onclose = function(e)
michael@0 1224 {
michael@0 1225 ok(true, "test 41c close");
michael@0 1226
michael@0 1227 // clean up the STS state
michael@0 1228 const Cc = SpecialPowers.Cc;
michael@0 1229 const Ci = SpecialPowers.Ci;
michael@0 1230 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 1231 var thehost = ios.newURI("http://example.com", null, null);
michael@0 1232 var sss = Cc["@mozilla.org/ssservice;1"].getService(Ci.nsISiteSecurityService);
michael@0 1233 var loadContext = SpecialPowers.wrap(window)
michael@0 1234 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 1235 .getInterface(Ci.nsIWebNavigation)
michael@0 1236 .QueryInterface(Ci.nsILoadContext);
michael@0 1237 var flags = 0;
michael@0 1238 if (loadContext.usePrivateBrowsing)
michael@0 1239 flags |= Ci.nsISocketProvider.NO_PERMANENT_STORAGE;
michael@0 1240 sss.removeState(Ci.nsISiteSecurityService.HEADER_HSTS, thehost, flags);
michael@0 1241 doTest(42);
michael@0 1242 }
michael@0 1243 }
michael@0 1244 }
michael@0 1245 }
michael@0 1246
michael@0 1247 function test42()
michael@0 1248 {
michael@0 1249 // test some utf-8 non-characters. They should be allowed in the
michael@0 1250 // websockets context. Test via round trip echo.
michael@0 1251 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-42");
michael@0 1252 var data = ["U+FFFE \ufffe",
michael@0 1253 "U+FFFF \uffff",
michael@0 1254 "U+10FFFF \udbff\udfff"];
michael@0 1255 var index = 0;
michael@0 1256
michael@0 1257 ws.onopen = function()
michael@0 1258 {
michael@0 1259 ws.send(data[0]);
michael@0 1260 ws.send(data[1]);
michael@0 1261 ws.send(data[2]);
michael@0 1262 }
michael@0 1263
michael@0 1264 ws.onmessage = function(e)
michael@0 1265 {
michael@0 1266 ok(e.data == data[index], "bad received message in test-42! index="+index);
michael@0 1267 index++;
michael@0 1268 if (index == 3)
michael@0 1269 ws.close();
michael@0 1270 }
michael@0 1271
michael@0 1272 ws.onclose = function(e)
michael@0 1273 {
michael@0 1274 doTest(43);
michael@0 1275 }
michael@0 1276 }
michael@0 1277
michael@0 1278 function test43()
michael@0 1279 {
michael@0 1280 var prots=["test-43"];
michael@0 1281
michael@0 1282 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", prots);
michael@0 1283 ws.onopen = function(e)
michael@0 1284 {
michael@0 1285 ok(true, "test 43 open");
michael@0 1286 // Test binaryType setting
michael@0 1287 ws.binaryType = "arraybuffer";
michael@0 1288 ws.binaryType = "blob";
michael@0 1289 ws.binaryType = ""; // illegal
michael@0 1290 is(ws.binaryType, "blob");
michael@0 1291 ws.binaryType = "ArrayBuffer"; // illegal
michael@0 1292 is(ws.binaryType, "blob");
michael@0 1293 ws.binaryType = "Blob"; // illegal
michael@0 1294 is(ws.binaryType, "blob");
michael@0 1295 ws.binaryType = "mcfoofluu"; // illegal
michael@0 1296 is(ws.binaryType, "blob");
michael@0 1297 ws.close();
michael@0 1298 };
michael@0 1299
michael@0 1300 ws.onclose = function(e)
michael@0 1301 {
michael@0 1302 ok(true, "test 43 close");
michael@0 1303 doTest(44);
michael@0 1304 };
michael@0 1305 }
michael@0 1306
michael@0 1307
michael@0 1308 function test44()
michael@0 1309 {
michael@0 1310 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-44");
michael@0 1311 ok(ws.readyState == 0, "bad readyState in test-44!");
michael@0 1312 ws.binaryType = "arraybuffer";
michael@0 1313 ws.onopen = function()
michael@0 1314 {
michael@0 1315 ok(ws.readyState == 1, "open bad readyState in test-44!");
michael@0 1316 var buf = new ArrayBuffer(3);
michael@0 1317 // create byte view
michael@0 1318 var view = new Uint8Array(buf);
michael@0 1319 view[0] = 5;
michael@0 1320 view[1] = 0; // null byte
michael@0 1321 view[2] = 7;
michael@0 1322 ws.send(buf);
michael@0 1323 }
michael@0 1324 ws.onmessage = function(e)
michael@0 1325 {
michael@0 1326 ok(e.data instanceof ArrayBuffer, "Should receive an arraybuffer!");
michael@0 1327 var view = new Uint8Array(e.data);
michael@0 1328 ok(view.length == 2 && view[0] == 0 && view[1] ==4, "testing Reply arraybuffer" );
michael@0 1329 ws.close();
michael@0 1330 }
michael@0 1331 ws.onclose = function(e)
michael@0 1332 {
michael@0 1333 ok(ws.readyState == 3, "onclose bad readyState in test-44!");
michael@0 1334 shouldCloseCleanly(e);
michael@0 1335 doTest(45);
michael@0 1336 }
michael@0 1337 }
michael@0 1338
michael@0 1339 function createDOMFile(fileName, fileData)
michael@0 1340 {
michael@0 1341 // create File in profile dir
michael@0 1342 var dirSvc = SpecialPowers.Cc["@mozilla.org/file/directory_service;1"]
michael@0 1343 .getService(SpecialPowers.Ci.nsIProperties);
michael@0 1344 var testFile = dirSvc.get("ProfD", SpecialPowers.Ci.nsIFile);
michael@0 1345 testFile.append(fileName);
michael@0 1346 var outStream = SpecialPowers.Cc["@mozilla.org/network/file-output-stream;1"]
michael@0 1347 .createInstance(SpecialPowers.Ci.nsIFileOutputStream);
michael@0 1348 outStream.init(testFile, 0x02 | 0x08 | 0x20, 0666, 0);
michael@0 1349 outStream.write(fileData, fileData.length);
michael@0 1350 outStream.close();
michael@0 1351
michael@0 1352 // Set filename into DOM <input> field, as if selected by user
michael@0 1353 var fileList = document.getElementById('fileList');
michael@0 1354 SpecialPowers.wrap(fileList).value = testFile.path;
michael@0 1355
michael@0 1356 // return JS File object, aka Blob
michael@0 1357 return fileList.files[0];
michael@0 1358 }
michael@0 1359
michael@0 1360 function test45()
michael@0 1361 {
michael@0 1362 var blobFile = createDOMFile("testBlobFile", "flob");
michael@0 1363
michael@0 1364 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-45");
michael@0 1365 ok(ws.readyState == 0, "bad readyState in test-45!");
michael@0 1366
michael@0 1367 // ws.binaryType = "blob"; // Don't need to specify: blob is the default
michael@0 1368
michael@0 1369 ws.onopen = function()
michael@0 1370 {
michael@0 1371 ok(ws.readyState == 1, "open bad readyState in test-45!");
michael@0 1372 ws.send(blobFile);
michael@0 1373 }
michael@0 1374
michael@0 1375 var test45blob;
michael@0 1376
michael@0 1377 ws.onmessage = function(e)
michael@0 1378 {
michael@0 1379 test45blob = e.data;
michael@0 1380 ok(test45blob instanceof Blob, "We should be receiving a Blob");
michael@0 1381
michael@0 1382 ws.close();
michael@0 1383 }
michael@0 1384
michael@0 1385 ws.onclose = function(e)
michael@0 1386 {
michael@0 1387 ok(ws.readyState == 3, "onclose bad readyState in test-45!");
michael@0 1388 shouldCloseCleanly(e);
michael@0 1389
michael@0 1390 // check blob contents
michael@0 1391 var reader = new FileReader();
michael@0 1392 reader.onload = function(event)
michael@0 1393 {
michael@0 1394 ok(reader.result == "flob", "response should be 'flob': got '"
michael@0 1395 + reader.result + "'");
michael@0 1396 };
michael@0 1397 reader.onerror = function(event)
michael@0 1398 {
michael@0 1399 testFailed("Failed to read blob: error code = " + reader.error.code);
michael@0 1400 };
michael@0 1401 reader.onloadend = function(event)
michael@0 1402 {
michael@0 1403 doTest(46);
michael@0 1404 };
michael@0 1405
michael@0 1406 reader.readAsBinaryString(test45blob);
michael@0 1407 }
michael@0 1408 }
michael@0 1409
michael@0 1410 function test46()
michael@0 1411 {
michael@0 1412 var ws = CreateTestWS("ws://mochi.test:8888/tests/content/base/test/file_websocket", "test-46");
michael@0 1413 ok(ws.readyState == 0, "create bad readyState in test-46!");
michael@0 1414 ws.onopen = function()
michael@0 1415 {
michael@0 1416 ok(ws.readyState == 1, "open bad readyState in test-46!");
michael@0 1417 ws.close()
michael@0 1418 ok(ws.readyState == 2, "close must set readyState to 2 in test-46!");
michael@0 1419 }
michael@0 1420 ws.onmessage = function(e)
michael@0 1421 {
michael@0 1422 ok(false, "received message after calling close in test-46!");
michael@0 1423 }
michael@0 1424 ws.onclose = function(e)
michael@0 1425 {
michael@0 1426 ok(ws.readyState == 3, "onclose bad readyState in test-46!");
michael@0 1427 shouldCloseCleanly(e);
michael@0 1428 doTest(47);
michael@0 1429 }
michael@0 1430 }
michael@0 1431
michael@0 1432 function test47()
michael@0 1433 {
michael@0 1434 var hasError = false;
michael@0 1435 var ws = CreateTestWS("ws://another.websocket.server.that.probably.does.not.exist");
michael@0 1436 ws.onopen = shouldNotOpen;
michael@0 1437
michael@0 1438 ws.onerror = function (e)
michael@0 1439 {
michael@0 1440 ok(ws.readyState == 3, "test-47: readyState should be CLOSED(3) in onerror: got "
michael@0 1441 + ws.readyState);
michael@0 1442 ok(!ws._withinClose, "onerror() called during close()!");
michael@0 1443 hasError = true;
michael@0 1444 }
michael@0 1445
michael@0 1446 ws.onclose = function(e)
michael@0 1447 {
michael@0 1448 shouldCloseNotCleanly(e);
michael@0 1449 ok(hasError, "test-47: should have called onerror before onclose");
michael@0 1450 ok(ws.readyState == 3, "test-47: readyState should be CLOSED(3) in onclose: got "
michael@0 1451 + ws.readyState);
michael@0 1452 ok(!ws._withinClose, "onclose() called during close()!");
michael@0 1453 ok(e.code == 1006, "test-47 close code should be 1006 but is:" + e.code);
michael@0 1454 doTest(48);
michael@0 1455 };
michael@0 1456
michael@0 1457 // Call close before we're connected: throws error
michael@0 1458 // Make sure we call onerror/onclose asynchronously
michael@0 1459 ws._withinClose = 1;
michael@0 1460 ws.close(3333, "Closed before we were open: error");
michael@0 1461 ws._withinClose = 0;
michael@0 1462 ok(ws.readyState == 2, "test-47: readyState should be CLOSING(2) after close(): got "
michael@0 1463 + ws.readyState);
michael@0 1464 }
michael@0 1465
michael@0 1466
michael@0 1467 var ranAllTests = false;
michael@0 1468
michael@0 1469 function maybeFinished()
michael@0 1470 {
michael@0 1471 if (!ranAllTests)
michael@0 1472 return;
michael@0 1473
michael@0 1474 if (waitTest2Part1 || waitTest2Part2 || waitTest9 || waitTest10 ||
michael@0 1475 waitTest17 || waitTest20 || waitTest21 || waitTest22)
michael@0 1476 return;
michael@0 1477
michael@0 1478 for (i = 0; i < all_ws.length; ++i) {
michael@0 1479 if (all_ws[i] != shouldNotReceiveCloseEvent &&
michael@0 1480 !all_ws[i]._receivedCloseEvent) {
michael@0 1481 ok(false, "didn't called close on test " + all_ws[i]._testNumber + "!");
michael@0 1482 }
michael@0 1483 }
michael@0 1484
michael@0 1485 if (testsuite_iteration++ < testsuite_iterations) {
michael@0 1486 // play it again, Sam...
michael@0 1487 ok(1, "starting testsuite iteration " + testsuite_iteration);
michael@0 1488 test_started = new Array(last_test);
michael@0 1489 doTest(current_test = first_test);
michael@0 1490 } else {
michael@0 1491 // all done
michael@0 1492 SimpleTest.finish();
michael@0 1493 }
michael@0 1494 }
michael@0 1495
michael@0 1496 function testWebSocket ()
michael@0 1497 {
michael@0 1498 doTest(first_test);
michael@0 1499 }
michael@0 1500
michael@0 1501 SimpleTest.waitForExplicitFinish();
michael@0 1502
michael@0 1503 </script>
michael@0 1504 </pre>
michael@0 1505
michael@0 1506 <div id="feedback">
michael@0 1507 </div>
michael@0 1508
michael@0 1509 </body>
michael@0 1510 </html>

mercurial