tor-browser

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

browser_net_cached-status.js (4319B)


      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 cached requests have the correct status code
      8 */
      9 
     10 add_task(async function () {
     11  // Disable rcwn to make cache behavior deterministic.
     12  await pushPref("network.http.rcwn.enabled", false);
     13  // performing http to https redirects, hence we do not
     14  // want https-first to interfere with that test
     15  await pushPref("dom.security.https_first", false);
     16 
     17  const { tab, monitor } = await initNetMonitor(STATUS_CODES_URL, {
     18    enableCache: true,
     19    requestCount: 1,
     20  });
     21  info("Starting test... ");
     22 
     23  const { document, store, windowRequire } = monitor.panelWin;
     24  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     25  const { getDisplayedRequests, getSortedRequests } = windowRequire(
     26    "devtools/client/netmonitor/src/selectors/index"
     27  );
     28 
     29  store.dispatch(Actions.batchEnable(false));
     30 
     31  const REQUEST_DATA = [
     32    {
     33      method: "GET",
     34      uri: STATUS_CODES_SJS + "?sts=ok&cached",
     35      details: {
     36        status: 200,
     37        statusText: "OK",
     38        type: "plain",
     39        fullMimeType: "text/plain; charset=utf-8",
     40      },
     41    },
     42    {
     43      method: "GET",
     44      uri: STATUS_CODES_SJS + "?sts=redirect&cached",
     45      details: {
     46        status: 301,
     47        statusText: "Moved Permanently",
     48        type: "html",
     49        fullMimeType: "text/html; charset=utf-8",
     50      },
     51    },
     52    {
     53      method: "GET",
     54      uri: "http://example.com/redirected",
     55      details: {
     56        status: 404,
     57        statusText: "Not Found",
     58        type: "html",
     59        fullMimeType: "text/html; charset=utf-8",
     60      },
     61    },
     62    {
     63      method: "GET",
     64      uri: STATUS_CODES_SJS + "?sts=ok&cached",
     65      details: {
     66        status: 200,
     67        statusText: "OK (cached)",
     68        displayedStatus: "cached",
     69        type: "plain",
     70        fullMimeType: "text/plain; charset=utf-8",
     71      },
     72    },
     73    {
     74      method: "GET",
     75      uri: STATUS_CODES_SJS + "?sts=redirect&cached",
     76      details: {
     77        status: 301,
     78        statusText: "Moved Permanently (cached)",
     79        displayedStatus: "cached",
     80        type: "html",
     81        fullMimeType: "text/html; charset=utf-8",
     82      },
     83    },
     84    {
     85      method: "GET",
     86      uri: "http://example.com/redirected",
     87      details: {
     88        status: 404,
     89        statusText: "Not Found",
     90        type: "html",
     91        fullMimeType: "text/html; charset=utf-8",
     92      },
     93    },
     94  ];
     95 
     96  // Cancel the 200 cached request, so that the test can also assert
     97  // that the NS_BINDING_ABORTED status is never displayed for cached requests.
     98  const observer = {
     99    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
    100    observe(subject) {
    101      subject = subject.QueryInterface(Ci.nsIHttpChannel);
    102      if (subject.URI.spec == STATUS_CODES_SJS + "?sts=ok&cached") {
    103        subject.cancel(Cr.NS_BINDING_ABORTED);
    104        Services.obs.removeObserver(
    105          observer,
    106          "http-on-examine-cached-response"
    107        );
    108      }
    109    },
    110  };
    111  Services.obs.addObserver(observer, "http-on-examine-cached-response");
    112 
    113  info("Performing requests #1...");
    114  await performRequestsAndWait();
    115 
    116  info("Performing requests #2...");
    117  await performRequestsAndWait();
    118 
    119  await waitForAllNetworkUpdateEvents();
    120 
    121  let index = 0;
    122  for (const request of REQUEST_DATA) {
    123    const requestItem = document.querySelectorAll(".request-list-item")[index];
    124    requestItem.scrollIntoView();
    125    const requestsListStatus = requestItem.querySelector(".status-code");
    126    EventUtils.sendMouseEvent({ type: "mouseover" }, requestsListStatus);
    127    await waitUntil(() => requestsListStatus.title);
    128    await waitForDOMIfNeeded(requestItem, ".requests-list-timings-total");
    129 
    130    info("Verifying request #" + index);
    131    await verifyRequestItemTarget(
    132      document,
    133      getDisplayedRequests(store.getState()),
    134      getSortedRequests(store.getState())[index],
    135      request.method,
    136      request.uri,
    137      request.details
    138    );
    139 
    140    index++;
    141  }
    142 
    143  await teardown(monitor);
    144 
    145  async function performRequestsAndWait() {
    146    const wait = waitForNetworkEvents(monitor, 3);
    147    await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    148      content.wrappedJSObject.performCachedRequests();
    149    });
    150    await wait;
    151  }
    152 });