summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_xhr_capabilities.js
blob: 0728946817a2335188e34b2a2f77c981f35bc2b3 (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
"use strict";

const server = createHttpServer({ hosts: ["example.com"] });
server.registerDirectory("/data/", do_get_file("data"));

add_task(async function test_xhr_capabilities() {
  let extension = ExtensionTestUtils.loadExtension({
    background() {
      let xhr = new XMLHttpRequest();
      xhr.open("GET", browser.runtime.getURL("bad.xml"));

      browser.test.sendMessage("result", {
        name: "Background script XHRs should not be privileged",
        result: xhr.channel === undefined,
      });

      xhr.onload = () => {
        browser.test.sendMessage("result", {
          name: "Background script XHRs should not yield <parsererrors>",
          result: xhr.responseXML === null,
        });
      };
      xhr.send();
    },

    manifest: {
      content_scripts: [
        {
          matches: ["http://example.com/data/file_sample.html"],
          js: ["content_script.js"],
        },
      ],
      web_accessible_resources: ["bad.xml"],
    },

    files: {
      "bad.xml": "<xml",
      "content_script.js"() {
        let xhr = new XMLHttpRequest();
        xhr.open("GET", browser.runtime.getURL("bad.xml"));

        browser.test.sendMessage("result", {
          name: "Content script XHRs should not be privileged",
          result: xhr.channel === undefined,
        });

        xhr.onload = () => {
          browser.test.sendMessage("result", {
            name: "Content script XHRs should not yield <parsererrors>",
            result: xhr.responseXML === null,
          });
        };
        xhr.send();
      },
    },
  });

  await extension.startup();

  let contentPage = await ExtensionTestUtils.loadContentPage(
    "http://example.com/data/file_sample.html"
  );

  // We expect four test results from the content/background scripts.
  for (let i = 0; i < 4; ++i) {
    let result = await extension.awaitMessage("result");
    ok(result.result, result.name);
  }

  await contentPage.close();
  await extension.unload();
});