tor-browser

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

browser_net_worker_stacks.js (3096B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that we get stack traces for the network requests made when starting or
      7 // running worker threads.
      8 
      9 const TOP_FILE_NAME = "html_worker-test-page.html";
     10 const TOP_URL = HTTPS_EXAMPLE_URL + TOP_FILE_NAME;
     11 const WORKER_FILE_NAME = "js_worker-test.js";
     12 const WORKER_URL = HTTPS_EXAMPLE_URL + WORKER_FILE_NAME;
     13 
     14 const EXPECTED_REQUESTS = [
     15  {
     16    method: "GET",
     17    url: TOP_URL,
     18    causeType: "document",
     19    causeUri: null,
     20    stack: false,
     21  },
     22  {
     23    method: "GET",
     24    url: WORKER_URL,
     25    causeType: "script",
     26    causeUri: TOP_URL,
     27    stack: [
     28      { fn: "startWorkerInner", file: TOP_URL, line: 11 },
     29      { fn: "startWorker", file: TOP_URL, line: 8 },
     30      { file: TOP_URL, line: 4 },
     31    ],
     32  },
     33  {
     34    method: "GET",
     35    url: HTTPS_EXAMPLE_URL + "missing1.js",
     36    causeType: "script",
     37    causeUri: TOP_URL,
     38    stack: [
     39      { fn: "importScriptsFromWorker", file: WORKER_URL, line: 14 },
     40      { file: WORKER_URL, line: 10 },
     41    ],
     42  },
     43  {
     44    method: "GET",
     45    url: HTTPS_EXAMPLE_URL + "missing2.js",
     46    causeType: "script",
     47    causeUri: TOP_URL,
     48    stack: [
     49      { fn: "importScriptsFromWorker", file: WORKER_URL, line: 14 },
     50      { file: WORKER_URL, line: 10 },
     51    ],
     52  },
     53  {
     54    method: "GET",
     55    url: HTTPS_EXAMPLE_URL + "js_worker-test2.js",
     56    causeType: "script",
     57    causeUri: TOP_URL,
     58    stack: [
     59      { fn: "startWorkerFromWorker", file: WORKER_URL, line: 7 },
     60      { file: WORKER_URL, line: 3 },
     61    ],
     62  },
     63  {
     64    method: "GET",
     65    url: HTTPS_EXAMPLE_URL + "missing.json",
     66    causeType: "xhr",
     67    causeUri: TOP_URL,
     68    stack: [
     69      { fn: "createJSONRequest", file: WORKER_URL, line: 22 },
     70      { file: WORKER_URL, line: 18 },
     71    ],
     72  },
     73  {
     74    method: "GET",
     75    url: HTTPS_EXAMPLE_URL + "missing.txt",
     76    causeType: "fetch",
     77    causeUri: TOP_URL,
     78    stack: [
     79      { fn: "fetchThing", file: WORKER_URL, line: 29 },
     80      { file: WORKER_URL, line: 26 },
     81    ],
     82  },
     83 ];
     84 
     85 add_task(async function () {
     86  // Load a different URL first to instantiate the network monitor before we
     87  // load the page we're really interested in.
     88  const { monitor } = await initNetMonitor(SIMPLE_URL, { requestCount: 1 });
     89 
     90  const { store, windowRequire, connector } = monitor.panelWin;
     91  const { getSortedRequests } = windowRequire(
     92    "devtools/client/netmonitor/src/selectors/index"
     93  );
     94 
     95  const onNetworkEvents = waitForNetworkEvents(
     96    monitor,
     97    EXPECTED_REQUESTS.length
     98  );
     99  await navigateTo(TOP_URL);
    100  await onNetworkEvents;
    101 
    102  is(
    103    store.getState().requests.requests.length,
    104    EXPECTED_REQUESTS.length,
    105    "All the page events should be recorded."
    106  );
    107 
    108  // Wait for stack traces from all requests.
    109  const requests = getSortedRequests(store.getState());
    110  await Promise.all(
    111    requests.map(requestItem =>
    112      connector.requestData(requestItem.id, "stackTrace")
    113    )
    114  );
    115 
    116  await validateRequests(EXPECTED_REQUESTS, monitor, {
    117    allowDifferentOrder: true,
    118  });
    119 
    120  await teardown(monitor);
    121 });