tor-browser

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

browser_net_post-data.js (6448B)


      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 the POST requests display the correct information in the UI.
      8 */
      9 add_task(async function () {
     10  const {
     11    L10N,
     12  } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     13 
     14  // Set a higher panel height in order to get full CodeMirror content
     15  Services.prefs.setIntPref("devtools.toolbox.footer.height", 600);
     16 
     17  const { tab, monitor } = await initNetMonitor(POST_DATA_URL, {
     18    requestCount: 1,
     19  });
     20  info("Starting test... ");
     21 
     22  const { document, store, windowRequire } = 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  // Execute requests.
     31  await performRequests(monitor, tab, 2);
     32 
     33  const requestItems = document.querySelectorAll(".request-list-item");
     34  for (const requestItem of requestItems) {
     35    requestItem.scrollIntoView();
     36    const requestsListStatus = requestItem.querySelector(".status-code");
     37    EventUtils.sendMouseEvent({ type: "mouseover" }, requestsListStatus);
     38    await waitUntil(() => requestsListStatus.title);
     39    await waitForDOMIfNeeded(requestItem, ".requests-list-timings-total");
     40  }
     41 
     42  await verifyRequestItemTarget(
     43    document,
     44    getDisplayedRequests(store.getState()),
     45    getSortedRequests(store.getState())[0],
     46    "POST",
     47    SIMPLE_SJS +
     48      "?foo=bar&baz=42&valueWithEqualSign=hijk=123=mnop&type=urlencoded",
     49    {
     50      status: 200,
     51      statusText: "Och Aye",
     52      type: "plain",
     53      fullMimeType: "text/plain; charset=utf-8",
     54      size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 12),
     55      time: true,
     56    }
     57  );
     58  await verifyRequestItemTarget(
     59    document,
     60    getDisplayedRequests(store.getState()),
     61    getSortedRequests(store.getState())[1],
     62    "POST",
     63    SIMPLE_SJS + "?foo=bar&baz=42&type=multipart",
     64    {
     65      status: 200,
     66      statusText: "Och Aye",
     67      type: "plain",
     68      fullMimeType: "text/plain; charset=utf-8",
     69      size: L10N.getFormatStrWithNumbers("networkMenu.sizeB", 12),
     70      time: true,
     71    }
     72  );
     73 
     74  // Wait for raw data toggle to be displayed
     75  const wait = waitForDOM(
     76    document,
     77    "#request-panel .raw-data-toggle-input .devtools-checkbox-toggle"
     78  );
     79  EventUtils.sendMouseEvent(
     80    { type: "mousedown" },
     81    document.querySelectorAll(".request-list-item")[0]
     82  );
     83  clickOnSidebarTab(document, "request");
     84  await wait;
     85  await testParamsTab("urlencoded");
     86 
     87  // Wait for header and CodeMirror editor to be displayed
     88  const waitForHeader = waitForDOM(document, "#request-panel .data-header");
     89  const waitForSourceEditor = waitForDOM(
     90    document,
     91    "#request-panel .cm-content"
     92  );
     93  EventUtils.sendMouseEvent(
     94    { type: "mousedown" },
     95    document.querySelectorAll(".request-list-item")[1]
     96  );
     97  await Promise.all([waitForHeader, waitForSourceEditor]);
     98  await testParamsTab("multipart");
     99 
    100  return teardown(monitor);
    101 
    102  async function testParamsTab(type) {
    103    const tabpanel = document.querySelector("#request-panel");
    104 
    105    function checkVisibility(box) {
    106      is(
    107        tabpanel.querySelector(".cm-content") === null,
    108        !box.includes("editor"),
    109        "The request post data doesn't have the intended visibility."
    110      );
    111    }
    112 
    113    is(
    114      tabpanel.querySelectorAll(".raw-data-toggle").length,
    115      type == "urlencoded" ? 1 : 0,
    116      "The display of the raw request data toggle must be correct."
    117    );
    118    is(
    119      tabpanel.querySelectorAll(".empty-notice").length,
    120      0,
    121      "The empty notice should not be displayed in this tabpanel."
    122    );
    123 
    124    is(
    125      tabpanel.querySelector(".data-label").textContent,
    126      L10N.getStr(
    127        type == "urlencoded" ? "paramsFormData" : "paramsPostPayload"
    128      ),
    129      "The post section doesn't have the correct title."
    130    );
    131 
    132    const labels = tabpanel.querySelectorAll("tr .treeLabelCell .treeLabel");
    133    const values = tabpanel.querySelectorAll("tr .treeValueCell .objectBox");
    134 
    135    if (type == "urlencoded") {
    136      checkVisibility("request");
    137      is(
    138        labels.length,
    139        4,
    140        "There should be 4 param values displayed in this tabpanel."
    141      );
    142      is(
    143        labels[0].textContent,
    144        "foo",
    145        "The first post param name was incorrect."
    146      );
    147      is(
    148        values[0].textContent,
    149        `"bar"`,
    150        "The first post param value was incorrect."
    151      );
    152      is(
    153        labels[1].textContent,
    154        "baz",
    155        "The second post param name was incorrect."
    156      );
    157      is(
    158        values[1].textContent,
    159        `"123"`,
    160        "The second post param value was incorrect."
    161      );
    162      is(
    163        labels[2].textContent,
    164        "valueWithEqualSign",
    165        "The third post param name was incorrect."
    166      );
    167      is(
    168        values[2].textContent,
    169        `"xyz=abc=123"`,
    170        "The third post param value was incorrect."
    171      );
    172      is(
    173        labels[3].textContent,
    174        "valueWithAmpersand",
    175        "The fourth post param name was incorrect."
    176      );
    177      is(
    178        values[3].textContent,
    179        `"abcd&1234"`,
    180        "The fourth post param value was incorrect."
    181      );
    182    } else {
    183      checkVisibility("request editor");
    184 
    185      const text = getCodeMirrorValue(monitor);
    186 
    187      ok(
    188        text.includes('Content-Disposition: form-data; name="text"'),
    189        "The text shown in the source editor is incorrect (1.1)."
    190      );
    191      ok(
    192        text.includes('Content-Disposition: form-data; name="email"'),
    193        "The text shown in the source editor is incorrect (2.1)."
    194      );
    195      ok(
    196        text.includes('Content-Disposition: form-data; name="range"'),
    197        "The text shown in the source editor is incorrect (3.1)."
    198      );
    199      ok(
    200        text.includes('Content-Disposition: form-data; name="Custom field"'),
    201        "The text shown in the source editor is incorrect (4.1)."
    202      );
    203      ok(
    204        text.includes("Some text..."),
    205        "The text shown in the source editor is incorrect (2.2)."
    206      );
    207      ok(
    208        text.includes("42"),
    209        "The text shown in the source editor is incorrect (3.2)."
    210      );
    211      ok(
    212        text.includes("Extra data"),
    213        "The text shown in the source editor is incorrect (4.2)."
    214      );
    215    }
    216  }
    217 });