tor-browser

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

browser_net_json-malformed.js (3143B)


      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 malformed JSON responses are handled correctly.
      8 */
      9 
     10 add_task(async function () {
     11  const {
     12    L10N,
     13  } = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
     14  const { tab, monitor } = await initNetMonitor(JSON_MALFORMED_URL, {
     15    requestCount: 1,
     16  });
     17  info("Starting test... ");
     18 
     19  const { document, store, windowRequire } = monitor.panelWin;
     20  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
     21  const { getDisplayedRequests, getSortedRequests } = windowRequire(
     22    "devtools/client/netmonitor/src/selectors/index"
     23  );
     24 
     25  store.dispatch(Actions.batchEnable(false));
     26 
     27  // Execute requests.
     28  await performRequests(monitor, tab, 1);
     29 
     30  const requestItem = document.querySelector(".request-list-item");
     31  const requestsListStatus = requestItem.querySelector(".status-code");
     32  EventUtils.sendMouseEvent({ type: "mouseover" }, requestsListStatus);
     33  await waitUntil(() => requestsListStatus.title);
     34  await waitForDOMIfNeeded(requestItem, ".requests-list-timings-total");
     35 
     36  await verifyRequestItemTarget(
     37    document,
     38    getDisplayedRequests(store.getState()),
     39    getSortedRequests(store.getState())[0],
     40    "GET",
     41    CONTENT_TYPE_SJS + "?fmt=json-malformed",
     42    {
     43      status: 200,
     44      statusText: "OK",
     45      type: "json",
     46      fullMimeType: "text/json; charset=utf-8",
     47    }
     48  );
     49 
     50  const wait = waitForDOM(document, "#response-panel .cm-content");
     51  store.dispatch(Actions.toggleNetworkDetails());
     52  clickOnSidebarTab(document, "response");
     53  await wait;
     54 
     55  const tabpanel = document.querySelector("#response-panel");
     56  is(
     57    tabpanel.querySelector(".response-error-header") === null,
     58    false,
     59    "The response error header doesn't have the intended visibility."
     60  );
     61  is(
     62    tabpanel.querySelector(".response-error-header").textContent,
     63    "SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data" +
     64      " at line 1 column 40 of the JSON data",
     65    "The response error header doesn't have the intended text content."
     66  );
     67  is(
     68    tabpanel.querySelector(".response-error-header").getAttribute("title"),
     69    "SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data" +
     70      " at line 1 column 40 of the JSON data",
     71    "The response error header doesn't have the intended tooltiptext attribute."
     72  );
     73  const jsonView = tabpanel.querySelector(".tree-section .treeLabel") || {};
     74  is(
     75    jsonView.textContent === L10N.getStr("jsonScopeName"),
     76    false,
     77    "The response json view doesn't have the intended visibility."
     78  );
     79  is(
     80    tabpanel.querySelector(".cm-content") === null,
     81    false,
     82    "The response editor has the intended visibility."
     83  );
     84  is(
     85    tabpanel.querySelector(".response-image-box") === null,
     86    true,
     87    "The response image box doesn't have the intended visibility."
     88  );
     89 
     90  is(
     91    getCodeMirrorValue(monitor),
     92    '{ "greeting": "Hello malformed JSON!" },',
     93    "The text shown in the source editor is incorrect."
     94  );
     95 
     96  await teardown(monitor);
     97 });