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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for simple WebExtension</title>
<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>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(function* test_suspend() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
],
},
background() {
browser.webRequest.onBeforeSendHeaders.addListener(
details => {
// Make sure that returning undefined or a promise that resolves to
// undefined does not break later handlers.
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);
browser.webRequest.onBeforeSendHeaders.addListener(
details => {
return Promise.resolve();
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);
browser.webRequest.onBeforeSendHeaders.addListener(
details => {
let requestHeaders = details.requestHeaders.concat({name: "Foo", value: "Bar"});
return new Promise(resolve => {
setTimeout(resolve, 500);
}).then(() => {
return {requestHeaders};
});
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]);
},
});
yield extension.startup();
let result = yield fetch(SimpleTest.getTestFileURL("return_headers.sjs"));
let headers = JSON.parse(yield result.text());
is(headers.foo, "Bar", "Request header was correctly set on suspended request");
yield extension.unload();
});
// Test that requests that were canceled while suspended for a blocking
// listener are correctly resumed.
add_task(function* test_error_resume() {
let chromeScript = SpecialPowers.loadChromeScript(() => {
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
let observer = channel => {
if (channel instanceof Ci.nsIHttpChannel && channel.URI.spec === "http://example.com/") {
Services.obs.removeObserver(observer, "http-on-modify-request");
// Wait until the next tick to make sure this runs after WebRequest observers.
Promise.resolve().then(() => {
channel.cancel(Cr.NS_BINDING_ABORTED);
});
}
};
Services.obs.addObserver(observer, "http-on-modify-request", false);
});
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
],
},
background() {
browser.webRequest.onBeforeSendHeaders.addListener(
details => {
browser.test.log(`onBeforeSendHeaders({url: ${details.url}})`);
if (details.url === "http://example.com/") {
browser.test.sendMessage("got-before-send-headers");
}
},
{urls: ["<all_urls>"]},
["blocking"]);
browser.webRequest.onErrorOccurred.addListener(
details => {
browser.test.log(`onErrorOccurred({url: ${details.url}})`);
if (details.url === "http://example.com/") {
browser.test.sendMessage("got-error-occurred");
}
},
{urls: ["<all_urls>"]});
},
});
yield extension.startup();
try {
yield fetch("http://example.com/");
ok(false, "Fetch should have failed.");
} catch (e) {
ok(true, "Got expected error.");
}
yield extension.awaitMessage("got-before-send-headers");
yield extension.awaitMessage("got-error-occurred");
yield extension.unload();
chromeScript.destroy();
});
// Test that response header modifications take effect before onStartRequest fires.
add_task(function* test_set_responseHeaders() {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: [
"webRequest",
"webRequestBlocking",
"http://example.com/",
],
},
background() {
browser.webRequest.onHeadersReceived.addListener(
details => {
browser.test.log(`onHeadersReceived({url: ${details.url}})`);
details.responseHeaders.push({name: "foo", value: "bar"});
return {responseHeaders: details.responseHeaders};
},
{urls: ["http://example.com/?modify_headers"]},
["blocking", "responseHeaders"]);
},
});
yield extension.startup();
yield new Promise(resolve => setTimeout(resolve, 0));
let chromeScript = SpecialPowers.loadChromeScript(() => {
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
let ssm = Services.scriptSecurityManager;
let channel = NetUtil.newChannel({
uri: "http://example.com/?modify_headers",
loadingPrincipal: ssm.createCodebasePrincipalFromOrigin("http://example.com"),
contentPolicyType: Ci.nsIContentPolicy.TYPE_XMLHTTPREQUEST,
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
});
channel.asyncOpen2({
QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener]),
onStartRequest(request, context) {
request.QueryInterface(Ci.nsIHttpChannel);
try {
sendAsyncMessage("response-header-foo", request.getResponseHeader("foo"));
} catch (e) {
sendAsyncMessage("response-header-foo", null);
}
request.cancel(Cr.NS_BINDING_ABORTED);
},
onStopRequest() {
},
onDataAvailable() {
throw new Components.Exception("", Cr.NS_ERROR_FAILURE);
},
});
});
let headerValue = yield chromeScript.promiseOneMessage("response-header-foo");
is(headerValue, "bar", "Expected Foo header value");
yield extension.unload();
chromeScript.destroy();
});
</script>
</body>
</html>
|