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 onRequest = chrome.declarativeWebRequest.onRequest;
6 var AddResponseHeader =
7 chrome.declarativeWebRequest.AddResponseHeader;
8 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher;
9 var CancelRequest = chrome.declarativeWebRequest.CancelRequest;
10 var RedirectByRegEx = chrome.declarativeWebRequest.RedirectByRegEx;
11 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest;
12 var RedirectToTransparentImage =
13 chrome.declarativeWebRequest.RedirectToTransparentImage;
14 var RedirectToEmptyDocument =
15 chrome.declarativeWebRequest.RedirectToEmptyDocument;
16 var SetRequestHeader =
17 chrome.declarativeWebRequest.SetRequestHeader;
18 var RemoveRequestHeader =
19 chrome.declarativeWebRequest.RemoveRequestHeader;
20 var RemoveResponseHeader =
21 chrome.declarativeWebRequest.RemoveResponseHeader;
23 chrome.declarativeWebRequest.IgnoreRules;
24 var AddRequestCookie = chrome.declarativeWebRequest.AddRequestCookie;
25 var AddResponseCookie = chrome.declarativeWebRequest.AddResponseCookie;
26 var EditRequestCookie = chrome.declarativeWebRequest.EditRequestCookie;
27 var EditResponseCookie = chrome.declarativeWebRequest.EditResponseCookie;
28 var RemoveRequestCookie = chrome.declarativeWebRequest.RemoveRequestCookie;
29 var RemoveResponseCookie = chrome.declarativeWebRequest.RemoveResponseCookie;
31 // Constants as functions, not to be called until after runTests.
32 function getURLEchoUserAgent() {
33 return getServerURL('echoheader?User-Agent');
36 function getURLHttpSimple() {
37 return getServerURL("extensions/api_test/webrequest/simpleLoad/a.html");
40 function getURLOfHTMLWithThirdParty() {
41 // Returns the URL of a HTML document with a third-party resource.
43 "extensions/api_test/webrequest/declarative/third-party.html");
46 function getURLSetCookie() {
47 return getServerURL('set-cookie?Foo=Bar');
50 function getURLSetCookie2() {
51 return getServerURL('set-cookie?passedCookie=Foo&editedCookie=Foo&' +
55 function getURLEchoCookie() {
56 return getServerURL('echoheader?Cookie');
59 function getURLHttpXHRData() {
60 return getServerURL("extensions/api_test/webrequest/xhr/data.json",
66 function testSetRequestHeader() {
67 ignoreUnexpected = true;
68 expect(); // Used for initialization.
70 [{conditions: [new RequestMatcher()],
71 actions: [new SetRequestHeader({name: "User-Agent", value: "FoobarUA"})]
74 // Check the page content for our modified User-Agent string.
75 navigateAndWait(getURLEchoUserAgent(), function() {
76 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
77 chrome.test.assertTrue(request.pass, "Request header was not set.");
79 chrome.tabs.executeScript(tabId,
81 code: "chrome.extension.sendRequest(" +
82 "{pass: document.body.innerText.indexOf('FoobarUA') >= 0});"
88 function testRemoveRequestHeader() {
89 ignoreUnexpected = true;
90 expect(); // Used for initialization.
92 [{conditions: [new RequestMatcher()],
93 actions: [new RemoveRequestHeader({name: "user-AGENT"})]
96 // Check the page content for our modified User-Agent string.
97 navigateAndWait(getURLEchoUserAgent(), function() {
98 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
99 chrome.test.assertTrue(request.pass, "User-Agent was not removed.");
101 chrome.tabs.executeScript(tabId,
103 code: "chrome.extension.sendRequest(" +
104 "{pass: document.body.innerText.indexOf('Mozilla') == -1});"
110 function testAddResponseHeader() {
111 ignoreUnexpected = true;
112 expect(); // Used for initialization.
114 [{conditions: [new RequestMatcher()],
115 actions: [new AddResponseHeader({name: "Set-Cookie", value: "Bar=baz"})]
118 navigateAndWait(getURLEchoUserAgent(), function() {
119 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
120 chrome.test.assertTrue(request.pass, "Cookie was not added.");
122 chrome.tabs.executeScript(tabId,
124 code: "chrome.extension.sendRequest(" +
125 "{pass: document.cookie.indexOf('Bar') != -1});"
131 function testRemoveResponseHeader() {
132 ignoreUnexpected = true;
133 expect(); // Used for initialization.
135 [{conditions: [new RequestMatcher()],
136 actions: [new RemoveResponseHeader({name: "Set-Cookie",
140 navigateAndWait(getURLSetCookie(), function() {
141 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
142 chrome.test.assertTrue(request.pass, "Cookie was not removed.");
144 chrome.tabs.executeScript(tabId,
146 code: "chrome.extension.sendRequest(" +
147 "{pass: document.cookie.indexOf('Foo') == -1});"
153 function testPriorities() {
154 ignoreUnexpected = true;
157 { label: "onCompleted",
158 event: "onCompleted",
160 url: getURLHttpSimple(),
163 statusLine: "HTTP/1.1 200 OK",
168 [ ["onCompleted"] ]);
171 [ {conditions: [new RequestMatcher({url: {pathContains: "simpleLoad"}})],
172 actions: [new CancelRequest()]},
173 {conditions: [new RequestMatcher({url: {pathContains: "a.html"}})],
174 actions: [new IgnoreRules({lowerPriorityThan: 200})],
177 function() {navigateAndWait(getURLHttpSimple());}
181 function testEditRequestCookies() {
182 ignoreUnexpected = true;
184 var cookie1 = {name: "requestCookie1", value: "foo"};
185 var cookie2 = {name: "requestCookie2", value: "foo"};
187 [ {conditions: [new RequestMatcher({})],
189 // We exploit the fact that cookies are first added, then modified
190 // and finally removed.
191 new AddRequestCookie({cookie: cookie1}),
192 new AddRequestCookie({cookie: cookie2}),
193 new EditRequestCookie({filter: {name: "requestCookie1"},
194 modification: {value: "bar"}}),
195 new RemoveRequestCookie({filter: {name: "requestCookie2"}})
199 navigateAndWait(getURLEchoCookie(), function() {
200 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
201 chrome.test.assertTrue(request.pass, "Invalid cookies. " +
202 JSON.stringify(request.cookies));
204 chrome.tabs.executeScript(tabId, {code:
205 "function hasCookie(name, value) {" +
206 " var entry = name + '=' + value;" +
207 " return document.body.innerText.indexOf(entry) >= 0;" +
210 "result.pass = hasCookie('requestCookie1', 'bar') && " +
211 " !hasCookie('requestCookie1', 'foo') && " +
212 " !hasCookie('requestCookie2', 'foo');" +
213 "result.cookies = document.body.innerText;" +
214 "chrome.extension.sendRequest(result);"});
220 function testEditResponseCookies() {
221 ignoreUnexpected = true;
224 [ {conditions: [new RequestMatcher({})],
226 new AddResponseCookie({cookie: {name: "addedCookie", value: "Foo"}}),
227 new EditResponseCookie({filter: {name: "editedCookie"},
228 modification: {value: "bar"}}),
229 new RemoveResponseCookie({filter: {name: "deletedCookie"}})
233 navigateAndWait(getURLSetCookie2(), function() {
234 chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
235 chrome.test.assertTrue(request.pass, "Invalid cookies. " +
236 JSON.stringify(request.cookies));
238 chrome.tabs.executeScript(tabId, {code:
239 "var cookies = document.cookie.split('; ');" +
240 "var cookieMap = {};" +
241 "for (var i = 0; i < cookies.length; ++i) {" +
242 " var cookieParts = cookies[i].split('=');" +
243 " cookieMap[cookieParts[0]] = cookieParts[1];" +
246 "result.cookies = cookieMap;" +
247 "result.pass = (cookieMap.passedCookie === 'Foo') &&" +
248 " (cookieMap.addedCookie === 'Foo') &&" +
249 " (cookieMap.editedCookie === 'bar') &&" +
250 " !cookieMap.hasOwnProperty('deletedCookie');" +
251 "chrome.extension.sendRequest(result);"});
257 function testRequestHeaders() {
258 ignoreUnexpected = true;
261 { label: "onErrorOccurred",
262 event: "onErrorOccurred",
264 url: getURLHttpSimple(),
266 error: "net::ERR_BLOCKED_BY_CLIENT"
270 [ ["onErrorOccurred"] ]);
275 'pathSuffix': ".html",
276 'ports': [testServerPort, [1000, 2000]],
279 'requestHeaders': [{ nameContains: "" }],
280 'excludeRequestHeaders': [{ valueContains: ["", "value123"] }]
282 'actions': [new CancelRequest()]}
284 function() {navigateAndWait(getURLHttpSimple());}