tor-browser

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

browser_net_throttling_disable_unblocks_requests.js (2585B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Network throttling test: check that disabling throttling allows previously
      5 // blocked requests to go back to unthrottled and complete quickly.
      6 
      7 "use strict";
      8 
      9 const httpServer = createTestHTTPServer();
     10 httpServer.registerPathHandler(`/`, function (request, response) {
     11  response.setStatusLine(request.httpVersion, 200, "OK");
     12  response.write(`<meta charset=utf8><h1>Test throttling profiles</h1>`);
     13 });
     14 
     15 // The "data" path takes a size query parameter and will return a body of the
     16 // requested size.
     17 httpServer.registerPathHandler("/data", function (request, response) {
     18  const size = request.queryString.match(/size=(\d+)/)[1];
     19  response.setHeader("Content-Type", "text/plain");
     20 
     21  response.setStatusLine(request.httpVersion, 200, "OK");
     22  const body = new Array(size * 1).join("a");
     23  response.bodyOutputStream.write(body, body.length);
     24 });
     25 
     26 const TEST_URI = `http://localhost:${httpServer.identity.primaryPort}/`;
     27 
     28 add_task(async function () {
     29  const { monitor } = await initNetMonitor(TEST_URI, { requestCount: 1 });
     30  const { store, windowRequire, connector } = monitor.panelWin;
     31  const { updateNetworkThrottling } = connector;
     32  const { getSortedRequests } = windowRequire(
     33    "devtools/client/netmonitor/src/selectors/index"
     34  );
     35 
     36  const throttleProfile = {
     37    latency: 100,
     38    download: 1,
     39    upload: 10000,
     40  };
     41 
     42  info("Enable very slow throttling");
     43  await updateNetworkThrottling(true, throttleProfile);
     44 
     45  // Start waiting for 2 network events.
     46  const onNetworkEvents = waitForNetworkEvents(monitor, 2);
     47 
     48  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     49    content.fetch("data?size=100");
     50  });
     51 
     52  info("Wait until the request is visible in the UI and then wait for 100ms");
     53  const throttledRequest = await waitFor(
     54    () => getSortedRequests(store.getState())[0]
     55  );
     56  await wait(100);
     57  ok(!throttledRequest.eventTimings, "Request is still not finished");
     58 
     59  info("Disable network throttling");
     60  await updateNetworkThrottling(false);
     61  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     62    content.fetch("data?size=100");
     63  });
     64 
     65  await waitForRequestData(store, ["eventTimings"]);
     66  // The throttled request should be unblocked after disabling throttling.
     67  await onNetworkEvents;
     68 
     69  const requestItem = getSortedRequests(store.getState())[1];
     70  Assert.less(
     71    requestItem.eventTimings.timings.receive,
     72    1000,
     73    "download reported as taking less than one second"
     74  );
     75 
     76  await teardown(monitor);
     77 });