tor-browser

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

browser_jsonview_access_data.js (4496B)


      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 that $json.data is accessible from the console and contains
      8 * the parsed JSON object.
      9 */
     10 
     11 /**
     12 * Helper function to get console messages from the content page.
     13 */
     14 function getConsoleMessages() {
     15  const consoleStorageInner = Cc["@mozilla.org/consoleAPI-storage;1"];
     16  const storageInner = consoleStorageInner.getService(Ci.nsIConsoleAPIStorage);
     17  const events = storageInner.getEvents();
     18 
     19  // Find messages from the JSON viewer
     20  return events
     21    .filter(e => e.arguments && !!e.arguments.length)
     22    .map(e => {
     23      // Get the formatted message text
     24      return e.arguments.map(arg => String(arg)).join(" ");
     25    });
     26 }
     27 
     28 add_task(async function test_console_welcome_message() {
     29  // Clear any existing console messages
     30  const consoleStorage = Cc["@mozilla.org/consoleAPI-storage;1"];
     31  const storage = consoleStorage.getService(Ci.nsIConsoleAPIStorage);
     32  storage.clearEvents();
     33 
     34  await addJsonViewTab("data:application/json," + JSON.stringify({ test: 1 }));
     35 
     36  // Get console messages
     37  const messages = await SpecialPowers.spawn(
     38    gBrowser.selectedBrowser,
     39    [],
     40    getConsoleMessages
     41  );
     42 
     43  // Check that the welcome message was logged
     44  const welcomeMessage = messages.find(msg =>
     45    msg.includes("Data available from the console")
     46  );
     47  ok(welcomeMessage, "Console welcome message was logged");
     48 
     49  // Verify the message documents the available fields
     50  ok(welcomeMessage.includes("$json.data"), "Message documents $json.data");
     51  ok(welcomeMessage.includes("$json.text"), "Message documents $json.text");
     52  ok(
     53    welcomeMessage.includes("$json.headers"),
     54    "Message documents $json.headers"
     55  );
     56 });
     57 
     58 /**
     59 * Load the JSON viewer for the given json string and get $json.data from it.
     60 */
     61 async function getJSONViewData(json) {
     62  await addJsonViewTab("data:application/json," + json);
     63 
     64  return await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     65    return content.wrappedJSObject.$json?.data;
     66  });
     67 }
     68 
     69 /**
     70 * Helper function to test that $json.data matches the original object.
     71 *
     72 * @param {*} obj - The object to stringify, load, and verify
     73 * @param {string} dataDescription - Description of the data being tested
     74 */
     75 async function testJSONViewData(obj, dataDescription) {
     76  const json = JSON.stringify(obj);
     77  const data = await getJSONViewData(json);
     78 
     79  Assert.deepEqual(data, obj, `$json.data matches ${dataDescription}`);
     80 }
     81 
     82 add_task(async function test_jsonview_data_object() {
     83  await testJSONViewData(
     84    {
     85      name: "test",
     86      values: [1, 2, 3],
     87      nested: { foo: "bar" },
     88    },
     89    "original object"
     90  );
     91 });
     92 
     93 add_task(async function test_jsonview_data_array() {
     94  await testJSONViewData([10, 20, 30, { key: "value" }], "original array");
     95 });
     96 
     97 add_task(async function test_jsonview_data_primitive() {
     98  await testJSONViewData(42, "original number");
     99 });
    100 
    101 add_task(async function test_jsonview_data_string() {
    102  await testJSONViewData("hello world", "original string");
    103 });
    104 
    105 add_task(async function test_jsonview_data_null() {
    106  await testJSONViewData(null, "original null");
    107 });
    108 
    109 add_task(async function test_jsonview_data_invalid_json() {
    110  const invalidJson = "{this is not valid json}";
    111  const data = await getJSONViewData(invalidJson);
    112 
    113  is(data, undefined, "$json.data is undefined for invalid JSON");
    114 });
    115 
    116 add_task(async function test_jsonview_data_large_array() {
    117  const largeArray = Array(1000)
    118    .fill(null)
    119    .map((_, i) => i * 2);
    120 
    121  await testJSONViewData(largeArray, "large array");
    122 });
    123 
    124 add_task(async function test_jsonview_text() {
    125  const obj = { name: "test", value: 42 };
    126  const json = JSON.stringify(obj);
    127  await addJsonViewTab("data:application/json," + json);
    128 
    129  const text = await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    130    return content.wrappedJSObject.$json?.text;
    131  });
    132 
    133  is(text, json, "$json.text matches the original JSON text");
    134 });
    135 
    136 add_task(async function test_jsonview_headers() {
    137  await addJsonViewTab(
    138    "data:application/json," + JSON.stringify({ foo: "bar" })
    139  );
    140 
    141  const headers = await SpecialPowers.spawn(
    142    gBrowser.selectedBrowser,
    143    [],
    144    () => {
    145      return content.wrappedJSObject.$json?.headers;
    146    }
    147  );
    148 
    149  ok(headers, "$json.headers is defined");
    150  ok(Array.isArray(headers.response), "$json.headers.response is an array");
    151  ok(Array.isArray(headers.request), "$json.headers.request is an array");
    152 });