summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_block-extensions.js
blob: 067f18d3c6c9861be55b9abb6023734d8575a259 (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
103
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test the requests that are blocked by extenstions show correctly.
 */
add_task(async function () {
  const extensionName = "Test Blocker";
  info(`Start loading the ${extensionName} extension...`);
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: extensionName,
      permissions: ["*://example.com/", "webRequest", "webRequestBlocking"],
    },
    useAddonManager: "temporary",
    background() {
      // eslint-disable-next-line no-undef
      browser.webRequest.onBeforeRequest.addListener(
        () => {
          return {
            cancel: true,
          };
        },
        {
          urls: [
            "https://example.com/browser/devtools/client/netmonitor/test/request_0",
          ],
        },
        ["blocking"]
      );
      // eslint-disable-next-line no-undef
      browser.test.sendMessage("ready");
    },
  });

  await extension.startup();
  await extension.awaitMessage("ready");

  const { tab, monitor } = await initNetMonitor(HTTPS_SINGLE_GET_URL, {
    requestCount: 2,
  });

  const { document } = monitor.panelWin;

  info("Starting test... ");

  const waitForNetworkEventsToComplete = waitForNetworkEvents(monitor, 2);
  const waitForRequestsToRender = waitForDOM(
    document,
    ".requests-list-row-group"
  );
  const waitForReload = BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  await reloadBrowser();

  await Promise.all([
    waitForNetworkEventsToComplete,
    waitForRequestsToRender,
    waitForReload,
  ]);

  // Find the request list item that matches the blocked url
  const request = document.querySelector(
    "td.requests-list-url[title*=request_0]"
  ).parentNode;

  info("Assert the blocked request");
  ok(
    !!request.querySelector(".requests-list-status .status-code-blocked-icon"),
    "The request blocked status icon is visible"
  );

  is(
    request.querySelector(".requests-list-status .requests-list-status-code")
      .title,
    "Blocked",
    "The request status title is 'Blocked'"
  );

  is(
    request.querySelector(".requests-list-type").innerText,
    "",
    "The request shows no type"
  );

  is(
    request.querySelector(".requests-list-transferred").innerText,
    `Blocked By ${extensionName}`,
    "The request shows the blocking extension name"
  );

  is(
    request.querySelector(".requests-list-size").innerText,
    "",
    "The request shows no size"
  );

  await teardown(monitor);
  info(`Unloading the ${extensionName} extension`);
  await extension.unload();
});