tor-browser

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

test_channel_close.js (1904B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 var httpProtocolHandler = Cc[
      8  "@mozilla.org/network/protocol;1?name=http"
      9 ].getService(Ci.nsIHttpProtocolHandler);
     10 
     11 ChromeUtils.defineLazyGetter(this, "URL", function () {
     12  return "http://localhost:" + httpserver.identity.primaryPort;
     13 });
     14 
     15 var httpserver = new HttpServer();
     16 var testpath = "/simple";
     17 var httpbody = "0123456789";
     18 
     19 var live_channels = [];
     20 
     21 add_task(async function test() {
     22  httpserver.registerPathHandler(testpath, serverHandler);
     23  httpserver.start(-1);
     24  registerCleanupFunction(async () => {
     25    if (httpserver) {
     26      await httpserver.stop();
     27    }
     28  });
     29 
     30  await httpProtocolHandler.EnsureHSTSDataReady();
     31 
     32  // Opened channel that has no remaining references on shutdown
     33  let local_channel = setupChannel(testpath);
     34  local_channel.asyncOpen(new SimpleChannelListener());
     35 
     36  // Opened channel that has no remaining references after being opened
     37  setupChannel(testpath).asyncOpen(new SimpleChannelListener());
     38 
     39  // Unopened channel that has remaining references on shutdown
     40  live_channels.push(setupChannel(testpath));
     41 
     42  // Opened channel that has remaining references on shutdown
     43  live_channels.push(setupChannel(testpath));
     44  await new Promise(resolve => {
     45    live_channels[1].asyncOpen(
     46      new SimpleChannelListener((req, data) => {
     47        Assert.equal(data, httpbody);
     48        resolve();
     49      })
     50    );
     51  });
     52 
     53  await httpserver.stop();
     54  httpserver = null;
     55 });
     56 
     57 function setupChannel(path) {
     58  var chan = NetUtil.newChannel({
     59    uri: URL + path,
     60    loadUsingSystemPrincipal: true,
     61  });
     62  chan.QueryInterface(Ci.nsIHttpChannel);
     63  chan.requestMethod = "GET";
     64  return chan;
     65 }
     66 
     67 function serverHandler(metadata, response) {
     68  response.setHeader("Content-Type", "text/plain", false);
     69  response.bodyOutputStream.write(httpbody, httpbody.length);
     70 }