tor-browser

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

browser_jsterm_block_command.js (3069B)


      1 "use strict";
      2 
      3 const TEST_URI =
      4  "https://example.com/browser/devtools/client/webconsole/" +
      5  "test/browser/test-block-action.html";
      6 const TIMEOUT = "TIMEOUT";
      7 
      8 add_task(async function () {
      9  const hud = await openNewTabAndConsole(TEST_URI);
     10 
     11  ok(hud, "web console opened");
     12 
     13  const filter = "test-block-action-style.css";
     14  const blockCommand = `:block ${filter}`;
     15  const unblockCommand = `:unblock ${filter}`;
     16 
     17  info("Before blocking");
     18  await tryFetching();
     19  const resp1 = await waitFor(() => findConsoleAPIMessage(hud, "successful"));
     20  ok(resp1, "the request was not blocked");
     21  info(`Execute the :block command and try to do execute a network request`);
     22  await executeAndWaitForMessageByType(
     23    hud,
     24    blockCommand,
     25    "are now blocked",
     26    ".console-api"
     27  );
     28  await tryFetching();
     29 
     30  const resp2 = await waitFor(() => findConsoleAPIMessage(hud, "blocked"));
     31  ok(resp2, "the request was blocked as expected");
     32 
     33  info("Open netmonitor check the blocked filter is registered in its state");
     34  const { panelWin } = await openNetMonitor();
     35  const nmStore = panelWin.store;
     36  nmStore.dispatch(panelWin.actions.toggleRequestBlockingPanel());
     37  //await waitForTime(1e7);
     38  // wait until the blockedUrls property is populated
     39  await waitFor(() => !!nmStore.getState().requestBlocking.blockedUrls.length);
     40  const netMonitorState1 = nmStore.getState();
     41  is(
     42    netMonitorState1.requestBlocking.blockedUrls[0].url,
     43    filter,
     44    "blocked request shows up in netmonitor state"
     45  );
     46 
     47  info("Switch back to the console");
     48  await hud.toolbox.selectTool("webconsole");
     49 
     50  // :unblock
     51  await executeAndWaitForMessageByType(
     52    hud,
     53    unblockCommand,
     54    "Removed blocking",
     55    ".console-api"
     56  );
     57  info("After unblocking");
     58 
     59  const netMonitorState2 = nmStore.getState();
     60  is(
     61    netMonitorState2.requestBlocking.blockedUrls.length,
     62    0,
     63    "unblocked request should not be in netmonitor state"
     64  );
     65 
     66  await tryFetching();
     67 
     68  const resp3 = await waitFor(() => findConsoleAPIMessage(hud, "successful"));
     69  ok(resp3, "the request was not blocked");
     70 });
     71 
     72 async function tryFetching() {
     73  await SpecialPowers.spawn(
     74    gBrowser.selectedBrowser,
     75    [TIMEOUT],
     76    async function (timeoutStr) {
     77      const win = content.wrappedJSObject;
     78      const FETCH_URI =
     79        "https://example.com/browser/devtools/client/webconsole/" +
     80        "test/browser/test-block-action-style.css";
     81      const timeout = new Promise(res =>
     82        win.setTimeout(() => res(timeoutStr), 1000)
     83      );
     84      const fetchPromise = win.fetch(FETCH_URI);
     85 
     86      try {
     87        const resp = await Promise.race([fetchPromise, timeout]);
     88        if (typeof resp === "object") {
     89          // Request Promise
     90          win.console.log("the request was successful");
     91        } else if (resp === timeoutStr) {
     92          // Timeout
     93          win.console.log("the request was blocked");
     94        } else {
     95          win.console.error("Unkown response");
     96        }
     97      } catch {
     98        // NetworkError
     99        win.console.log("the request was blocked");
    100      }
    101    }
    102  );
    103 }