tor-browser

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

browser_net_edit_resend_caret.js (3413B)


      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 position of caret does not change (resets to the end) after setting
      8 * header's value to empty string. Also make sure the "method" field stays empty
      9 * after removing it and resets to its original value when it looses focus.
     10 */
     11 
     12 add_task(async function () {
     13  if (
     14    Services.prefs.getBoolPref(
     15      "devtools.netmonitor.features.newEditAndResend",
     16      true
     17    )
     18  ) {
     19    ok(
     20      true,
     21      "Skip this test when pref is true, because this panel won't be default when that is the case."
     22    );
     23    return;
     24  }
     25  const { monitor } = await initNetMonitor(HTTPS_SIMPLE_URL, {
     26    requestCount: 1,
     27  });
     28  info("Starting test... ");
     29 
     30  const { document, store, windowRequire } = monitor.panelWin;
     31  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     32  store.dispatch(Actions.batchEnable(false));
     33 
     34  // Reload to have one request in the list.
     35  const waitForEvents = waitForNetworkEvents(monitor, 1);
     36  await navigateTo(HTTPS_SIMPLE_URL);
     37  await waitForEvents;
     38 
     39  // Open context menu and execute "Edit & Resend".
     40  const firstRequest = document.querySelectorAll(".request-list-item")[0];
     41  const waitForHeaders = waitUntil(() =>
     42    document.querySelector(".headers-overview")
     43  );
     44  EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
     45  await waitForHeaders;
     46  await waitForRequestData(store, ["requestHeaders"]);
     47  EventUtils.sendMouseEvent({ type: "contextmenu" }, firstRequest);
     48 
     49  // Open "New Request" form
     50  await selectContextMenuItem(monitor, "request-list-context-edit-resend");
     51  await waitUntil(() => document.querySelector("#custom-headers-value"));
     52  const headersTextarea = document.querySelector("#custom-headers-value");
     53  await waitUntil(() => document.querySelector("#custom-method-value"));
     54  const methodField = document.querySelector("#custom-method-value");
     55  const originalMethodValue = methodField.value;
     56  const { getSelectedRequest } = windowRequire(
     57    "devtools/client/netmonitor/src/selectors/index"
     58  );
     59  const request = getSelectedRequest(store.getState());
     60  const hostHeader = request.requestHeaders.headers[0];
     61 
     62  // Close the open context menu, otherwise sendString will not work
     63  EventUtils.synthesizeKey("VK_ESCAPE", {});
     64  headersTextarea.focus();
     65 
     66  // Clean value of host header
     67  const headersContent = headersTextarea.value;
     68  const start = "Host: ".length;
     69  const end = headersContent.indexOf("\n");
     70  headersTextarea.setSelectionRange(start, end);
     71  EventUtils.synthesizeKey("VK_DELETE", {});
     72 
     73  methodField.focus();
     74  methodField.select();
     75  EventUtils.synthesizeKey("VK_DELETE", {});
     76 
     77  Assert.notStrictEqual(
     78    getSelectedRequest(store.getState()).requestHeaders.headers[0],
     79    hostHeader,
     80    "Value of Host header was edited and should change"
     81  );
     82 
     83  ok(
     84    headersTextarea.selectionStart === start &&
     85      headersTextarea.selectionEnd === start,
     86    "Position of caret should not change"
     87  );
     88 
     89  Assert.strictEqual(
     90    getSelectedRequest(store.getState()).method,
     91    "",
     92    "Value of method header was deleted and should be empty"
     93  );
     94 
     95  headersTextarea.focus();
     96 
     97  Assert.strictEqual(
     98    getSelectedRequest(store.getState()).method,
     99    originalMethodValue,
    100    "Value of method header should reset to its original value"
    101  );
    102 
    103  await teardown(monitor);
    104 });