tor-browser

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

browser_net_send-beacon.js (2216B)


      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 beacons are handled correctly.
      8 */
      9 
     10 const IFRAME_URL = EXAMPLE_URL + "html_send-beacon-late-iframe-request.html";
     11 
     12 add_task(async function () {
     13  const { tab, monitor } = await initNetMonitor(SEND_BEACON_URL, {
     14    requestCount: 1,
     15  });
     16  const { store, windowRequire } = monitor.panelWin;
     17  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     18  const { getSortedRequests } = windowRequire(
     19    "devtools/client/netmonitor/src/selectors/index"
     20  );
     21 
     22  store.dispatch(Actions.batchEnable(false));
     23 
     24  is(
     25    store.getState().requests.requests.length,
     26    0,
     27    "The requests menu should be empty."
     28  );
     29 
     30  // Execute requests.
     31  await performRequests(monitor, tab, 1);
     32 
     33  is(
     34    store.getState().requests.requests.length,
     35    1,
     36    "The beacon should be recorded."
     37  );
     38 
     39  // Wait for updates of status, blockedReason fields
     40  await waitForRequestData(store, ["status", "blockedReason"], null, 0);
     41  const request = getSortedRequests(store.getState())[0];
     42  is(request.method, "POST", "The method is correct.");
     43  ok(request.url.endsWith("beacon_request"), "The URL is correct.");
     44  is(request.status, "404", "The status is correct.");
     45  is(request.blockedReason, 0, "The request is not blocked");
     46 
     47  const onNetworkEvents = waitForNetworkEvents(monitor, 2);
     48  await SpecialPowers.spawn(
     49    tab.linkedBrowser,
     50    [IFRAME_URL],
     51    async function (url) {
     52      const iframe = content.document.createElement("iframe");
     53      iframe.src = url;
     54      content.document.body.appendChild(iframe);
     55      await new Promise(resolve => (iframe.onload = resolve));
     56      iframe.remove();
     57    }
     58  );
     59  await onNetworkEvents;
     60 
     61  await waitForRequestData(store, ["status", "blockedReason"], null, 2);
     62  const lateRequest = getSortedRequests(store.getState())[2];
     63  is(lateRequest.method, "POST", "The method is correct.");
     64  ok(lateRequest.url.endsWith("beacon_late_request"), "The URL is correct.");
     65  is(lateRequest.status, "404", "The status is correct.");
     66  is(lateRequest.blockedReason, 0, "The request is not blocked");
     67 
     68  return teardown(monitor);
     69 });