summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_block_command.js
blob: c774f9ae6e70825e0c0e029b0750aa8f65cb8b10 (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
"use strict";

const TEST_URI =
  "https://example.com/browser/devtools/client/webconsole/" +
  "test/browser/test-block-action.html";
const TIMEOUT = "TIMEOUT";

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  ok(hud, "web console opened");

  const filter = "test-block-action-style.css";
  const blockCommand = `:block ${filter}`;
  const unblockCommand = `:unblock ${filter}`;

  info("Before blocking");
  await tryFetching();
  const resp1 = await waitFor(() => findConsoleAPIMessage(hud, "successful"));
  ok(resp1, "the request was not blocked");
  info(`Execute the :block command and try to do execute a network request`);
  await executeAndWaitForMessageByType(
    hud,
    blockCommand,
    "are now blocked",
    ".console-api"
  );
  await tryFetching();

  const resp2 = await waitFor(() => findConsoleAPIMessage(hud, "blocked"));
  ok(resp2, "the request was blocked as expected");

  info("Open netmonitor check the blocked filter is registered in its state");
  const { panelWin } = await openNetMonitor();
  const nmStore = panelWin.store;
  nmStore.dispatch(panelWin.actions.toggleRequestBlockingPanel());
  //await waitForTime(1e7);
  // wait until the blockedUrls property is populated
  await waitFor(() => !!nmStore.getState().requestBlocking.blockedUrls.length);
  const netMonitorState1 = nmStore.getState();
  is(
    netMonitorState1.requestBlocking.blockedUrls[0].url,
    filter,
    "blocked request shows up in netmonitor state"
  );

  info("Switch back to the console");
  await hud.toolbox.selectTool("webconsole");

  // :unblock
  await executeAndWaitForMessageByType(
    hud,
    unblockCommand,
    "Removed blocking",
    ".console-api"
  );
  info("After unblocking");

  const netMonitorState2 = nmStore.getState();
  is(
    netMonitorState2.requestBlocking.blockedUrls.length,
    0,
    "unblocked request should not be in netmonitor state"
  );

  await tryFetching();

  const resp3 = await waitFor(() => findConsoleAPIMessage(hud, "successful"));
  ok(resp3, "the request was not blocked");
});

async function tryFetching() {
  await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [TIMEOUT],
    async function (timeoutStr) {
      const win = content.wrappedJSObject;
      const FETCH_URI =
        "https://example.com/browser/devtools/client/webconsole/" +
        "test/browser/test-block-action-style.css";
      const timeout = new Promise(res =>
        win.setTimeout(() => res(timeoutStr), 1000)
      );
      const fetchPromise = win.fetch(FETCH_URI);

      try {
        const resp = await Promise.race([fetchPromise, timeout]);
        if (typeof resp === "object") {
          // Request Promise
          win.console.log("the request was successful");
        } else if (resp === timeoutStr) {
          // Timeout
          win.console.log("the request was blocked");
        } else {
          win.console.error("Unkown response");
        }
      } catch {
        // NetworkError
        win.console.log("the request was blocked");
      }
    }
  );
}