1 // Copyright (c) 2012 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 var declarative = chrome.declarative;
7 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher;
8 var CancelRequest = chrome.declarativeWebRequest.CancelRequest;
9 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest;
10 var SetRequestHeader = chrome.declarativeWebRequest.SetRequestHeader;
13 // No 'id', this should be filled by the API.
14 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}}),
15 new RequestMatcher({url: {hostPrefix: "test2"}})],
16 actions: [new CancelRequest(),
17 new RedirectRequest({redirectUrl: "http://foobar.com"})]
18 // No 'priority', this should be filled by the API.
23 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}}),
24 new RequestMatcher({url: {hostPrefix: "test2"}})],
25 actions: [new CancelRequest(),
26 new RedirectRequest({redirectUrl: "http://foobar.com"})],
37 var outputRule1 = inputRule1;
40 // No 'id', this should be filled by the API.
41 conditions: [new RequestMatcher({url: {hostPrefix: "test3"}})],
42 actions: [new CancelRequest()]
43 // No 'priority', this should be filled by the API.
48 conditions: [new RequestMatcher({url: {hostPrefix: "test3"}})],
49 actions: [new CancelRequest()],
54 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})]
55 // "actions" is missing but not optional.
59 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})],
60 // "actions" contains an invalid action (separate test because this validation
61 // happens on a different code path).
62 actions: [{key: "value"}]
65 conditions: [new RequestMatcher({url: {hostPrefix: "test1"}})],
66 actions: [new SetRequestHeader({name: '\x00', value: 'whatever'})]
69 var testEvent = chrome.declarativeWebRequest.onRequest;
71 chrome.test.runTests([
73 function testInvalidAddRules() {
78 chrome.test.succeed();
81 function testInvalidGetRules() {
83 testEvent.getRules(function() {});
86 chrome.test.succeed();
89 function testInvalidRemoveRules() {
91 testEvent.removeRules(function() {});
94 chrome.test.succeed();
97 // Add adding two simple rules and check that their optional fields are set
98 // correctly in the call back function.
99 function testAddRules() {
100 var callback = function(rules) {
101 chrome.test.assertNoLastError();
102 chrome.test.assertEq(2, rules.length);
103 // API should have generated id and priority fields.
104 chrome.test.assertTrue("id" in rules[0]);
105 chrome.test.assertEq([outputRule0, outputRule1], rules);
106 chrome.test.succeed();
108 testEvent.addRules([inputRule0, inputRule1], callback);
110 // Check that getRules() returns all rules if no filter is passed.
111 function testGetRules() {
112 var callback = function(rules) {
113 chrome.test.assertNoLastError();
114 // We are not given any gurantee on the order in which rules are returned.
115 chrome.test.assertTrue(
116 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) ||
117 chrome.test.checkDeepEq([outputRule1, outputRule0], rules));
118 chrome.test.succeed();
120 testEvent.getRules(null, callback);
122 // Check that getRules() returns all rules if no filter is passed.
123 function testGetRules2() {
124 var callback = function(rules) {
125 chrome.test.assertNoLastError();
126 // We are not given any gurantee on the order in which rules are returned.
127 chrome.test.assertTrue(
128 chrome.test.checkDeepEq([outputRule0, outputRule1], rules) ||
129 chrome.test.checkDeepEq([outputRule1, outputRule0], rules));
130 chrome.test.succeed();
132 testEvent.getRules(undefined, callback);
134 // Check that getRules() returns no rules if empty filter is passed.
135 function testGetRules3() {
136 var callback = function(rules) {
137 chrome.test.assertNoLastError();
138 // We are not given any gurantee on the order in which rules are returned.
139 chrome.test.assertEq([], rules);
140 chrome.test.succeed();
142 testEvent.getRules([], callback);
144 // Check that getRules() returns all rules if rules are filtered by ID.
145 function testSelectiveGetRules() {
146 var callback = function(rules) {
147 chrome.test.assertNoLastError();
148 chrome.test.assertEq([outputRule1], rules);
149 chrome.test.succeed();
151 testEvent.getRules(["my_rule_id"], callback);
153 // Check that we can remove individual rules.
154 function testSelectiveRemoveRules() {
155 var callback = function(rules) {
156 chrome.test.assertNoLastError();
157 chrome.test.succeed();
159 testEvent.removeRules(["my_rule_id"], callback);
161 // Check that after removal, the rules are really gone.
162 function testGetRemainingRules() {
163 var callback = function(rules) {
164 chrome.test.assertNoLastError();
165 chrome.test.assertEq([outputRule0], rules);
166 chrome.test.succeed();
168 testEvent.getRules(null, callback);
170 // Check that rules are assigned unique IDs.
171 function testIdGeneration() {
172 var callback = function(rules) {
173 chrome.test.assertNoLastError();
174 chrome.test.assertEq(1, rules.length);
175 // API should have generated id and priority fields.
176 chrome.test.assertTrue("id" in rules[0]);
177 // The IDs should be distinct.
178 chrome.test.assertFalse(outputRule0["id"] === rules[0]["id"]);
179 chrome.test.succeed();
181 testEvent.addRules([inputRule2], callback);
183 // Check that we can remove all rules at once.
184 function testRemovingAllRules() {
185 var callback = function() {
186 chrome.test.assertNoLastError();
187 chrome.test.succeed();
189 testEvent.removeRules(null, callback);
191 // Check that the rules are actually gone.
192 function testAllRulesRemoved() {
193 var callback = function(rules) {
194 chrome.test.assertNoLastError();
195 chrome.test.assertEq(0, rules.length);
196 chrome.test.succeed();
198 testEvent.getRules(null, callback);
200 // Check that validation is performed.
201 function testValidation() {
202 var fail = function() {
203 chrome.test.fail("An exception was expected");
206 testEvent.addRules([invalidRule0], fail);
210 testEvent.addRules([invalidRule1], fail);
213 // None of these rules should have been registered.
214 var callback = function(rules) {
215 chrome.test.assertNoLastError();
216 chrome.test.assertEq(0, rules.length);
217 chrome.test.succeed();
219 testEvent.getRules(null, callback);
221 // Check that errors are propagated
222 function testValidationAsync() {
223 var callback = function() {
224 chrome.test.assertLastError('Invalid header name.');
225 chrome.test.succeed();
227 testEvent.addRules([invalidRule2], callback);
229 // Finally we add one additional rule, to check that is is removed
231 function testAddRules() {
232 var callback = function(rules) {
233 chrome.test.assertNoLastError();
234 chrome.test.assertEq(1, rules.length);
235 chrome.test.succeed();
237 testEvent.addRules([inputRule0], callback);