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
|
"use strict";
const server = createHttpServer({ hosts: ["example.com"] });
server.registerPathHandler("/dummy", (request, response) => {
response.setStatusLine(request.httpVersion, 200, "OK");
response.write("ok");
});
add_task(async function test_webRequest_viewsource() {
function background(serverPort) {
browser.proxy.onRequest.addListener(
details => {
if (details.url === `http://example.com:${serverPort}/dummy`) {
browser.test.assertTrue(
true,
"viewsource protocol worked in proxy request"
);
browser.test.sendMessage("proxied");
}
},
{ urls: ["<all_urls>"] }
);
browser.webRequest.onBeforeRequest.addListener(
details => {
browser.test.assertEq(
`http://example.com:${serverPort}/redirect`,
details.url,
"viewsource protocol worked in webRequest"
);
browser.test.sendMessage("viewed");
return { redirectUrl: `http://example.com:${serverPort}/dummy` };
},
{ urls: ["http://example.com/redirect"] },
["blocking"]
);
browser.webRequest.onBeforeRequest.addListener(
details => {
browser.test.assertEq(
`http://example.com:${serverPort}/dummy`,
details.url,
"viewsource protocol worked in webRequest"
);
browser.test.sendMessage("redirected");
return { cancel: true };
},
{ urls: ["http://example.com/dummy"] },
["blocking"]
);
browser.webRequest.onCompleted.addListener(
details => {
// If cancel fails we get onCompleted.
browser.test.fail("onCompleted received");
},
{ urls: ["http://example.com/dummy"] }
);
browser.webRequest.onErrorOccurred.addListener(
details => {
browser.test.assertEq(
details.error,
"NS_ERROR_ABORT",
"request cancelled"
);
browser.test.sendMessage("cancelled");
},
{ urls: ["http://example.com/dummy"] }
);
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
},
background: `(${background})(${server.identity.primaryPort})`,
});
await extension.startup();
let contentPage = await ExtensionTestUtils.loadContentPage(
`view-source:http://example.com:${server.identity.primaryPort}/redirect`
);
await Promise.all([
extension.awaitMessage("proxied"),
extension.awaitMessage("viewed"),
extension.awaitMessage("redirected"),
extension.awaitMessage("cancelled"),
]);
await contentPage.close();
await extension.unload();
});
|