tor-browser

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

test_simple.js (1603B)


      1 //
      2 //  Simple HTTP test: fetches page
      3 //
      4 
      5 // Note: sets Cc and Ci variables
      6 "use strict";
      7 
      8 const { HttpServer } = ChromeUtils.importESModule(
      9  "resource://testing-common/httpd.sys.mjs"
     10 );
     11 
     12 var httpserver = new HttpServer();
     13 var testpath = "/simple";
     14 var httpbody = "0123456789";
     15 
     16 var dbg = 0;
     17 if (dbg) {
     18  print("============== START ==========");
     19 }
     20 
     21 function run_test() {
     22  setup_test();
     23  do_test_pending();
     24 }
     25 
     26 function setup_test() {
     27  if (dbg) {
     28    print("============== setup_test: in");
     29  }
     30  httpserver.registerPathHandler(testpath, serverHandler);
     31  httpserver.start(-1);
     32  var channel = setupChannel(testpath);
     33  // ChannelListener defined in head_channels.js
     34  channel.asyncOpen(new ChannelListener(checkRequest, channel));
     35  if (dbg) {
     36    print("============== setup_test: out");
     37  }
     38 }
     39 
     40 function setupChannel(path) {
     41  var chan = NetUtil.newChannel({
     42    uri: "http://localhost:" + httpserver.identity.primaryPort + path,
     43    loadUsingSystemPrincipal: true,
     44  });
     45  chan.QueryInterface(Ci.nsIHttpChannel);
     46  chan.requestMethod = "GET";
     47  return chan;
     48 }
     49 
     50 function serverHandler(metadata, response) {
     51  if (dbg) {
     52    print("============== serverHandler: in");
     53  }
     54  response.setHeader("Content-Type", "text/plain", false);
     55  response.bodyOutputStream.write(httpbody, httpbody.length);
     56  if (dbg) {
     57    print("============== serverHandler: out");
     58  }
     59 }
     60 
     61 function checkRequest(request, data) {
     62  if (dbg) {
     63    print("============== checkRequest: in");
     64  }
     65  Assert.equal(data, httpbody);
     66  httpserver.stop(do_test_finished);
     67  if (dbg) {
     68    print("============== checkRequest: out");
     69  }
     70 }