tor-browser

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

browser_net_open_request_in_tab.js (7565B)


      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 Open in new tab works by ContextMenu.
      8 */
      9 
     10 add_task(async function () {
     11  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
     12    requestCount: 1,
     13  });
     14  info("Starting test...");
     15 
     16  const { document, store, windowRequire } = monitor.panelWin;
     17  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     18  let newTab;
     19 
     20  store.dispatch(Actions.batchEnable(false));
     21 
     22  // Post data may be fetched by the Header panel,
     23  // so set the Timings panel as the new default.
     24  store.getState().ui.detailsPanelSelectedTab = "timings";
     25 
     26  // Open GET request in new tab
     27  await performRequest(monitor, tab, "GET");
     28  newTab = await openLastRequestInTab();
     29  await checkTabResponse(newTab, "GET");
     30  gBrowser.removeCurrentTab();
     31 
     32  // Open POST request in new tab
     33  await performRequest(
     34    monitor,
     35    tab,
     36    "POST",
     37    "application/x-www-form-urlencoded",
     38    "foo=bar&baz=42"
     39  );
     40  newTab = await openLastRequestInTab();
     41  await checkTabResponse(
     42    newTab,
     43    "POST",
     44    "application/x-www-form-urlencoded",
     45    "foo=bar&baz=42"
     46  );
     47  gBrowser.removeCurrentTab();
     48 
     49  // Open POST application/json request in new tab
     50  await performRequest(
     51    monitor,
     52    tab,
     53    "POST",
     54    "application/json",
     55    '{"foo":"bar"}'
     56  );
     57  newTab = await openLastRequestInTab();
     58  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
     59  gBrowser.removeCurrentTab();
     60 
     61  await teardown(monitor);
     62 
     63  // OpenLastRequestInTab by ContextMenu
     64  async function openLastRequestInTab() {
     65    const requestItems = document.querySelectorAll(".request-list-item");
     66    const lastRequest = requestItems[requestItems.length - 1];
     67    EventUtils.sendMouseEvent({ type: "mousedown" }, lastRequest);
     68    EventUtils.sendMouseEvent({ type: "contextmenu" }, lastRequest);
     69 
     70    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
     71    await selectContextMenuItem(monitor, "request-list-context-newtab");
     72    await onTabOpen;
     73    info("A new tab has been opened");
     74 
     75    const awaitedTab = gBrowser.selectedTab;
     76    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
     77    info("The tab load completed");
     78 
     79    return awaitedTab;
     80  }
     81 });
     82 
     83 /**
     84 * Tests if Open in new tab works by DoubleClick RequestItem.
     85 */
     86 
     87 add_task(async function () {
     88  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
     89    requestCount: 1,
     90  });
     91  info("Starting test...");
     92 
     93  const { document, store, windowRequire } = monitor.panelWin;
     94  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     95  let newTab;
     96 
     97  store.dispatch(Actions.batchEnable(false));
     98 
     99  // Post data may be fetched by the Header panel,
    100  // so set the Timings panel as the new default.
    101  store.getState().ui.detailsPanelSelectedTab = "timings";
    102 
    103  // Open GET request in new tab
    104  await performRequest(monitor, tab, "GET");
    105  newTab = await openLastRequestInTab();
    106  await checkTabResponse(newTab, "GET");
    107  gBrowser.removeCurrentTab();
    108 
    109  // Open POST request in new tab
    110  await performRequest(
    111    monitor,
    112    tab,
    113    "POST",
    114    "application/x-www-form-urlencoded",
    115    "foo=bar&baz=42"
    116  );
    117  newTab = await openLastRequestInTab();
    118  await checkTabResponse(
    119    newTab,
    120    "POST",
    121    "application/x-www-form-urlencoded",
    122    "foo=bar&baz=42"
    123  );
    124  gBrowser.removeCurrentTab();
    125 
    126  // Open POST application/json request in new tab
    127  await performRequest(
    128    monitor,
    129    tab,
    130    "POST",
    131    "application/json",
    132    '{"foo":"bar"}'
    133  );
    134  newTab = await openLastRequestInTab();
    135  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
    136  gBrowser.removeCurrentTab();
    137 
    138  await teardown(monitor);
    139 
    140  // OpenLastRequestInTab by DoubleClick
    141  async function openLastRequestInTab() {
    142    const requestItems = document.querySelectorAll(".request-list-item");
    143    const lastRequest = requestItems[requestItems.length - 1];
    144 
    145    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
    146    EventUtils.sendMouseEvent({ type: "dblclick" }, lastRequest);
    147    await onTabOpen;
    148    info("A new tab has been opened");
    149 
    150    const awaitedTab = gBrowser.selectedTab;
    151    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
    152    info("The tab load completed");
    153 
    154    return awaitedTab;
    155  }
    156 });
    157 
    158 /**
    159 * Tests if Open in new tab works by middle click RequestItem.
    160 */
    161 
    162 add_task(async function () {
    163  const { tab, monitor } = await initNetMonitor(OPEN_REQUEST_IN_TAB_URL, {
    164    requestCount: 1,
    165  });
    166  const MIDDLE_MOUSE_BUTTON = 1;
    167  info("Starting test...");
    168 
    169  const { document, store, windowRequire } = monitor.panelWin;
    170  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
    171  let newTab;
    172 
    173  store.dispatch(Actions.batchEnable(false));
    174 
    175  // Post data may be fetched by the Header panel,
    176  // so set the Timings panel as the new default.
    177  store.getState().ui.detailsPanelSelectedTab = "timings";
    178 
    179  // Open GET request in new tab
    180  await performRequest(monitor, tab, "GET");
    181  newTab = await openLastRequestInTab();
    182  await checkTabResponse(newTab, "GET");
    183  gBrowser.removeCurrentTab();
    184 
    185  // Open POST request in new tab
    186  await performRequest(
    187    monitor,
    188    tab,
    189    "POST",
    190    "application/x-www-form-urlencoded",
    191    "foo=bar&baz=42"
    192  );
    193  newTab = await openLastRequestInTab();
    194  await checkTabResponse(
    195    newTab,
    196    "POST",
    197    "application/x-www-form-urlencoded",
    198    "foo=bar&baz=42"
    199  );
    200  gBrowser.removeCurrentTab();
    201 
    202  // Open POST application/json request in new tab
    203  await performRequest(
    204    monitor,
    205    tab,
    206    "POST",
    207    "application/json",
    208    '{"foo":"bar"}'
    209  );
    210  newTab = await openLastRequestInTab();
    211  await checkTabResponse(newTab, "POST", "application/json", '{"foo":"bar"}');
    212  gBrowser.removeCurrentTab();
    213 
    214  await teardown(monitor);
    215 
    216  // OpenLastRequestInTab by middle click
    217  async function openLastRequestInTab() {
    218    const requestItems = document.querySelectorAll(".request-list-item");
    219    const lastRequest = requestItems[requestItems.length - 1];
    220 
    221    const onTabOpen = once(gBrowser.tabContainer, "TabOpen", false);
    222    EventUtils.sendMouseEvent(
    223      { type: "mousedown", button: MIDDLE_MOUSE_BUTTON },
    224      lastRequest
    225    );
    226    await onTabOpen;
    227    info("A new tab has been opened");
    228 
    229    const awaitedTab = gBrowser.selectedTab;
    230    await BrowserTestUtils.browserLoaded(awaitedTab.linkedBrowser);
    231    info("The tab load completed");
    232 
    233    return awaitedTab;
    234  }
    235 });
    236 
    237 async function performRequest(monitor, tab, method, contentType, payload) {
    238  const wait = waitForNetworkEvents(monitor, 1);
    239  await SpecialPowers.spawn(
    240    tab.linkedBrowser,
    241    [[method, contentType, payload]],
    242    async function ([method_, contentType_, payload_]) {
    243      content.wrappedJSObject.performRequest(method_, contentType_, payload_);
    244    }
    245  );
    246  await wait;
    247  info("Performed request to test server");
    248 }
    249 
    250 async function checkTabResponse(checkedTab, method, contentType, payload) {
    251  await SpecialPowers.spawn(
    252    checkedTab.linkedBrowser,
    253    [[method, contentType, payload]],
    254    async function ([method_, contentType_, payload_]) {
    255      const { body } = content.wrappedJSObject.document;
    256      const expected = [method_, contentType_, payload_].join("\n");
    257      info("Response from the server:" + body.innerHTML.replace(/\n/g, "\\n"));
    258      ok(
    259        body.innerHTML.includes(expected),
    260        "Tab method and data match original request"
    261      );
    262    }
    263  );
    264 }