tor-browser

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

browser_net_service-worker-status.js (3397B)


      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 requests intercepted by service workers have the correct status code
      8 */
      9 
     10 // Service workers only work on https
     11 const URL = EXAMPLE_URL.replace("http:", "https:");
     12 
     13 const TEST_URL = URL + "service-workers/status-codes.html";
     14 
     15 add_task(async function () {
     16  const { tab, monitor } = await initNetMonitor(TEST_URL, {
     17    enableCache: true,
     18    requestCount: 1,
     19  });
     20  info("Starting test... ");
     21 
     22  const { document, store, windowRequire, connector } = monitor.panelWin;
     23  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     24  const { getDisplayedRequests, getSortedRequests } = windowRequire(
     25    "devtools/client/netmonitor/src/selectors/index"
     26  );
     27 
     28  store.dispatch(Actions.batchEnable(false));
     29 
     30  const REQUEST_DATA = [
     31    {
     32      method: "GET",
     33      uri:
     34        URL +
     35        "service-workers/sjs_content-type-test-server.sjs?sts=304&fmt=html",
     36      details: {
     37        status: 200,
     38        statusText: "OK (service worker)",
     39        displayedStatus: "service worker",
     40        type: "plain",
     41        fullMimeType: "text/plain; charset=UTF-8",
     42      },
     43      stackFunctions: ["performRequests"],
     44    },
     45  ];
     46 
     47  info("Registering the service worker...");
     48  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     49    await content.wrappedJSObject.registerServiceWorker();
     50  });
     51 
     52  info("Performing requests...");
     53  // Execute requests.
     54  await performRequests(monitor, tab, REQUEST_DATA.length);
     55 
     56  // Fetch stack-trace data from the backend and wait till
     57  // all packets are received.
     58  const requests = getSortedRequests(store.getState());
     59  await Promise.all(
     60    requests.map(requestItem =>
     61      connector.requestData(requestItem.id, "stackTrace")
     62    )
     63  );
     64 
     65  const requestItems = document.querySelectorAll(".request-list-item");
     66  for (const requestItem of requestItems) {
     67    requestItem.scrollIntoView();
     68    const requestsListStatus = requestItem.querySelector(".status-code");
     69    EventUtils.sendMouseEvent({ type: "mouseover" }, requestsListStatus);
     70    await waitUntil(() => requestsListStatus.title);
     71    await waitForDOMIfNeeded(requestItem, ".requests-list-timings-total");
     72  }
     73 
     74  let index = 0;
     75  for (const request of REQUEST_DATA) {
     76    const item = getSortedRequests(store.getState())[index];
     77 
     78    info(`Verifying request #${index}`);
     79    await verifyRequestItemTarget(
     80      document,
     81      getDisplayedRequests(store.getState()),
     82      item,
     83      request.method,
     84      request.uri,
     85      request.details
     86    );
     87 
     88    const { stacktrace } = item;
     89    const stackLen = stacktrace ? stacktrace.length : 0;
     90 
     91    ok(stacktrace, `Request #${index} has a stacktrace`);
     92    Assert.greaterOrEqual(
     93      stackLen,
     94      request.stackFunctions.length,
     95      `Request #${index} has a stacktrace with enough (${stackLen}) items`
     96    );
     97 
     98    request.stackFunctions.forEach((functionName, j) => {
     99      is(
    100        stacktrace[j].functionName,
    101        functionName,
    102        `Request #${index} has the correct function at position #${j} on the stack`
    103      );
    104    });
    105 
    106    index++;
    107  }
    108 
    109  info("Unregistering the service worker...");
    110  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    111    await content.wrappedJSObject.unregisterServiceWorker();
    112  });
    113 
    114  await teardown(monitor);
    115 });