tor-browser

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

browser_net_use_as_fetch.js (2522B)


      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 Use as Fetch works.
      8 */
      9 
     10 add_task(async function () {
     11  const { tab, monitor, toolbox } = await initNetMonitor(CURL_URL, {
     12    requestCount: 1,
     13  });
     14  info("Starting test... ");
     15 
     16  // GET request, no cookies (first request)
     17  await performRequest("GET");
     18  await testConsoleInput(`await fetch("http://example.com/browser/devtools/client/netmonitor/test/sjs_simple-test-server.sjs", {
     19    "credentials": "omit",
     20    "headers": {
     21        "User-Agent": "${navigator.userAgent}",
     22        "Accept": "*/*",
     23        "Accept-Language": "en-US",
     24        "X-Custom-Header-1": "Custom value",
     25        "X-Custom-Header-2": "8.8.8.8",
     26        "X-Custom-Header-3": "Mon, 3 Mar 2014 11:11:11 GMT",
     27        "Pragma": "no-cache",
     28        "Cache-Control": "no-cache"
     29    },
     30    "referrer": "http://example.com/browser/devtools/client/netmonitor/test/html_copy-as-curl.html",
     31    "method": "GET",
     32    "mode": "cors"
     33 });`);
     34 
     35  await teardown(monitor);
     36 
     37  async function performRequest(method, payload) {
     38    const waitRequest = waitForNetworkEvents(monitor, 1);
     39    await SpecialPowers.spawn(
     40      tab.linkedBrowser,
     41      [
     42        {
     43          url: SIMPLE_SJS,
     44          method_: method,
     45          payload_: payload,
     46        },
     47      ],
     48      async function ({ url, method_, payload_ }) {
     49        content.wrappedJSObject.performRequest(url, method_, payload_);
     50      }
     51    );
     52    await waitRequest;
     53  }
     54 
     55  async function testConsoleInput(expectedResult) {
     56    const { document } = monitor.panelWin;
     57 
     58    const items = document.querySelectorAll(".request-list-item");
     59    EventUtils.sendMouseEvent({ type: "mousedown" }, items[items.length - 1]);
     60    EventUtils.sendMouseEvent(
     61      { type: "contextmenu" },
     62      document.querySelectorAll(".request-list-item")[0]
     63    );
     64 
     65    /* Ensure that the use as fetch option is always visible */
     66    is(
     67      !!getContextMenuItem(monitor, "request-list-context-use-as-fetch"),
     68      true,
     69      'The "Use as Fetch" context menu item should not be hidden.'
     70    );
     71 
     72    const split = toolbox.once("split-console");
     73    await selectContextMenuItem(monitor, "request-list-context-use-as-fetch");
     74 
     75    await split;
     76    const hud = toolbox.getPanel("webconsole").hud;
     77    await hud.jsterm.once("set-input-value");
     78 
     79    is(
     80      hud.getInputValue(),
     81      expectedResult,
     82      "Console input contains fetch request for item " + (items.length - 1)
     83    );
     84  }
     85 });