tor-browser

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

browser_net_status-codes.js (6077B)


      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 display the correct status code and text in the UI.
      8 */
      9 
     10 add_task(async function () {
     11  const {
     12    L10N,
     13  } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     14 
     15  const { tab, monitor } = await initNetMonitor(STATUS_CODES_URL, {
     16    requestCount: 1,
     17  });
     18 
     19  info("Starting test... ");
     20 
     21  const { document, store, windowRequire } = monitor.panelWin;
     22  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     23  const { getDisplayedRequests, getSortedRequests } = windowRequire(
     24    "devtools/client/netmonitor/src/selectors/index"
     25  );
     26 
     27  store.dispatch(Actions.batchEnable(false));
     28 
     29  const requestItems = [];
     30 
     31  const REQUEST_DATA = [
     32    {
     33      // request #0
     34      method: "GET",
     35      uri: STATUS_CODES_SJS + "?sts=100",
     36      correctUri: STATUS_CODES_SJS + "?sts=100",
     37      details: {
     38        status: 101,
     39        statusText: "Switching Protocols",
     40        type: "plain",
     41        fullMimeType: "text/plain; charset=utf-8",
     42        size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 0),
     43        time: true,
     44      },
     45    },
     46    {
     47      // request #1
     48      method: "GET",
     49      uri: STATUS_CODES_SJS + "?sts=200",
     50      correctUri: STATUS_CODES_SJS + "?sts=200",
     51      details: {
     52        status: 202,
     53        statusText: "Created",
     54        type: "plain",
     55        fullMimeType: "text/plain; charset=utf-8",
     56        size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 22),
     57        time: true,
     58      },
     59    },
     60    {
     61      // request #2
     62      method: "GET",
     63      uri: STATUS_CODES_SJS + "?sts=300",
     64      correctUri: STATUS_CODES_SJS + "?sts=300",
     65      details: {
     66        status: 303,
     67        statusText: "See Other",
     68        type: "plain",
     69        fullMimeType: "text/plain; charset=utf-8",
     70        size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 22),
     71        time: true,
     72      },
     73    },
     74    {
     75      // request #3
     76      method: "GET",
     77      uri: STATUS_CODES_SJS + "?sts=400",
     78      correctUri: STATUS_CODES_SJS + "?sts=400",
     79      details: {
     80        status: 404,
     81        statusText: "Not Found",
     82        type: "plain",
     83        fullMimeType: "text/plain; charset=utf-8",
     84        size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 22),
     85        time: true,
     86      },
     87    },
     88    {
     89      // request #4
     90      method: "GET",
     91      uri: STATUS_CODES_SJS + "?sts=500",
     92      correctUri: STATUS_CODES_SJS + "?sts=500",
     93      details: {
     94        status: 501,
     95        statusText: "Not Implemented",
     96        type: "plain",
     97        fullMimeType: "text/plain; charset=utf-8",
     98        size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 22),
     99        time: true,
    100      },
    101    },
    102  ];
    103 
    104  // Execute requests.
    105  await performRequests(monitor, tab, 5);
    106 
    107  info("Performing tests");
    108  await verifyRequests();
    109  await testTab(0, testHeaders);
    110 
    111  return teardown(monitor);
    112 
    113  /**
    114   * A helper that verifies all requests show the correct information and caches
    115   * request list items to requestItems array.
    116   */
    117  async function verifyRequests() {
    118    const requestListItems = document.querySelectorAll(".request-list-item");
    119    for (const requestItem of requestListItems) {
    120      requestItem.scrollIntoView();
    121      const requestsListStatus = requestItem.querySelector(".status-code");
    122      EventUtils.sendMouseEvent({ type: "mouseover" }, requestsListStatus);
    123      await waitUntil(() => requestsListStatus.title);
    124      await waitUntil(() =>
    125        requestItem.querySelector(".requests-list-timings-total")
    126      );
    127    }
    128 
    129    info("Verifying requests contain correct information.");
    130    let index = 0;
    131    for (const request of REQUEST_DATA) {
    132      const item = getSortedRequests(store.getState())[index];
    133      requestItems[index] = item;
    134 
    135      info("Verifying request #" + index);
    136      await verifyRequestItemTarget(
    137        document,
    138        getDisplayedRequests(store.getState()),
    139        item,
    140        request.method,
    141        request.uri,
    142        request.details
    143      );
    144 
    145      index++;
    146    }
    147  }
    148 
    149  /**
    150   * A helper that opens a given tab of request details pane, selects and passes
    151   * all requests to the given test function.
    152   *
    153   * @param Number tabIdx
    154   *               The index of tab to activate.
    155   * @param Function testFn(requestItem)
    156   *        A function that should perform all necessary tests. It's called once
    157   *        for every item of REQUEST_DATA with that item being selected in the
    158   *        NetworkMonitor.
    159   */
    160  async function testTab(tabIdx, testFn) {
    161    let counter = 0;
    162    for (const item of REQUEST_DATA) {
    163      info("Testing tab #" + tabIdx + " to update with request #" + counter);
    164      await testFn(item, counter);
    165 
    166      counter++;
    167    }
    168  }
    169 
    170  /**
    171   * A function that tests "Headers" panel contains correct information.
    172   */
    173  async function testHeaders(data, index) {
    174    EventUtils.sendMouseEvent(
    175      { type: "mousedown" },
    176      document.querySelectorAll(".request-list-item")[index]
    177    );
    178 
    179    // wait till all the summary section is loaded
    180    await waitUntil(() =>
    181      document.querySelector("#headers-panel .tabpanel-summary-value")
    182    );
    183    const panel = document.querySelector("#headers-panel");
    184    const {
    185      method,
    186      correctUri,
    187      details: { status, statusText },
    188    } = data;
    189 
    190    const statusCode = panel.querySelector(".status-code");
    191 
    192    EventUtils.sendMouseEvent({ type: "mouseover" }, statusCode);
    193    await waitUntil(() => statusCode.title);
    194 
    195    is(
    196      panel.querySelector(".url-preview .url").textContent,
    197      correctUri,
    198      "The url summary value is incorrect."
    199    );
    200    is(
    201      panel.querySelectorAll(".treeLabel")[0].textContent,
    202      method,
    203      "The method value is incorrect."
    204    );
    205    is(
    206      parseInt(statusCode.dataset.code, 10),
    207      status,
    208      "The status summary code is incorrect."
    209    );
    210    is(
    211      statusCode.getAttribute("title"),
    212      status + " " + statusText,
    213      "The status summary value is incorrect."
    214    );
    215  }
    216 });