summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_webrequest_filter.html
blob: 5ccbf761ec0492a1f4cead6b943bd6fdfad07516 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!DOCTYPE HTML>

<html>
<head>
<meta charset="utf-8">
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <script type="text/javascript" src="head_webrequest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script>
"use strict";

if (AppConstants.platform === "android") {
  SimpleTest.requestLongerTimeout(6);
}

let windowData, testWindow;

add_task(async function setup() {
  await SpecialPowers.spawnChrome([], async () => {
    Services.cache2.clear();
  });

  testWindow = window.open("about:blank", "_blank", "width=100,height=100");
  await waitForLoad(testWindow);

  // Fetch the windowId and tabId we need to filter with WebRequest.
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "tabs",
      ],
    },
    background() {
      browser.tabs.query({currentWindow: true}).then(tabs => {
        let tab = tabs.find(tab => tab.active);
        let {windowId} = tab;

        browser.test.log(`current window ${windowId} tabs: ${JSON.stringify(tabs.map(tab => [tab.id, tab.url]))}`);
        browser.test.sendMessage("windowData", {windowId, tabId: tab.id});
      });
    },
  });
  await extension.startup();
  windowData = await extension.awaitMessage("windowData");
  info(`window is ${JSON.stringify(windowData)}`);
  await extension.unload();
});

add_task(async function test_webRequest_filter_window() {
  if (AppConstants.MOZ_BUILD_APP !== "browser") {
    // Android does not support multiple windows.
    return;
  }

  await SpecialPowers.pushPrefEnv({
    set: [["dom.serviceWorkers.testing.enabled", true],
          ["network.http.rcwn.enabled", false]],
  });

  let events = {
    "onBeforeRequest":     [{urls: ["<all_urls>"], windowId: windowData.windowId}],
    "onBeforeSendHeaders": [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["requestHeaders"]],
    "onSendHeaders":       [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["requestHeaders"]],
    "onBeforeRedirect":    [{urls: ["<all_urls>"], windowId: windowData.windowId}],
    "onHeadersReceived":   [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["responseHeaders"]],
    "onResponseStarted":   [{urls: ["<all_urls>"], windowId: windowData.windowId}],
    "onCompleted":         [{urls: ["<all_urls>"], windowId: windowData.windowId}, ["responseHeaders"]],
    "onErrorOccurred":     [{urls: ["<all_urls>"], windowId: windowData.windowId}],
  };
  let expect = {
    "file_image_bad.png": {
      optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders"],
      type: "main_frame",
    },
  };

  if (AppConstants.platform != "android") {
    expect["favicon.ico"] = {
      // These events only happen in non-e10s. See bug 1472156.
      optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders"],
      type: "image",
      origin: SimpleTest.getTestFileURL("file_image_bad.png"),
    };
  }

  let extension = makeExtension(events);
  await extension.startup();
  extension.sendMessage("set-expected", {expect, origin: location.href});
  await extension.awaitMessage("continue");

  // We should not get events for a new window load.
  let newWindow = window.open("file_image_good.png", "_blank", "width=100,height=100");
  await waitForLoad(newWindow);
  newWindow.close();

  // We should not get background events.
  let registration = await navigator.serviceWorker.register("webrequest_worker.js?test0", {scope: "."});
  await waitForState(registration.installing, "activated");

  // We should get events for the reload.
  testWindow.location = "file_image_bad.png";
  await extension.awaitMessage("done");

  testWindow.location = "about:blank";
  await registration.unregister();
  await extension.unload();
});

