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

AddonTestUtils.init(this);

AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);

const server = createHttpServer({ hosts: ["example.com"] });
server.registerPathHandler("/", (request, response) => {
  response.setHeader("Content-Tpe", "text/plain", false);
  response.write("OK");
});

add_task(async function test_all_webRequest_ResourceTypes() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["webRequest", "webRequestBlocking", "*://example.com/*"],
    },
    background() {
      browser.test.onMessage.addListener(async msg => {
        browser.webRequest[msg.event].addListener(
          () => {},
          { urls: ["*://example.com/*"], ...msg.filter },
          ["blocking"]
        );
        // Call an API method implemented in the parent process to
        // be sure that the webRequest listener has been registered
        // in the parent process as well.
        await browser.runtime.getBrowserInfo();
        browser.test.sendMessage(`webRequest-listener-registered`);
      });
    },
  });

  await extension.startup();

  const { Schemas } = ChromeUtils.importESModule(
    "resource://gre/modules/Schemas.sys.mjs"
  );
  const webRequestSchema = Schemas.privilegedSchemaJSON
    .get("chrome://extensions/content/schemas/web_request.json")
    .deserialize({});
  const ResourceType = webRequestSchema[1].types.filter(
    type => type.id == "ResourceType"
  )[0];
  ok(
    ResourceType && ResourceType.enum,
    "Found ResourceType in the web_request.json schema"
  );
  info(
    "Register webRequest.onBeforeRequest event listener for all supported ResourceType"
  );

  let { messages } = await promiseConsoleOutput(async () => {
    ExtensionTestUtils.failOnSchemaWarnings(false);
    extension.sendMessage({
      event: "onBeforeRequest",
      filter: {
        // Verify that the resourceType not supported is going to be ignored
        // and all the ones supported does not trigger a ChannelWrapper.matches
        // exception once the listener is being triggered.
        types: [].concat(ResourceType.enum, "not-supported-resource-type"),
      },
    });
    await extension.awaitMessage("webRequest-listener-registered");
    ExtensionTestUtils.failOnSchemaWarnings();

    await ExtensionTestUtils.fetch(
      "http://example.com/dummy",
      "http://example.com"
    );
  });

  AddonTestUtils.checkMessages(messages, {
    expected: [
      { message: /Warning processing types: .* "not-supported-resource-type"/ },
    ],
    forbidden: [{ message: /JavaScript Error: "ChannelWrapper.matches/ }],
  });
  info("No ChannelWrapper.matches errors have been logged");

  await extension.unload();
});