tor-browser

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

browser_net_copy_headers.js (3251B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Tests if copying a request's request/response headers works.
      8 */
      9 
     10 add_task(async function () {
     11  const { monitor } = await initNetMonitor(SIMPLE_URL, {
     12    requestCount: 1,
     13  });
     14  info("Starting test... ");
     15 
     16  const { document, store, windowRequire } = monitor.panelWin;
     17  const { getSortedRequests, getSelectedRequest } = windowRequire(
     18    "devtools/client/netmonitor/src/selectors/index"
     19  );
     20 
     21  const wait = waitForNetworkEvents(monitor, 1);
     22  await reloadBrowser();
     23  await wait;
     24 
     25  EventUtils.sendMouseEvent(
     26    { type: "mousedown" },
     27    document.querySelectorAll(".request-list-item")[0]
     28  );
     29 
     30  const requestItem = getSortedRequests(store.getState())[0];
     31  const { method, httpVersion, status, statusText } = requestItem;
     32 
     33  EventUtils.sendMouseEvent(
     34    { type: "contextmenu" },
     35    document.querySelectorAll(".request-list-item")[0]
     36  );
     37 
     38  const selectedRequest = getSelectedRequest(store.getState());
     39  is(selectedRequest, requestItem, "Proper request is selected");
     40 
     41  const EXPECTED_REQUEST_HEADERS = [
     42    `${method} ${SIMPLE_URL.split("example.com")[1]} ${httpVersion}`,
     43    "Host: example.com",
     44    "User-Agent: " + navigator.userAgent + "",
     45    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
     46    "Accept-Language: " + navigator.languages.join(",") + ";q=0.9",
     47    "Accept-Encoding: gzip, deflate",
     48    "Connection: keep-alive",
     49    "Upgrade-Insecure-Requests: 1",
     50    "Priority: u=0, i",
     51    "Pragma: no-cache",
     52    "Cache-Control: no-cache",
     53  ].join("\n");
     54 
     55  await waitForClipboardPromise(
     56    async function setup() {
     57      await selectContextMenuItem(
     58        monitor,
     59        "request-list-context-copy-request-headers"
     60      );
     61    },
     62    function validate(result) {
     63      // Sometimes, a "Cookie" header is left over from other tests. Remove it:
     64      result = String(result).replace(/Cookie: [^\n]+\n/, "");
     65      return result === EXPECTED_REQUEST_HEADERS;
     66    }
     67  );
     68  info("Clipboard contains the currently selected item's request headers.");
     69 
     70  const EXPECTED_RESPONSE_HEADERS = [
     71    `${httpVersion} ${status} ${statusText}`,
     72    "last-modified: Sun, 3 May 2015 11:11:11 GMT",
     73    "content-type: text/html",
     74    "content-length: 465",
     75    "connection: close",
     76    "server: httpd.js",
     77    "date: Sun, 3 May 2015 11:11:11 GMT",
     78  ].join("\n");
     79 
     80  EventUtils.sendMouseEvent(
     81    { type: "contextmenu" },
     82    document.querySelectorAll(".request-list-item")[0]
     83  );
     84 
     85  await waitForClipboardPromise(
     86    async function setup() {
     87      await selectContextMenuItem(
     88        monitor,
     89        "response-list-context-copy-response-headers"
     90      );
     91    },
     92    function validate(result) {
     93      // Fake the "Last-Modified" and "Date" headers because they will vary:
     94      result = String(result)
     95        .replace(
     96          /last-modified: [^\n]+ GMT/,
     97          "last-modified: Sun, 3 May 2015 11:11:11 GMT"
     98        )
     99        .replace(/date: [^\n]+ GMT/, "date: Sun, 3 May 2015 11:11:11 GMT");
    100      return result === EXPECTED_RESPONSE_HEADERS;
    101    }
    102  );
    103  info("Clipboard contains the currently selected item's response headers.");
    104 
    105  await teardown(monitor);
    106 });