tor-browser

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

browser_net_block.js (5472B)


      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 blocking and unblocking a request.
      8 */
      9 
     10 add_task(async function () {
     11  const { monitor, tab } = await initNetMonitor(HTTPS_SIMPLE_URL, {
     12    requestCount: 1,
     13  });
     14  info("Starting test... ");
     15 
     16  const { document, store, windowRequire } = monitor.panelWin;
     17  const { getSelectedRequest } = windowRequire(
     18    "devtools/client/netmonitor/src/selectors/index"
     19  );
     20  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     21  store.dispatch(Actions.batchEnable(false));
     22 
     23  // Reload to have one request in the list
     24  let waitForEvents = waitForNetworkEvents(monitor, 1);
     25  await navigateTo(HTTPS_SIMPLE_URL);
     26  await waitForEvents;
     27 
     28  // Capture normal request
     29  let normalRequestState;
     30  let normalRequestSize;
     31  let normalHeadersSectionSize;
     32  let normalFirstHeaderSectionTitle;
     33  {
     34    // Wait for the response and request header sections
     35    const waitForHeaderSections = waitForDOM(
     36      document,
     37      "#headers-panel .accordion-item",
     38      2
     39    );
     40 
     41    const firstRequest = document.querySelectorAll(".request-list-item")[0];
     42    EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
     43 
     44    await waitForHeaderSections;
     45 
     46    const headerSections = document.querySelectorAll(
     47      "#headers-panel .accordion-item"
     48    );
     49    normalRequestState = getSelectedRequest(store.getState());
     50    normalRequestSize = firstRequest.querySelector(
     51      ".requests-list-transferred"
     52    ).textContent;
     53    normalHeadersSectionSize = headerSections.length;
     54    normalFirstHeaderSectionTitle = headerSections[0].querySelector(
     55      ".accordion-header-label"
     56    ).textContent;
     57 
     58    info("Captured normal request");
     59 
     60    // Mark as blocked
     61    EventUtils.sendMouseEvent({ type: "contextmenu" }, firstRequest);
     62    const onRequestBlocked = waitForDispatch(
     63      store,
     64      "REQUEST_BLOCKING_UPDATE_COMPLETE"
     65    );
     66 
     67    await selectContextMenuItem(monitor, "request-list-context-block-url");
     68 
     69    info("Wait for selected request to be blocked");
     70    await onRequestBlocked;
     71    info("Selected request is now blocked");
     72  }
     73 
     74  // Reload to have one request in the list
     75  info("Reloading to check block");
     76  // We can't use the normal waiting methods because a canceled request won't send all
     77  // the extra update packets.
     78  waitForEvents = waitForNetworkEvents(monitor, 1);
     79  tab.linkedBrowser.reload();
     80  await waitForEvents;
     81 
     82  // Capture blocked request, then unblock
     83  let blockedRequestState;
     84  let blockedRequestSize;
     85  let blockedHeadersSectionSize;
     86  let blockedFirstHeaderSectionTitle;
     87  {
     88    const waitForHeaderSections = waitForDOM(
     89      document,
     90      "#headers-panel .accordion-item",
     91      1
     92    );
     93 
     94    const firstRequest = document.querySelectorAll(".request-list-item")[0];
     95    EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
     96 
     97    await waitForHeaderSections;
     98 
     99    const headerSections = document.querySelectorAll(
    100      "#headers-panel .accordion-item"
    101    );
    102    blockedRequestSize = firstRequest.querySelector(
    103      ".requests-list-transferred"
    104    ).textContent;
    105    blockedRequestState = getSelectedRequest(store.getState());
    106    blockedHeadersSectionSize = headerSections.length;
    107    blockedFirstHeaderSectionTitle = headerSections[0].querySelector(
    108      ".accordion-header-label"
    109    ).textContent;
    110 
    111    info("Captured blocked request");
    112 
    113    // Mark as unblocked
    114    EventUtils.sendMouseEvent({ type: "contextmenu" }, firstRequest);
    115    const onRequestUnblocked = waitForDispatch(
    116      store,
    117      "REQUEST_BLOCKING_UPDATE_COMPLETE"
    118    );
    119 
    120    await selectContextMenuItem(monitor, "request-list-context-unblock-url");
    121 
    122    info("Wait for selected request to be unblocked");
    123    await onRequestUnblocked;
    124    info("Selected request is now unblocked");
    125  }
    126 
    127  // Reload to have one request in the list
    128  info("Reloading to check unblock");
    129  waitForEvents = waitForNetworkEvents(monitor, 1);
    130  await reloadBrowser();
    131  await waitForEvents;
    132 
    133  // Capture unblocked request
    134  let unblockedRequestState;
    135  let unblockedRequestSize;
    136  {
    137    const firstRequest = document.querySelectorAll(".request-list-item")[0];
    138    unblockedRequestSize = firstRequest.querySelector(
    139      ".requests-list-transferred"
    140    ).textContent;
    141    EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
    142    unblockedRequestState = getSelectedRequest(store.getState());
    143    info("Captured unblocked request");
    144  }
    145 
    146  ok(!normalRequestState.blockedReason, "Normal request is not blocked");
    147  ok(!normalRequestSize.includes("Blocked"), "Normal request has a size");
    148  Assert.equal(normalHeadersSectionSize, 2, "Both header sections are showing");
    149  ok(
    150    normalFirstHeaderSectionTitle.includes("Response"),
    151    "The response header section is visible for normal requests"
    152  );
    153 
    154  ok(blockedRequestState.blockedReason, "Blocked request is blocked");
    155  ok(
    156    blockedRequestSize.includes("Blocked"),
    157    "Blocked request shows reason as size"
    158  );
    159  Assert.equal(
    160    blockedHeadersSectionSize,
    161    1,
    162    "Only one header section is showing"
    163  );
    164  ok(
    165    blockedFirstHeaderSectionTitle.includes("Request"),
    166    "The response header section is not visible for blocked requests"
    167  );
    168 
    169  ok(!unblockedRequestState.blockedReason, "Unblocked request is not blocked");
    170  ok(!unblockedRequestSize.includes("Blocked"), "Unblocked request has a size");
    171 
    172  return teardown(monitor);
    173 });