toolkit/components/extensions/test/mochitest/test_ext_webrequest_websocket.html
author Shane Caraveo <scaraveo@mozilla.com>
Mon, 24 Jul 2017 17:00:29 -0700
changeset 422600 59e214139531f8aaf544d93f9fba0a77d6efa554
parent 413565 62e44a82730804257046e8fe9aafd8d53652d6ea
child 424096 1aadca44c4b72c06d18fc046b85444fc411d4980
permissions -rw-r--r--
Bug 1382834 fix ws/wss protocol matching, r=kmag MozReview-Commit-ID: 6IKTPA1sJgh

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
  <title>Basic websocket test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">
"use strict";

add_task(async function test_webSocket() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      "permissions": [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>",
      ],
    },
    background() {
      browser.webRequest.onBeforeRequest.addListener(details => {
        browser.test.assertEq("ws:", new URL(details.url).protocol, "ws protocol worked");
        browser.test.notifyPass("websocket");
      }, {urls: ["ws://mochi.test/*"]}, ["blocking"]);

      browser.test.onMessage.addListener(msg => {
        let ws = new WebSocket("ws://mochi.test:8888/tests/dom/base/test/file_websocket_hello");
        ws.onopen = (e) => {
          ws.send("data");
        };
        ws.onclose = (e) => {};
        ws.onerror = (e) => {};
        ws.onmessage = (e) => {
          ws.close();
        };
      });
      browser.test.sendMessage("ready");
    },
  });
  await extension.startup();
  await extension.awaitMessage("ready");
  extension.sendMessage("go");
  await extension.awaitFinish();
  await extension.unload();
});

</script>
</head>
<body>
</body>
</html>