tor-browser

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

browser_networkobserver_serviceworker.js (4032B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Tests that all the expected service worker requests are received
      7 // by the network observer.
      8 add_task(async function testServiceWorkerSuccessRequests() {
      9  await addTab(URL_ROOT + "doc_network-observer.html");
     10 
     11  const REQUEST_URL =
     12    URL_ROOT + `sjs_network-observer-test-server.sjs?sts=200&fmt=`;
     13 
     14  const EXPECTED_REQUESTS = [
     15    // The main service worker script request
     16    `https://example.com/browser/devtools/shared/network-observer/test/browser/serviceworker.js`,
     17    // The requests intercepted by the service worker
     18    REQUEST_URL + "js",
     19    REQUEST_URL + "css",
     20    // The request initiated by the service worker
     21    REQUEST_URL + "json",
     22  ];
     23 
     24  const onNetworkEvents = waitForNetworkEvents(null, 4);
     25 
     26  info("Register the service worker and send requests...");
     27  await SpecialPowers.spawn(
     28    gBrowser.selectedBrowser,
     29    [REQUEST_URL],
     30    async url => {
     31      await content.wrappedJSObject.registerServiceWorker();
     32      content.wrappedJSObject.fetch(url + "js");
     33      content.wrappedJSObject.fetch(url + "css");
     34    }
     35  );
     36  const events = await onNetworkEvents;
     37 
     38  is(events.length, 4, "Received the expected number of network events");
     39  for (const { options, channel } of events) {
     40    info(`Assert the info for the request from ${channel.URI.spec}`);
     41    ok(
     42      EXPECTED_REQUESTS.includes(channel.URI.spec),
     43      `The request for ${channel.URI.spec} is an expected service worker request`
     44    );
     45    Assert.notStrictEqual(
     46      channel.loadInfo.browsingContextID,
     47      0,
     48      `The request for ${channel.URI.spec} has a Browsing Context ID of ${channel.loadInfo.browsingContextID}`
     49    );
     50    // The main service worker script request is not from the service worker
     51    if (channel.URI.spec.includes("serviceworker.js")) {
     52      ok(
     53        !options.fromServiceWorker,
     54        `The request for ${channel.URI.spec} is not from the service worker\n`
     55      );
     56    } else {
     57      ok(
     58        options.fromServiceWorker,
     59        `The request for ${channel.URI.spec} is from the service worker\n`
     60      );
     61    }
     62  }
     63 
     64  info("Unregistering the service worker...");
     65  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     66    await content.wrappedJSObject.unregisterServiceWorker();
     67  });
     68 });
     69 
     70 // Tests that the expected failed service worker request is received by the network observer.
     71 add_task(async function testServiceWorkerFailedRequests() {
     72  await addTab(URL_ROOT + "doc_network-observer-missing-service-worker.html");
     73 
     74  const REQUEST_URL =
     75    URL_ROOT + `sjs_network-observer-test-server.sjs?sts=200&fmt=js`;
     76 
     77  const EXPECTED_REQUESTS = [
     78    // The main service worker script request which should be missing
     79    "https://example.com/browser/devtools/shared/network-observer/test/browser/serviceworker-missing.js",
     80    // A notrmal request
     81    "https://example.com/browser/devtools/shared/network-observer/test/browser/sjs_network-observer-test-server.sjs?sts=200&fmt=js",
     82  ];
     83 
     84  const onNetworkEvents = waitForNetworkEvents(null, 2);
     85  await SpecialPowers.spawn(
     86    gBrowser.selectedBrowser,
     87    [REQUEST_URL],
     88    async url => {
     89      await content.wrappedJSObject.registerServiceWorker();
     90      content.wrappedJSObject.fetch(url);
     91    }
     92  );
     93 
     94  const events = await onNetworkEvents;
     95  is(events.length, 2, "Received the expected number of network events");
     96 
     97  for (const { options, channel } of events) {
     98    info(`Assert the info for the request from ${channel.URI.spec}`);
     99    ok(
    100      EXPECTED_REQUESTS.includes(channel.URI.spec),
    101      `The request for ${channel.URI.spec} is an expected request`
    102    );
    103    Assert.notStrictEqual(
    104      channel.loadInfo.browsingContextID,
    105      0,
    106      `The request for ${channel.URI.spec} has a Browsing Context ID of ${channel.loadInfo.browsingContextID}`
    107    );
    108    ok(
    109      !options.fromServiceWorker,
    110      `The request for ${channel.URI.spec} is not from the service worker\n`
    111    );
    112  }
    113 });