tor-browser

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

browser_net_post-data-raw-payloads-with-upload-stream-headers.js (6683B)


      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 * for raw payloads with content-type headers attached to the upload stream.
      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(POST_RAW_WITH_HEADERS_URL, {
     16    requestCount: 1,
     17  });
     18  info("Starting test... ");
     19 
     20  const { document, store, windowRequire } = monitor.panelWin;
     21  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     22 
     23  store.dispatch(Actions.batchEnable(false));
     24 
     25  // Execute requests.
     26  await performRequests(monitor, tab, 3);
     27 
     28  const expectedRequestsContent = [
     29    {
     30      headersFromUploadSectionTitle:
     31        "Request headers from upload stream (47 B)",
     32      uploadSectionHeaders: [
     33        { label: "content-type", value: "application/x-www-form-urlencoded" },
     34      ],
     35      uploadSectionRawText: "content-type: application/x-www-form-urlencoded",
     36      requestPanelFormData: [
     37        { label: "foo", value: '"bar"' },
     38        { label: "baz", value: '"123"' },
     39      ],
     40      requestPanelPayload: [
     41        "content-type: application/x-www-form-urlencoded",
     42        "foo=bar&baz=123",
     43      ],
     44    },
     45    {
     46      headersFromUploadSectionTitle:
     47        "Request headers from upload stream (47 B)",
     48      uploadSectionHeaders: [
     49        { label: "content-type", value: "application/x-www-form-urlencoded" },
     50      ],
     51      uploadSectionRawText: "content-type: application/x-www-form-urlencoded",
     52      requestPanelPayload: ["content-type: application/x-www-form-urlencoded"],
     53    },
     54    {
     55      headersFromUploadSectionTitle:
     56        "Request headers from upload stream (74 B)",
     57      uploadSectionHeaders: [
     58        { label: "content-type", value: "application/x-www-form-urlencoded" },
     59        { label: "custom-header", value: "hello world!" },
     60      ],
     61      uploadSectionRawText:
     62        "content-type: application/x-www-form-urlencoded\r\ncustom-header: hello world!",
     63      requestPanelFormData: [
     64        { label: "foo", value: '"bar"' },
     65        { label: "baz", value: '"123"' },
     66      ],
     67      requestPanelPayload: [
     68        "content-type: application/x-www-form-urlencoded",
     69        "custom-header: hello world!",
     70        "foo=bar&baz=123",
     71      ],
     72    },
     73  ];
     74 
     75  const requests = document.querySelectorAll(".request-list-item");
     76  store.dispatch(Actions.toggleNetworkDetails());
     77 
     78  for (let i = 0; i < expectedRequestsContent.length; i++) {
     79    EventUtils.sendMouseEvent({ type: "mousedown" }, requests[i]);
     80    await assertRequestContentInHeaderAndRequestSidePanels(
     81      expectedRequestsContent[i]
     82    );
     83  }
     84 
     85  async function assertRequestContentInHeaderAndRequestSidePanels(expected) {
     86    // Wait for all 3 headers sections to load (Response Headers, Request Headers, Request headers from upload stream)
     87    let wait = waitForDOM(document, "#headers-panel .accordion-item", 3);
     88    clickOnSidebarTab(document, "headers");
     89    await wait;
     90 
     91    let tabpanel = document.querySelector("#headers-panel");
     92    is(
     93      tabpanel.querySelectorAll(".accordion-item").length,
     94      3,
     95      "There should be 3 header sections displayed in this tabpanel."
     96    );
     97 
     98    info("Check that the Headers in the upload stream section are correct.");
     99    is(
    100      tabpanel.querySelectorAll(".accordion-item .accordion-header-label")[2]
    101        .textContent,
    102      expected.headersFromUploadSectionTitle,
    103      "The request headers from upload section doesn't have the correct title."
    104    );
    105 
    106    let labels = tabpanel.querySelectorAll(
    107      ".accordion-item:last-child .accordion-content tr .treeLabelCell .treeLabel"
    108    );
    109    let values = tabpanel.querySelectorAll(
    110      ".accordion-item:last-child .accordion-content tr .treeValueCell .objectBox"
    111    );
    112 
    113    for (let i = 0; i < labels.length; i++) {
    114      is(
    115        labels[i].textContent,
    116        expected.uploadSectionHeaders[i].label,
    117        "The request header name was incorrect."
    118      );
    119      is(
    120        values[i].textContent,
    121        expected.uploadSectionHeaders[i].value,
    122        "The request header value was incorrect."
    123      );
    124    }
    125 
    126    info(
    127      "Toggle to open the raw view for the request headers from upload stream"
    128    );
    129 
    130    wait = waitForDOM(
    131      tabpanel,
    132      ".accordion-item:last-child .accordion-content .raw-headers-container"
    133    );
    134 
    135    tabpanel
    136      .querySelector("#raw-requestHeadersFromUploadStream-checkbox")
    137      .click();
    138    await wait;
    139 
    140    const rawTextArea = tabpanel.querySelector(
    141      ".accordion-item:last-child .accordion-content .raw-headers"
    142    );
    143    is(
    144      rawTextArea.textContent,
    145      expected.uploadSectionRawText,
    146      "The raw text for the request headers from upload section is correct"
    147    );
    148 
    149    info("Switch to the Request panel");
    150 
    151    wait = waitForDOM(document, "#request-panel .panel-container");
    152    clickOnSidebarTab(document, "request");
    153    await wait;
    154 
    155    tabpanel = document.querySelector("#request-panel");
    156    if (expected.requestPanelFormData) {
    157      await waitUntil(
    158        () =>
    159          tabpanel.querySelector(".data-label").textContent ==
    160          L10N.getStr("paramsFormData")
    161      );
    162 
    163      labels = tabpanel.querySelectorAll("tr .treeLabelCell .treeLabel");
    164      values = tabpanel.querySelectorAll("tr .treeValueCell .objectBox");
    165 
    166      for (let i = 0; i < labels.length; i++) {
    167        is(
    168          labels[i].textContent,
    169          expected.requestPanelFormData[i].label,
    170          "The form data param name was incorrect."
    171        );
    172        is(
    173          values[i].textContent,
    174          expected.requestPanelFormData[i].value,
    175          "The form data param value was incorrect."
    176        );
    177      }
    178 
    179      info("Toggle open the the request payload raw view");
    180 
    181      tabpanel.querySelector("#raw-request-checkbox").click();
    182    }
    183    await waitUntil(
    184      () =>
    185        tabpanel.querySelector(".data-label").textContent ==
    186          L10N.getStr("paramsPostPayload") &&
    187        tabpanel.querySelector(
    188          ".panel-container .editor-row-container .cm-content"
    189        )
    190    );
    191 
    192    // Check that the expected header lines are included in the codemirror
    193    // text.
    194    const actualText = tabpanel.querySelector(
    195      ".panel-container .editor-row-container .cm-content"
    196    ).textContent;
    197    const requestPayloadIsCorrect = expected.requestPanelPayload.every(
    198      content => actualText.includes(content)
    199    );
    200 
    201    is(requestPayloadIsCorrect, true, "The request payload is not correct");
    202  }
    203 
    204  return teardown(monitor);
    205 });