summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_json_api.js
blob: ca37e2e9514933fc0a039f5e5963acc688895130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";

Services.prefs.setBoolPref("extensions.manifestV3.enabled", true);

const server = createHttpServer({ hosts: ["example.com"] });
server.registerPathHandler("/dummy", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.write(
    `<script>
      // Clobber the JSON API to allow us to confirm that the page's value for
      // the "JSON" object does not affect the content script's JSON API.
      window.JSON = new String("overridden by page");
      window.objFromPage = { serializeMe: "thanks" };
      window.objWithToJSON = { toJSON: () => "toJSON ran", should_not_see: 1 };
      </script>
    `
  );
});

async function test_JSON_parse_and_stringify({ manifest_version }) {
  let extension = ExtensionTestUtils.loadExtension({
    temporarilyInstalled: true, // Needed for granted_host_permissions
    manifest: {
      manifest_version,
      granted_host_permissions: true, // Test-only: grant permissions in MV3.
      host_permissions: ["http://example.com/"], // Work-around for bug 1766752.
      content_scripts: [
        {
          matches: ["http://example.com/dummy"],
          run_at: "document_end",
          js: ["contentscript.js"],
        },
      ],
    },
    files: {
      "contentscript.js"() {
        let json = `{"a":[123,true,null]}`;
        browser.test.assertEq(
          JSON.stringify({ a: [123, true, null] }),
          json,
          "JSON.stringify with basic values"
        );
        let parsed = JSON.parse(json);
        browser.test.assertTrue(
          parsed instanceof Object,
          "Parsed JSON is an Object"
        );
        browser.test.assertTrue(
          parsed.a instanceof Array,
          "Parsed JSON has an Array"
        );
        browser.test.assertEq(
          JSON.stringify(parsed),
          json,
          "JSON.stringify for parsed JSON returns original input"
        );
        browser.test.assertEq(
          JSON.stringify({ toJSON: () => "overridden", hideme: true }),
          `"overridden"`,
          "JSON.parse with toJSON method"
        );

        browser.test.assertEq(
          JSON.stringify(window.wrappedJSObject.objFromPage),
          `{"serializeMe":"thanks"}`,
          "JSON.parse with value from the page"
        );

        browser.test.assertEq(
          JSON.stringify(window.wrappedJSObject.objWithToJSON),
          `"toJSON ran"`,
          "JSON.parse with object with toJSON method from the page"
        );

        browser.test.assertTrue(JSON === globalThis.JSON, "JSON === this.JSON");
        browser.test.assertTrue(JSON === window.JSON, "JSON === window.JSON");
        browser.test.assertEq(
          "overridden by page",
          window.wrappedJSObject.JSON.toString(),
          "page's JSON object is still the original value (overridden by page)"
        );
        browser.test.sendMessage("done");
      },
    },
  });
  await extension.startup();

  let contentPage = await ExtensionTestUtils.loadContentPage(
    "http://example.com/dummy"
  );
  await extension.awaitMessage("done");
  await contentPage.close();
  await extension.unload();
}

add_task(async function test_JSON_apis_MV2() {
  await test_JSON_parse_and_stringify({ manifest_version: 2 });
});

add_task(async function test_JSON_apis_MV3() {
  await test_JSON_parse_and_stringify({ manifest_version: 3 });
});