summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/network/tests/browser_network_command_request_blocking.js
blob: dd1167fa9b7c6052d4866fe4605fc86f59c6dd5a (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test the NetworkCommand API around request blocking

add_task(async function () {
  info("Test NetworkCommand request blocking");
  const tab = await addTab("data:text/html,foo");
  const commands = await CommandsFactory.forTab(tab);
  const networkCommand = commands.networkCommand;
  const resourceCommand = commands.resourceCommand;

  // Usage of request blocking APIs requires to listen to NETWORK_EVENT.
  await resourceCommand.watchResources([resourceCommand.TYPES.NETWORK_EVENT], {
    onAvailable: () => {},
  });

  let blockedUrls = await networkCommand.getBlockedUrls();
  Assert.deepEqual(
    blockedUrls,
    [],
    "The list of blocked URLs is originaly empty"
  );

  await networkCommand.blockRequestForUrl("https://foo.com");
  blockedUrls = await networkCommand.getBlockedUrls();
  Assert.deepEqual(
    blockedUrls,
    ["https://foo.com"],
    "The freshly added blocked URL is reported as blocked"
  );

  // We pass "url filters" which can be only part of a URL string
  await networkCommand.blockRequestForUrl("bar");
  blockedUrls = await networkCommand.getBlockedUrls();
  Assert.deepEqual(
    blockedUrls,
    ["https://foo.com", "bar"],
    "The second blocked URL is also reported as blocked"
  );

  await networkCommand.setBlockedUrls(["https://mozilla.org"]);
  blockedUrls = await networkCommand.getBlockedUrls();
  Assert.deepEqual(
    blockedUrls,
    ["https://mozilla.org"],
    "setBlockedUrls replace the whole list of blocked URLs"
  );

  await networkCommand.unblockRequestForUrl("https://mozilla.org");
  blockedUrls = await networkCommand.getBlockedUrls();
  Assert.deepEqual(
    blockedUrls,
    [],
    "The unblocked URL disappear from the list of blocked URLs"
  );

  await commands.destroy();
});