1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 function addListeners() {
7 chrome
.webRequest
.onBeforeRequest
.addListener(
8 function(details
) {}, {urls
: ["<all_urls>"]});
9 chrome
.webRequest
.onBeforeSendHeaders
.addListener(
10 function(details
) {}, {urls
: ["<all_urls>"]});
11 chrome
.webRequest
.onSendHeaders
.addListener(
12 function(details
) {}, {urls
: ["<all_urls>"]});
13 chrome
.webRequest
.onHeadersReceived
.addListener(
14 function(details
) {}, {urls
: ["<all_urls>"]});
15 chrome
.webRequest
.onBeforeRedirect
.addListener(
16 function(details
) {}, {urls
: ["<all_urls>"]});
17 chrome
.webRequest
.onResponseStarted
.addListener(
18 function(details
) {}, {urls
: ["<all_urls>"]});
19 chrome
.webRequest
.onCompleted
.addListener(
20 function(details
) {}, {urls
: ["<all_urls>"]});
21 chrome
.webRequest
.onErrorOccurred
.addListener(
22 function(details
) {}, {urls
: ["<all_urls>"]});
23 chrome
.webRequest
.onAuthRequired
.addListener(
24 function(details
) {}, {urls
: ["<all_urls>"]});
25 chrome
.test
.succeed();
28 // Tests that we can remove a listener and it goes away.
29 // http://crbug.com/96755
30 function removeListeners() {
31 function newCallback(value
) {
32 return function(details
) { console
.log(value
); };
34 var cb1
= newCallback(1);
35 var cb2
= newCallback(2);
36 var event
= chrome
.webRequest
.onBeforeRequest
;
37 event
.addListener(cb1
, {urls
: ["<all_urls>"]});
38 event
.addListener(cb2
, {urls
: ["<all_urls>"]});
39 chrome
.test
.assertTrue(event
.hasListener(cb1
));
40 chrome
.test
.assertTrue(event
.hasListener(cb2
));
41 event
.removeListener(cb1
);
42 chrome
.test
.assertFalse(event
.hasListener(cb1
));
43 chrome
.test
.assertTrue(event
.hasListener(cb2
));
44 event
.removeListener(cb2
);
45 chrome
.test
.assertFalse(event
.hasListener(cb1
));
46 chrome
.test
.assertFalse(event
.hasListener(cb2
));
47 event
.addListener(cb1
, {urls
: ["http://foo/*"]});
48 event
.addListener(cb1
, {urls
: ["http://bar/*"]});
49 event
.removeListener(cb1
);
50 chrome
.test
.assertFalse(event
.hasListener(cb1
));
51 chrome
.test
.succeed();
54 // Tests that the extra parameters to addListener are checked for invalid
56 function specialEvents() {
57 var goodFilter
= {urls
: ["http://*.google.com/*"]};
58 var goodExtraInfo
= ["blocking"];
59 chrome
.webRequest
.onBeforeRequest
.addListener(
61 goodFilter
, goodExtraInfo
);
63 // Try a bad RequestFilter.
65 chrome
.webRequest
.onBeforeRequest
.addListener(
67 {badFilter
: 42, urls
: ["<all_urls>"]}, goodExtraInfo
);
70 chrome
.test
.assertTrue(e
.message
.search("Invalid value") >= 0);
73 // Try a bad ExtraInfoSpec.
75 chrome
.webRequest
.onBeforeRequest
.addListener(
77 goodFilter
, ["badExtraInfo"]);
80 chrome
.test
.assertTrue(e
.message
.search("Invalid value") >= 0);
83 // This extraInfoSpec should only work for onBeforeSendHeaders.
84 var headersExtraInfo
= ["requestHeaders"];
85 chrome
.webRequest
.onBeforeSendHeaders
.addListener(
87 goodFilter
, headersExtraInfo
);
89 chrome
.webRequest
.onBeforeRequest
.addListener(
91 goodFilter
, headersExtraInfo
);
94 chrome
.test
.assertTrue(e
.message
.search("Invalid value") >= 0);
97 // ExtraInfoSpec with "responseHeaders" should work for onCompleted.
98 headersExtraInfo
= ["responseHeaders"];
99 chrome
.webRequest
.onCompleted
.addListener(
100 function(details
) {},
101 goodFilter
, headersExtraInfo
);
103 chrome
.webRequest
.onBeforeRequest
.addListener(
104 function(details
) {},
105 goodFilter
, headersExtraInfo
);
108 chrome
.test
.assertTrue(e
.message
.search("Invalid value") >= 0);
111 // Try a bad URL pattern. The error happens asynchronously. We're just
112 // verifying that the browser doesn't crash.
113 var emptyCallback = function (details
) {};
114 chrome
.webRequest
.onBeforeRequest
.addListener(
116 {urls
: ["badpattern://*"]});
117 chrome
.webRequest
.onBeforeRequest
.removeListener(
120 chrome
.test
.succeed();
123 // Checks that a header with NUL bytes does not cause a crash.
124 // Regression test for http://crbug.com/348417
125 function badResponseHeaderDoesNotCauseCrash() {
126 var callbackWithBadHeadersResponse = function (details
) {
127 var responseHeaders
= [
128 {name
: "X-Header-With-Invalid-Value", value
: "\x00"}
130 return {responseHeaders
: responseHeaders
};
132 chrome
.webRequest
.onHeadersReceived
.addListener(
133 callbackWithBadHeadersResponse
,
134 {types
: ["xmlhttprequest"], urls
: ["*://*/*"]},
135 ["blocking", "responseHeaders"]);
137 chrome
.test
.getConfig(function(config
) {
138 var url
= 'http://127.0.0.1:' + config
.testServer
.port
+ '/simple.html';
139 var xhr
= new XMLHttpRequest();
140 xhr
.open('GET', url
);
141 xhr
.onload = function() {
142 chrome
.webRequest
.onHeadersReceived
.removeListener(
143 callbackWithBadHeadersResponse
);
144 var responseHeaders
= xhr
.getAllResponseHeaders() || '';
145 chrome
.test
.assertTrue(
146 responseHeaders
.indexOf('X-Header-With-Invalid-Value') === -1);
147 // TODO(robwu): If possible, check whether an error with the following
148 // message has been logged to the JavaScript console:
149 // "Header 'X-Header-With-Invalid-Value' has an invalid value"
150 chrome
.test
.succeed();
152 xhr
.onerror = function() {
153 chrome
.webRequest
.onHeadersReceived
.removeListener(
154 callbackWithBadHeadersResponse
);