tor-browser

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

browser_net_throttling_cached.js (1861B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const {
      7  profiles,
      8  PROFILE_CONSTANTS,
      9 } = require("resource://devtools/client/shared/components/throttling/profiles.js");
     10 
     11 const offlineProfile = profiles.find(
     12  profile => profile.id === PROFILE_CONSTANTS.OFFLINE
     13 );
     14 
     15 const CACHED_URL = STATUS_CODES_SJS + "?sts=ok&cached&test_cached";
     16 
     17 add_task(async function () {
     18  const { monitor } = await initNetMonitor(SIMPLE_URL, { requestCount: 1 });
     19  const { connector } = monitor.panelWin;
     20  const { updateNetworkThrottling } = connector;
     21 
     22  // Throttle the network before entering the content process
     23  await pushPref("devtools.cache.disabled", false);
     24  await updateNetworkThrottling(true, offlineProfile);
     25 
     26  // Clear the cache beforehand
     27  Services.cache2.clear();
     28 
     29  // The request is blocked since the profile is offline
     30  await SpecialPowers.spawn(
     31    gBrowser.selectedBrowser,
     32    [CACHED_URL],
     33    async url => {
     34      try {
     35        await content.fetch(url);
     36        ok(false, "Should not load since profile is offline");
     37      } catch (err) {
     38        is(err.name, "TypeError", "Should fail since profile is offline");
     39      }
     40    }
     41  );
     42 
     43  // Disable throttling
     44  await pushPref("devtools.cache.disabled", false);
     45  await updateNetworkThrottling(false);
     46 
     47  // Fetch to prime the cache
     48  await SpecialPowers.spawn(
     49    gBrowser.selectedBrowser,
     50    [CACHED_URL],
     51    async url => {
     52      await content.fetch(url);
     53    }
     54  );
     55 
     56  // Set profile to offline again
     57  await pushPref("devtools.cache.disabled", false);
     58  await updateNetworkThrottling(true, offlineProfile);
     59 
     60  // Check that cached resource loaded
     61  await SpecialPowers.spawn(
     62    gBrowser.selectedBrowser,
     63    [CACHED_URL],
     64    async url => {
     65      await content.fetch(url);
     66    }
     67  );
     68 
     69  await teardown(monitor);
     70 });