tor-browser

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

browser_network_command_sendHTTPRequest.js (2435B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the NetworkCommand's sendHTTPRequest
      7 
      8 add_task(async function () {
      9  info("Test NetworkCommand.sendHTTPRequest");
     10  const tab = await addTab("data:text/html,foo");
     11  const commands = await CommandsFactory.forTab(tab);
     12 
     13  // We have to ensure TargetCommand is initialized to have access to the top level target
     14  // from NetworkCommand.sendHTTPRequest
     15  await commands.targetCommand.startListening();
     16 
     17  const { networkCommand } = commands;
     18 
     19  const httpServer = createTestHTTPServer();
     20  const onRequest = new Promise(resolve => {
     21    httpServer.registerPathHandler(
     22      "/http-request.html",
     23      (request, response) => {
     24        response.setStatusLine(request.httpVersion, 200, "OK");
     25        response.write("Response body");
     26        resolve(request);
     27      }
     28    );
     29  });
     30  const url = `http://localhost:${httpServer.identity.primaryPort}/http-request.html`;
     31 
     32  info("Call NetworkCommand.sendHTTPRequest");
     33  const { resourceCommand } = commands;
     34  const { onResource } = await resourceCommand.waitForNextResource(
     35    resourceCommand.TYPES.NETWORK_EVENT
     36  );
     37  const { channelId } = await networkCommand.sendHTTPRequest({
     38    url,
     39    method: "POST",
     40    headers: [{ name: "Request", value: "Header" }],
     41    body: "Hello",
     42    cause: {
     43      loadingDocumentUri: "https://example.com",
     44      stacktraceAvailable: true,
     45      type: "xhr",
     46    },
     47  });
     48  ok(channelId, "Received a channel id in response");
     49  const resource = await onResource;
     50  is(
     51    resource.resourceId,
     52    channelId,
     53    "NETWORK_EVENT resource channelId is the same as the one returned by sendHTTPRequest"
     54  );
     55 
     56  const request = await onRequest;
     57  is(request.method, "POST", "Request method is correct");
     58  is(request.getHeader("Request"), "Header", "The custom header was passed");
     59  is(fetchRequestBody(request), "Hello", "The request POST's body is correct");
     60 
     61  await commands.destroy();
     62 });
     63 
     64 const BinaryInputStream = Components.Constructor(
     65  "@mozilla.org/binaryinputstream;1",
     66  "nsIBinaryInputStream",
     67  "setInputStream"
     68 );
     69 
     70 function fetchRequestBody(request) {
     71  let body = "";
     72  const bodyStream = new BinaryInputStream(request.bodyInputStream);
     73  let avail = 0;
     74  while ((avail = bodyStream.available()) > 0) {
     75    body += String.fromCharCode.apply(String, bodyStream.readByteArray(avail));
     76  }
     77  return body;
     78 }