content/base/test/test_websocket_basic.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_websocket_basic.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,255 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Basic WebSocket test</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.10 +</head>
    1.11 +
    1.12 +<body onload="testWebSocket()">
    1.13 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=472529">Mozilla Bug 472529</a>
    1.14 +<p id="display"></p>
    1.15 +<div id="content" style="display: none">
    1.16 +</div>
    1.17 +<pre id="test">
    1.18 +<script class="testbody" type="text/javascript">
    1.19 +
    1.20 +const kUrl = "ws://mochi.test:8888/tests/content/base/test/file_websocket_basic";
    1.21 +
    1.22 +var gTestElement;
    1.23 +var ws;
    1.24 +
    1.25 +function forcegc() {
    1.26 +  SpecialPowers.forceGC();
    1.27 +  SpecialPowers.gc();
    1.28 +}
    1.29 +
    1.30 +function testWebSocket() {
    1.31 +  gTestElement = document.getElementById("test");
    1.32 +
    1.33 +  SimpleTest.executeSoon(testWebSocket1);
    1.34 +}
    1.35 +
    1.36 +/**
    1.37 + * Sends message keywords, then receives their values.
    1.38 + */
    1.39 +function testWebSocket1() {
    1.40 +  gTestElement.textContent = "Running testWebSocket1()";
    1.41 +
    1.42 +  var results = ["test",
    1.43 +                 "/tests/content/base/test/file_websocket_basic",
    1.44 +                 "http://mochi.test:8888",
    1.45 +                 "end"];
    1.46 +
    1.47 +  ws = new WebSocket(kUrl, "test");
    1.48 +  is(ws.url, kUrl, "[1] WebSocket.url");
    1.49 +  ws.onopen = function(e) {
    1.50 +    const params = ["protocol", "resource", "origin", "end"];
    1.51 +
    1.52 +    gTestElement.textContent += "\nSending :";
    1.53 +    for (var i = 0; i < params.length; ++i) {
    1.54 +      gTestElement.textContent += " " + params[i];
    1.55 +      ws.send(params[i]);
    1.56 +    }
    1.57 +
    1.58 +    // Set this before onmessage() is called, so it is displayed once only.
    1.59 +    gTestElement.textContent += "\nReceived:";
    1.60 +  };
    1.61 +  ws.onclose = function(e) {
    1.62 +    is(results.length, 0, "[1] Number of unreceived messages");
    1.63 +    ok(e.wasClean, "[1] Connection closed cleanly");
    1.64 +
    1.65 +    SimpleTest.executeSoon(testWebSocket2);
    1.66 +  };
    1.67 +  ws.onerror = function(e) {
    1.68 +    ok(false, "[1] onerror() should not have been called!");
    1.69 +    gTestElement.textContent += "\nonerror() should not have been called!";
    1.70 +    SimpleTest.executeSoon(SimpleTest.finish);
    1.71 +  };
    1.72 +  ws.onmessage = function(e) {
    1.73 +    is(e.data, results[0], "[1] Received message");
    1.74 +    gTestElement.textContent += " " + e.data;
    1.75 +    results.shift();
    1.76 +  };
    1.77 +}
    1.78 +
    1.79 +/**
    1.80 + * Sends 1000+1 test messages, then receives them.
    1.81 + */
    1.82 +function testWebSocket2() {
    1.83 +  gTestElement.textContent = "Running testWebSocket2()";
    1.84 +
    1.85 +  const displayInterval = 100;
    1.86 +  const testCount = 1000;
    1.87 +  const testMessage = "test message 2.";
    1.88 +
    1.89 +  var messageCount = 0;
    1.90 +
    1.91 +  ws = new WebSocket(kUrl, "test");
    1.92 +  ws.onopen = function(e) {
    1.93 +    gTestElement.textContent += "\nSending :";
    1.94 +    for (var i = 1; i <= testCount; ++i) {
    1.95 +      if (i % displayInterval == 1) {
    1.96 +        gTestElement.textContent += " " + i;
    1.97 +      }
    1.98 +      ws.send(testMessage + i);
    1.99 +    }
   1.100 +    gTestElement.textContent += " end";
   1.101 +    ws.send("end");
   1.102 +
   1.103 +    // Set this before onmessage() is called, so it is displayed once only.
   1.104 +    gTestElement.textContent += "\nReceived:";
   1.105 +  };
   1.106 +  ws.onclose = function(e) {
   1.107 +    is(messageCount, testCount + 1, "[2] Number of received messages");
   1.108 +    ok(e.wasClean, "[2] Connection closed cleanly");
   1.109 +
   1.110 +    SimpleTest.executeSoon(testWebSocket3);
   1.111 +  };
   1.112 +  ws.onerror = function(e) {
   1.113 +    ok(false, "[2] onerror() should not have been called!");
   1.114 +    gTestElement.textContent += "\nonerror() should not have been called!";
   1.115 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.116 +  };
   1.117 +  ws.onmessage = function(e) {
   1.118 +    ++messageCount;
   1.119 +    if (messageCount > testCount)
   1.120 +      is(e.data, "end", "[2] Received message");
   1.121 +    else
   1.122 +      is(e.data, testMessage + messageCount, "[2] Received message");
   1.123 +    if (messageCount % displayInterval == 1) {
   1.124 +      gTestElement.textContent += " " + messageCount;
   1.125 +    }
   1.126 +  };
   1.127 +}
   1.128 +
   1.129 +/**
   1.130 + * Sends testcount+1 test messages, then receives them, calling forcegc() at each step.
   1.131 + */
   1.132 +function testWebSocket3() {
   1.133 +  gTestElement.textContent = "Running testWebSocket3() [can take a little while]";
   1.134 +
   1.135 +  const displayInterval = 10;
   1.136 +  const testCount = 10;
   1.137 +  const testMessage = "test message 3.";
   1.138 +
   1.139 +  var messageCount = 0;
   1.140 +
   1.141 +  ws = new WebSocket(kUrl, "test");
   1.142 +  // Set this before onopen() is called,
   1.143 +  // otherwise its display would be delayed by forcegc() calls...
   1.144 +  gTestElement.textContent += "\nSending :";
   1.145 +  ws.onopen = function(e) {
   1.146 +    for (var i = 1; i <= testCount; ++i) {
   1.147 +      forcegc();
   1.148 +      if (i % displayInterval == 1) {
   1.149 +        // Actual display is delayed by forcegc() calls...
   1.150 +        gTestElement.textContent += " " + i;
   1.151 +      }
   1.152 +      ws.send(testMessage + i);
   1.153 +    }
   1.154 +    forcegc();
   1.155 +    gTestElement.textContent += " end";
   1.156 +    ws.send("end");
   1.157 +
   1.158 +    // Set this before onmessage() is called, so it is displayed once only.
   1.159 +    gTestElement.textContent += "\nReceived:";
   1.160 +  };
   1.161 +  ws.onclose = function(e) {
   1.162 +    is(messageCount, testCount + 1, "[3] Number of received messages");
   1.163 +    ok(e.wasClean, "[3] Connection closed cleanly");
   1.164 +
   1.165 +    SimpleTest.executeSoon(testWebSocket4);
   1.166 +  };
   1.167 +  ws.onerror = function(e) {
   1.168 +    ok(false, "[3] onerror() should not have been called!");
   1.169 +    gTestElement.textContent += "\nonerror() should not have been called!";
   1.170 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.171 +  };
   1.172 +  ws.onmessage = function(e) {
   1.173 +    forcegc();
   1.174 +    ++messageCount;
   1.175 +    if (messageCount > testCount)
   1.176 +      is(e.data, "end", "[3] Received message");
   1.177 +    else
   1.178 +      is(e.data, testMessage + messageCount, "[3] Received message");
   1.179 +    if (messageCount % displayInterval == 1) {
   1.180 +      // Actual display is delayed by forcegc() call(s)...
   1.181 +      gTestElement.textContent += " " + messageCount;
   1.182 +    }
   1.183 +  };
   1.184 +}
   1.185 +
   1.186 +/**
   1.187 + * Sends a huge test message, then receives it, then closes the WebSocket from client-side.
   1.188 + */
   1.189 +function testWebSocket4() {
   1.190 +  gTestElement.textContent = "Running testWebSocket4()";
   1.191 +
   1.192 +  // String length = 13 + ((10,000 - 1) * 26) + 11 = 259,998 = almost 254 KiB.
   1.193 +  const longString = "messageStart " + new Array(10000).join(" -huge WebSocket message- ") + " messageEnd";
   1.194 +
   1.195 +  ws = new WebSocket(kUrl, "test");
   1.196 +  ws.onopen = function(e) {
   1.197 +    is(this, ws, "[4, onopen()] 'this' should point to the WebSocket.");
   1.198 +    gTestElement.textContent += "\nSending the huge message";
   1.199 +    ws.send(longString);
   1.200 +  };
   1.201 +  ws.onclose = function(e) {
   1.202 +    is(this, ws, "[4, onclose()] 'this' should point to the WebSocket.");
   1.203 +    ok(e.wasClean, "[4] Connection closed cleanly");
   1.204 +
   1.205 +    SimpleTest.executeSoon(testWebSocket5);
   1.206 +  };
   1.207 +  ws.onerror = function(e) {
   1.208 +    is(this, ws, "[4, onerror()] 'this' should point to the WebSocket.");
   1.209 +    ok(false, "[4, onerror()] should not have been called!");
   1.210 +    gTestElement.textContent += "\nonerror() should not have been called!";
   1.211 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.212 +  };
   1.213 +  ws.onmessage = function(e) {
   1.214 +    is(this, ws, "[4, onmessage()] 'this' should point to the WebSocket.");
   1.215 +    // Do not use |is(e.data, longString, "...");| that results in a _very_ long line.
   1.216 +    is(e.data.length, longString.length, "[4] Length of received message");
   1.217 +    ok(e.data == longString, "[4] Content of received message");
   1.218 +    gTestElement.textContent += "\nReceived the huge message";
   1.219 +    this.close();
   1.220 +  };
   1.221 +}
   1.222 +
   1.223 +/**
   1.224 + * Closes the WebSocket from client-side, then sends a test message that should be buffered.
   1.225 + */
   1.226 +function testWebSocket5() {
   1.227 +  gTestElement.textContent = "Running testWebSocket5()";
   1.228 +
   1.229 +  ws = new WebSocket(kUrl, "test");
   1.230 +  ws.onopen = function(e) {
   1.231 +    is(this.bufferedAmount, 0, "[5] Length of empty buffer before closing");
   1.232 +    this.close();
   1.233 +  };
   1.234 +  ws.onclose = function(e) {
   1.235 +    ok(e.wasClean, "[5] Connection closed cleanly");
   1.236 +    is(this.bufferedAmount, 0, "[5] Length of empty buffer after closing");
   1.237 +
   1.238 +    var msg = "test message to be buffered";
   1.239 +    this.send(msg);
   1.240 +    is(this.bufferedAmount, msg.length, "[5] Length of buffered message sent after closing");
   1.241 +
   1.242 +    gTestElement.textContent += "\ntestWebSocket5() completed";
   1.243 +
   1.244 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.245 +  };
   1.246 +  ws.onerror = function(e) {
   1.247 +    ok(false, "[5] onerror() should not have been called!");
   1.248 +    gTestElement.textContent += "\nonerror() should not have been called!";
   1.249 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.250 +  };
   1.251 +}
   1.252 +
   1.253 +SimpleTest.waitForExplicitFinish();
   1.254 +
   1.255 +</script>
   1.256 +</pre>
   1.257 +</body>
   1.258 +</html>

mercurial