add_task(async function test_webRequest_filter_tab() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.serviceWorkers.testing.enabled", true]],
  });

  let img = `file_image_good.png?r=${Math.random()}`;

  let events = {
    "onBeforeRequest":     [{urls: ["<all_urls>"], tabId: windowData.tabId}],
    "onBeforeSendHeaders": [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["requestHeaders"]],
    "onSendHeaders":       [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["requestHeaders"]],
    "onBeforeRedirect":    [{urls: ["<all_urls>"], tabId: windowData.tabId}],
    "onHeadersReceived":   [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["responseHeaders"]],
    "onResponseStarted":   [{urls: ["<all_urls>"], tabId: windowData.tabId}],
    "onCompleted":         [{urls: ["<all_urls>"], tabId: windowData.tabId}, ["responseHeaders"]],
    "onErrorOccurred":     [{urls: ["<all_urls>"], tabId: windowData.tabId}],
  };
  let expect = {
    "file_image_good.png": {
      // These events only happen in non-e10s. See bug 1472156.
      optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders"],
      type: "main_frame",
      // cached: AppConstants.MOZ_BUILD_APP === "browser",
    },
  };

  if (AppConstants.platform != "android") {
    // A favicon request may be initiated, and complete or be aborted.
    expect["favicon.ico"] = {
      optional_events: ["onBeforeRedirect", "onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", "onHeadersReceived", "onResponseStarted", "onCompleted", "onErrorOccurred"],
      type: "image",
      origin: SimpleTest.getTestFileURL(img),
    };
  }

  let extension = makeExtension(events);
  await extension.startup();
  extension.sendMessage("set-expected", {expect, origin: location.href});
  await extension.awaitMessage("continue");

  if (AppConstants.MOZ_BUILD_APP === "browser") {
    // We should not get events for a new window load.
    let newWindow = window.open(img, "_blank", "width=100,height=100");
    await waitForLoad(newWindow);
    newWindow.close();
  }

  // We should not get background events.
  let registration = await navigator.serviceWorker.register("webrequest_worker.js?test1", {scope: "."});
  await waitForState(registration.installing, "activated");

  // We should get events for the reload.
  testWindow.location = img;
  await extension.awaitMessage("done");

  testWindow.location = "about:blank";
  await registration.unregister();
  await extension.unload();
});


add_task(async function test_webRequest_filter_background() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.serviceWorkers.testing.enabled", true]],
  });

  let events = {
    "onBeforeRequest":     [{urls: ["<all_urls>"], tabId: -1}],
    "onBeforeSendHeaders": [{urls: ["<all_urls>"], tabId: -1}, ["requestHeaders"]],
    "onSendHeaders":       [{urls: ["<all_urls>"], tabId: -1}, ["requestHeaders"]],
    "onBeforeRedirect":    [{urls: ["<all_urls>"], tabId: -1}],
    "onHeadersReceived":   [{urls: ["<all_urls>"], tabId: -1}, ["responseHeaders"]],
    "onResponseStarted":   [{urls: ["<all_urls>"], tabId: -1}],
    "onCompleted":         [{urls: ["<all_urls>"], tabId: -1}, ["responseHeaders"]],
    "onErrorOccurred":     [{urls: ["<all_urls>"], tabId: -1}],
  };
  let expect = {
    "webrequest_worker.js": {
      type: "script",
    },
    "example.txt": {
      status: 404,
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", "onHeadersReceived", "onResponseStarted"],
      optional_events: ["onCompleted", "onErrorOccurred"],
      type: "xmlhttprequest",
      origin: SimpleTest.getTestFileURL("webrequest_worker.js?test2"),
    },
  };

  let extension = makeExtension(events);
  await extension.startup();
  extension.sendMessage("set-expected", {expect, origin: location.href});
  await extension.awaitMessage("continue");

  // We should not get events for a window.
  testWindow.location = "file_image_bad.png";

  // We should get events for the background page.
  let registration = await navigator.serviceWorker.register(SimpleTest.getTestFileURL("webrequest_worker.js?test2"), {scope: "."});
  await waitForState(registration.installing, "activated");
  await extension.awaitMessage("done");
  testWindow.location = "about:blank";
  await registration.unregister();

  await extension.unload();
});

add_task(async function teardown() {
  testWindow.close();
});
</script>
</head>
<body>

</body>
</html>