tor-browser

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

test_devtools_bypass_serviceworker.html (3303B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title> Verify devtools can utilize nsIChannel::LOAD_BYPASS_SERVICE_WORKER to bypass the service worker </title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <script src="error_reporting_helpers.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
      9 </head>
     10 <body>
     11 <div id="content" style="display: none"></div>
     12 <script src="utils.js"></script>
     13 <script type="text/javascript">
     14 "use strict";
     15 
     16 async function testBypassSW () {
     17  let Ci = SpecialPowers.Ci;
     18 
     19  // Bypass SW imitates the "Disable Cache" option in dev-tools.
     20  // Note: if we put the setter/getter into dev-tools, we should take care of
     21  // the implementation of enabling/disabling cache since it just overwrite the
     22  // defaultLoadFlags of docShell.
     23  function setBypassServiceWorker(aDocShell, aBypass) {
     24    if (aBypass) {
     25      aDocShell.defaultLoadFlags |= Ci.nsIChannel.LOAD_BYPASS_SERVICE_WORKER;
     26      return;
     27    }
     28 
     29    aDocShell.defaultLoadFlags &= ~Ci.nsIChannel.LOAD_BYPASS_SERVICE_WORKER;
     30  }
     31 
     32  function getBypassServiceWorker(aDocShell) {
     33    return !!(aDocShell.defaultLoadFlags &
     34              Ci.nsIChannel.LOAD_BYPASS_SERVICE_WORKER);
     35  }
     36 
     37  async function fetchFakeDocAndCheckIfIntercepted(aWindow) {
     38    const fakeDoc = "fake.html";
     39 
     40    // Note: The fetching document doesn't exist, so the expected status of the
     41    //       repsonse is 404 unless the request is hijacked.
     42    let response = await aWindow.fetch(fakeDoc);
     43    if (response.status === 404) {
     44      return false;
     45    } else if (!response.ok) {
     46      throw(response.statusText);
     47    }
     48 
     49    let text = await response.text();
     50    if (text.includes("Hello")) {
     51      // Intercepted
     52      return true;
     53    }
     54 
     55    throw("Unexpected error");
     56  }
     57 
     58  let docShell = SpecialPowers.wrap(window).docShell;
     59 
     60  info("Test 1: Enable bypass service worker for the docShell");
     61 
     62  setBypassServiceWorker(docShell, true);
     63  ok(getBypassServiceWorker(docShell),
     64     "The loadFlags in docShell does bypass the serviceWorker by default");
     65 
     66  let intercepted = await fetchFakeDocAndCheckIfIntercepted(window);
     67  ok(!intercepted,
     68     "The fetched document wasn't intercepted by the serviceWorker");
     69 
     70  info("Test 2: Disable the bypass service worker for the docShell");
     71 
     72  setBypassServiceWorker(docShell, false);
     73  ok(!getBypassServiceWorker(docShell),
     74     "The loadFlags in docShell doesn't bypass the serviceWorker by default");
     75 
     76  intercepted = await fetchFakeDocAndCheckIfIntercepted(window);
     77  ok(intercepted,
     78     "The fetched document was intercepted by the serviceWorker");
     79 }
     80 
     81 // (This doesn't really need to be its own task, but it allows the actual test
     82 // case to be self-contained.)
     83 add_task(function setupPrefs() {
     84  return SpecialPowers.pushPrefEnv({"set": [
     85    ["dom.serviceWorkers.enabled", true],
     86    ["dom.serviceWorkers.testing.enabled", true],
     87  ]});
     88 });
     89 
     90 add_task(async function test_bypassServiceWorker() {
     91  const swURL = "fetch.js";
     92  let registration = await navigator.serviceWorker.register(swURL);
     93  await waitForState(registration.installing, 'activated');
     94 
     95  try {
     96    await testBypassSW();
     97  } catch (e) {
     98    ok(false, "Reason:" + e);
     99  }
    100 
    101  await registration.unregister();
    102 });
    103 
    104 </script>
    105 </body>
    106 </html>