summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_webrequest_basic.html
blob: ef77fee3be91adcd0a1fc9ab09e6b3d66c5a0240 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE HTML>

<html>
<head>
<meta charset="utf-8">
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" 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";

let extension;
add_task(function* setup() {
  // SelfSupport has a tendency to fire when running this test alone, without
  // a good way to turn it off we just set the url to ""
  yield SpecialPowers.pushPrefEnv({
    set: [["browser.selfsupport.url", ""]],
  });

  // Clear the image cache, since it gets in the way otherwise.
  let imgTools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService(SpecialPowers.Ci.imgITools);
  let cache = imgTools.getImgCacheForDocument(document);
  cache.clearCache(false);

  extension = makeExtension();
  yield extension.startup();
});

// expect is a set of test values used by the background script.
//
// type: type of request action
// events: optional, If defined only the events listed are expected for the
//                   request. If undefined, all events except onErrorOccurred
//                   and onBeforeRedirect are expected.  Must be in order received.
// redirect: url to redirect to during onBeforeSendHeaders
// status: number    expected status during onHeadersReceived, 200 default
// cancel: event in which we return cancel=true.  cancelled message is sent.
// cached: expected fromCache value, default is false, checked in onCompletion
// headers: request or response headers to modify

add_task(function* test_webRequest_links() {
  let expect = {
    "file_style_bad.css": {
      type: "stylesheet",
      events: ["onBeforeRequest", "onErrorOccurred"],
      cancel: "onBeforeRequest",
    },
    "file_style_redirect.css": {
      type: "stylesheet",
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onBeforeRedirect"],
      optional_events: ["onHeadersReceived"],
      redirect: "file_style_good.css",
    },
    "file_style_good.css": {
      type: "stylesheet",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addStylesheet("file_style_bad.css");
  yield extension.awaitMessage("cancelled");
  // we redirect to style_good which completes the test
  addStylesheet("file_style_redirect.css");
  yield extension.awaitMessage("done");

  let style = window.getComputedStyle(document.getElementById("test"), null);
  is(style.getPropertyValue("color"), "rgb(255, 0, 0)", "Good CSS loaded");
});

add_task(function* test_webRequest_images() {
  let expect = {
    "file_image_bad.png": {
      type: "image",
      events: ["onBeforeRequest", "onErrorOccurred"],
      cancel: "onBeforeRequest",
    },
    "file_image_redirect.png": {
      type: "image",
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onBeforeRedirect"],
      optional_events: ["onHeadersReceived"],
      redirect: "file_image_good.png",
    },
    "file_image_good.png": {
      type: "image",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addImage("file_image_bad.png");
  yield extension.awaitMessage("cancelled");
  // we redirect to image_good which completes the test
  addImage("file_image_redirect.png");
  yield extension.awaitMessage("done");
});

add_task(function* test_webRequest_scripts() {
  let expect = {
    "file_script_bad.js": {
      type: "script",
      events: ["onBeforeRequest", "onErrorOccurred"],
      cancel: "onBeforeRequest",
    },
    "file_script_redirect.js": {
      type: "script",
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onBeforeRedirect"],
      optional_events: ["onHeadersReceived"],
      redirect: "file_script_good.js",
    },
    "file_script_good.js": {
      type: "script",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addScript("file_script_bad.js");
  yield extension.awaitMessage("cancelled");
  // we redirect to script_good which completes the test
  addScript("file_script_redirect.js");
  yield extension.awaitMessage("done");

  is(window.success, 1, "Good script ran");
  is(window.failure, undefined, "Failure script didn't run");
});

add_task(function* test_webRequest_xhr_get() {
  let expect = {
    "file_script_xhr.js": {
      type: "script",
    },
    "xhr_resource": {
      status: 404,
      type: "xmlhttprequest",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addScript("file_script_xhr.js");
  yield extension.awaitMessage("done");
});

add_task(function* test_webRequest_nonexistent() {
  let expect = {
    "nonexistent_script_url.js": {
      status: 404,
      type: "script",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addScript("nonexistent_script_url.js");
  yield extension.awaitMessage("done");
});

add_task(function* test_webRequest_checkCached() {
  let expect = {
    "file_image_good.png": {
      type: "image",
      cached: true,
    },
    "file_script_good.js": {
      type: "script",
      cached: true,
    },
    "file_style_good.css": {
      type: "stylesheet",
      cached: true,
    },
    "nonexistent_script_url.js": {
      status: 404,
      type: "script",
      cached: false,
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addImage("file_image_good.png");
  addScript("file_script_good.js");
  addStylesheet("file_style_good.css");
  addScript("nonexistent_script_url.js");
  yield extension.awaitMessage("done");

  is(window.success, 2, "Good script ran");
  is(window.failure, undefined, "Failure script didn't run");
});

add_task(function* test_webRequest_headers() {
  let expect = {
    "file_script_nonexistent.js": {
      type: "script",
      status: 404,
      headers: {
        request: {
          add: {
            "X-WebRequest-request": "text",
            "X-WebRequest-request-binary": "binary",
          },
          modify: {
            "user-agent": "WebRequest",
          },
          remove: [
            "referer",
          ],
        },
        response: {
          add: {
            "X-WebRequest-response": "text",
            "X-WebRequest-response-binary": "binary",
          },
          modify: {
            "server": "WebRequest",
            "content-type": "text/html; charset=utf-8",
          },
          remove: [
            "connection",
          ],
        },
      },
      completion: "onCompleted",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addScript("file_script_nonexistent.js");
  yield extension.awaitMessage("done");
});

add_task(function* test_webRequest_tabId() {
  let expect = {
    "file_WebRequest_page3.html": {
      type: "main_frame",
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  let a = addLink("file_WebRequest_page3.html?trigger=a");
  a.click();
  yield extension.awaitMessage("done");
});

add_task(function* test_webRequest_tabId_browser() {
  async function background(url) {
    let tabId;
    browser.test.onMessage.addListener(async (msg, expected) => {
      await browser.tabs.remove(tabId);
      browser.test.sendMessage("done");
    });

    let tab = await browser.tabs.create({url});
    tabId = tab.id;
  }

  let tabExt = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "tabs",
      ],
    },
    background: `(${background})('${SimpleTest.getTestFileURL("file_sample.html")}?nocache=${Math.random()}')`,
  });

  let expect = {
    "file_sample.html": {
      type: "main_frame",
    },
  };
  // expecting origin == undefined
  extension.sendMessage("set-expected", {expect});
  yield extension.awaitMessage("continue");

  // open a tab from a system principal
  yield tabExt.startup();

  yield extension.awaitMessage("done");
  tabExt.sendMessage("done");
  yield tabExt.awaitMessage("done");
  yield tabExt.unload();
});

add_task(function* test_webRequest_frames() {
  let expect = {
    "text/plain,webRequestTest": {
      type: "sub_frame",
      events: ["onBeforeRequest", "onCompleted"],
    },
    "text/plain,webRequestTest_bad": {
      type: "sub_frame",
      events: ["onBeforeRequest", "onCompleted"],
      cancel: "onBeforeRequest",
    },
    "redirection.sjs": {
      status: 302,
      type: "sub_frame",
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", "onHeadersReceived", "onBeforeRedirect"],
    },
    "dummy_page.html": {
      type: "sub_frame",
      status: 404,
    },
    "badrobot": {
      type: "sub_frame",
      status: 404,
      events: ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", "onErrorOccurred"],
    },
  };
  extension.sendMessage("set-expected", {expect, origin: location.href});
  yield extension.awaitMessage("continue");
  addFrame("data:text/plain,webRequestTest");
  addFrame("data:text/plain,webRequestTest_bad");
  yield extension.awaitMessage("cancelled");
  addFrame("redirection.sjs");
  addFrame("https://invalid.localhost/badrobot");
  yield extension.awaitMessage("done");
});

add_task(function* teardown() {
  yield extension.unload();
});
</script>
</head>
<body>
<div id="test">Sample text</div>

</body>
</html>