Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest / test_api.js
blob399a7ca25d2b5154289a6b39867d0503fb179631
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.
5 chrome.test.runTests([
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();
26   },
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); };
33     }
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();
52   },
54   // Tests that the extra parameters to addListener are checked for invalid
55   // values.
56   function specialEvents() {
57     var goodFilter = {urls: ["http://*.google.com/*"]};
58     var goodExtraInfo = ["blocking"];
59     chrome.webRequest.onBeforeRequest.addListener(
60         function(details) {},
61         goodFilter, goodExtraInfo);
63     // Try a bad RequestFilter.
64     try {
65       chrome.webRequest.onBeforeRequest.addListener(
66           function(details) {},
67           {badFilter: 42, urls: ["<all_urls>"]}, goodExtraInfo);
68       chrome.test.fail();
69     } catch (e) {
70       chrome.test.assertTrue(e.message.search("Invalid value") >= 0);
71     }
73     // Try a bad ExtraInfoSpec.
74     try {
75       chrome.webRequest.onBeforeRequest.addListener(
76           function(details) {},
77           goodFilter, ["badExtraInfo"]);
78       chrome.test.fail();
79     } catch (e) {
80       chrome.test.assertTrue(e.message.search("Invalid value") >= 0);
81     }
83     // This extraInfoSpec should only work for onBeforeSendHeaders.
84     var headersExtraInfo = ["requestHeaders"];
85     chrome.webRequest.onBeforeSendHeaders.addListener(
86         function(details) {},
87         goodFilter, headersExtraInfo);
88     try {
89       chrome.webRequest.onBeforeRequest.addListener(
90           function(details) {},
91           goodFilter, headersExtraInfo);
92       chrome.test.fail();
93     } catch (e) {
94       chrome.test.assertTrue(e.message.search("Invalid value") >= 0);
95     }
97     // ExtraInfoSpec with "responseHeaders" should work for onCompleted.
98     headersExtraInfo = ["responseHeaders"];
99     chrome.webRequest.onCompleted.addListener(
100         function(details) {},
101         goodFilter, headersExtraInfo);
102     try {
103       chrome.webRequest.onBeforeRequest.addListener(
104           function(details) {},
105           goodFilter, headersExtraInfo);
106       chrome.test.fail();
107     } catch (e) {
108       chrome.test.assertTrue(e.message.search("Invalid value") >= 0);
109     }
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(
115         emptyCallback,
116         {urls: ["badpattern://*"]});
117     chrome.webRequest.onBeforeRequest.removeListener(
118         emptyCallback);
120     chrome.test.succeed();
121   },
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"}
129       ];
130       return {responseHeaders: responseHeaders};
131     };
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();
151       };
152       xhr.onerror = function() {
153         chrome.webRequest.onHeadersReceived.removeListener(
154             callbackWithBadHeadersResponse);
155         chrome.test.fail();
156       };
157       xhr.send();
158     });
159   }