tor-browser

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

browser_resources_network_event_stacktraces.js (2587B)


      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 ResourceCommand API around NETWORK_EVENT_STACKTRACE
      7 
      8 const TEST_URI = `${URL_ROOT_SSL}network_document.html`;
      9 
     10 const REQUEST_STUB = {
     11  code: `await fetch("/request_post_0.html", { method: "POST" });`,
     12  expected: {
     13    stacktraceAvailable: true,
     14    lastFrame: {
     15      filename:
     16        "https://example.com/browser/devtools/shared/commands/resource/tests/network_document.html",
     17      lineNumber: 1,
     18      columnNumber: 40,
     19      functionName: "triggerRequest",
     20      asyncCause: null,
     21    },
     22  },
     23 };
     24 
     25 add_task(async function () {
     26  info("Test network stacktraces events");
     27  const tab = await addTab(TEST_URI);
     28  const { client, resourceCommand, targetCommand } =
     29    await initResourceCommand(tab);
     30 
     31  const networkEvents = new Map();
     32  const stackTraces = new Map();
     33 
     34  function onResourceAvailable(resources) {
     35    for (const resource of resources) {
     36      if (
     37        resource.resourceType === resourceCommand.TYPES.NETWORK_EVENT_STACKTRACE
     38      ) {
     39        ok(
     40          !networkEvents.has(resource.resourceId),
     41          "The network event does not exist"
     42        );
     43 
     44        is(
     45          resource.stacktraceAvailable,
     46          REQUEST_STUB.expected.stacktraceAvailable,
     47          "The stacktrace is available"
     48        );
     49        is(
     50          JSON.stringify(resource.lastFrame),
     51          JSON.stringify(REQUEST_STUB.expected.lastFrame),
     52          "The last frame of the stacktrace is available"
     53        );
     54 
     55        stackTraces.set(resource.resourceId, true);
     56        return;
     57      }
     58 
     59      if (resource.resourceType === resourceCommand.TYPES.NETWORK_EVENT) {
     60        ok(
     61          stackTraces.has(resource.stacktraceResourceId),
     62          "The stack trace does exists"
     63        );
     64 
     65        networkEvents.set(resource.resourceId, true);
     66      }
     67    }
     68  }
     69 
     70  function onResourceUpdated() {}
     71 
     72  await resourceCommand.watchResources(
     73    [
     74      resourceCommand.TYPES.NETWORK_EVENT_STACKTRACE,
     75      resourceCommand.TYPES.NETWORK_EVENT,
     76    ],
     77    {
     78      onAvailable: onResourceAvailable,
     79      onUpdated: onResourceUpdated,
     80    }
     81  );
     82 
     83  await triggerNetworkRequests(tab.linkedBrowser, [REQUEST_STUB.code]);
     84 
     85  resourceCommand.unwatchResources(
     86    [
     87      resourceCommand.TYPES.NETWORK_EVENT_STACKTRACE,
     88      resourceCommand.TYPES.NETWORK_EVENT,
     89    ],
     90    {
     91      onAvailable: onResourceAvailable,
     92      onUpdated: onResourceUpdated,
     93    }
     94  );
     95 
     96  targetCommand.destroy();
     97  await client.close();
     98  BrowserTestUtils.removeTab(tab);
     99 });