tor-browser

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

browser_net_resend_headers.js (2751B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Test if custom request headers are not ignored (bug 1270096 and friends)
      8 */
      9 
     10 add_task(async function () {
     11  const { monitor } = await initNetMonitor(HTTPS_SIMPLE_SJS, {
     12    requestCount: 1,
     13  });
     14  info("Starting test... ");
     15 
     16  const { store, windowRequire, connector } = monitor.panelWin;
     17  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     18  const { requestData } = connector;
     19  const { getSortedRequests } = windowRequire(
     20    "devtools/client/netmonitor/src/selectors/index"
     21  );
     22 
     23  store.dispatch(Actions.batchEnable(false));
     24 
     25  const requestUrl = HTTPS_SIMPLE_SJS;
     26  const requestHeaders = [
     27    { name: "Host", value: "fakehost.example.com" },
     28    { name: "User-Agent", value: "Testzilla" },
     29    { name: "Referer", value: "https://example.com/referrer" },
     30    { name: "Accept", value: "application/jarda" },
     31    { name: "Accept-Encoding", value: "compress, identity, funcoding" },
     32    { name: "Accept-Language", value: "cs-CZ" },
     33  ];
     34 
     35  const wait = waitForNetworkEvents(monitor, 1);
     36  connector.networkCommand.sendHTTPRequest({
     37    url: requestUrl,
     38    method: "POST",
     39    headers: requestHeaders,
     40    body: "Hello",
     41    cause: {
     42      loadingDocumentUri: "https://example.com",
     43      stacktraceAvailable: true,
     44      type: "xhr",
     45    },
     46  });
     47  await wait;
     48 
     49  let item = getSortedRequests(store.getState())[0];
     50 
     51  ok(item.requestHeadersAvailable, "headers are available for lazily fetching");
     52 
     53  if (item.requestHeadersAvailable && !item.requestHeaders) {
     54    requestData(item.id, "requestHeaders");
     55  }
     56 
     57  // Wait until requestHeaders packet gets updated.
     58  await waitForRequestData(store, ["requestHeaders"]);
     59 
     60  item = getSortedRequests(store.getState())[0];
     61  is(item.method, "POST", "The request has the right method");
     62  is(item.url, requestUrl, "The request has the right URL");
     63 
     64  for (const { name, value } of item.requestHeaders.headers) {
     65    info(`Request header: ${name}: ${value}`);
     66  }
     67 
     68  function hasRequestHeader(name, value) {
     69    const { headers } = item.requestHeaders;
     70    return headers.some(h => h.name === name && h.value === value);
     71  }
     72 
     73  function hasNotRequestHeader(name) {
     74    const { headers } = item.requestHeaders;
     75    return headers.every(h => h.name !== name);
     76  }
     77 
     78  for (const { name, value } of requestHeaders) {
     79    ok(hasRequestHeader(name, value), `The ${name} header has the right value`);
     80  }
     81 
     82  // Check that the Cookie header was not added silently (i.e., that the request is
     83  // anonymous.
     84  for (const name of ["Cookie"]) {
     85    ok(hasNotRequestHeader(name), `The ${name} header is not present`);
     86  }
     87 
     88  return teardown(monitor);
     89 });