tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_worker_xhr_headers.html (2429B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <!DOCTYPE HTML>
      6 <html>
      7  <head>
      8    <title>Test for XHR Headers</title>
      9    <script src="/tests/SimpleTest/SimpleTest.js">
     10    </script>
     11    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
     12  </head>
     13  <body>
     14    <p id="display"></p>
     15    <div id="content" style="display: none"></div>
     16    <pre id="test">
     17      <script class="testbody">
     18 "use strict";
     19 
     20 SimpleTest.waitForExplicitFinish();
     21 
     22 var path =
     23  location.pathname.substring(0, location.pathname.lastIndexOf("/") + 1);
     24 var filenamePrefix = "worker_xhr_headers_";
     25 var serverFilename = filenamePrefix + "server.sjs";
     26 var workerFilename = filenamePrefix + "worker.js";
     27 var otherHost = "example.com";
     28 
     29 info("Informing server about the current host");
     30 
     31 var xhr = new XMLHttpRequest();
     32 xhr.open("POST", path + serverFilename);
     33 xhr.setRequestHeader("options-host", otherHost);
     34 xhr.setRequestHeader("empty", "");
     35 xhr.onreadystatechange = function() {
     36  if (xhr.readyState == 4) {
     37    info("Launching worker");
     38 
     39    var worker = new Worker(path + workerFilename);
     40    worker.postMessage("http://" + otherHost + path + serverFilename);
     41 
     42    worker.onmessage = function(event) {
     43      ok(event.data.response === "", "Worker responded, saw no response");
     44 
     45      var loopCount = 0;
     46 
     47      function checkServer() {
     48        var xhr2 = new XMLHttpRequest();
     49        xhr2.open("GET", path + serverFilename);
     50        xhr2.onreadystatechange = function() {
     51          if (xhr2.readyState == 4) {
     52            if (xhr2.responseText) {
     53              is(xhr2.responseText,
     54                 "Success: expected OPTIONS request with '" +
     55                 event.data.header + "' header",
     56                 "Server saw expected requests");
     57              SimpleTest.finish();
     58            } else if (++loopCount < 30) {
     59              setTimeout(checkServer, 1000);
     60            } else {
     61              ok(false, "Server never saw any requests");
     62              SimpleTest.finish();
     63            }
     64          }
     65        };
     66 
     67        info("Checking server status (" + loopCount + ")");
     68        xhr2.send();
     69      }
     70 
     71      checkServer();
     72    };
     73 
     74    worker.onerror = function(event) {
     75      ok(false, "Worker had an error: '" + event.message + "'");
     76      event.preventDefault();
     77      SimpleTest.finish();
     78    };
     79  }
     80 };
     81 xhr.send();
     82 
     83      </script>
     84    </pre>
     85  </body>
     86 </html>