summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_download.js
blob: f1415ca87a02982e825999991f36505152a446db (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
"use strict";

// Test for Bug 1579911: Check that download requests created by the
// downloads.download API can be observed by extensions.
// The DNR version is in test_ext_dnr_download.js.
add_task(async function testDownload() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "webRequest",
        "webRequestBlocking",
        "downloads",
        "https://example.com/*",
      ],
    },
    background: async function () {
      browser.webRequest.onBeforeRequest.addListener(
        () => {
          browser.test.sendMessage("request_intercepted");
          return { cancel: true };
        },
        {
          urls: ["https://example.com/downloadtest"],
        },
        ["blocking"]
      );

      browser.downloads.onChanged.addListener(delta => {
        browser.test.assertEq(delta.state.current, "interrupted");
        browser.test.sendMessage("done");
      });

      await browser.downloads.download({
        url: "https://example.com/downloadtest",
        filename: "example.txt",
      });
    },
  });

  await extension.startup();
  await extension.awaitMessage("request_intercepted");
  await extension.awaitMessage("done");
  await extension.unload();
});