Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest / test_declarative2.js
blobfd23a658785b29c769ce4dd9d74e7ca289bfe56a
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;
22 var IgnoreRules =
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.
42   return getServerURL(
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&' +
52                       'deletedCookie=Foo');
55 function getURLEchoCookie() {
56   return getServerURL('echoheader?Cookie');
59 function getURLHttpXHRData() {
60   return getServerURL("extensions/api_test/webrequest/xhr/data.json",
61                       "b.com");
64 runTests([
66   function testSetRequestHeader() {
67     ignoreUnexpected = true;
68     expect();  // Used for initialization.
69     onRequest.addRules(
70       [{conditions: [new RequestMatcher()],
71         actions: [new SetRequestHeader({name: "User-Agent", value: "FoobarUA"})]
72        }],
73       function() {
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.");
78           });
79           chrome.tabs.executeScript(tabId,
80             {
81               code: "chrome.extension.sendRequest(" +
82                     "{pass: document.body.innerText.indexOf('FoobarUA') >= 0});"
83             });
84         });
85       });
86   },
88   function testRemoveRequestHeader() {
89     ignoreUnexpected = true;
90     expect();  // Used for initialization.
91     onRequest.addRules(
92       [{conditions: [new RequestMatcher()],
93         actions: [new RemoveRequestHeader({name: "user-AGENT"})]
94        }],
95       function() {
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.");
100           });
101           chrome.tabs.executeScript(tabId,
102             {
103               code: "chrome.extension.sendRequest(" +
104                     "{pass: document.body.innerText.indexOf('Mozilla') == -1});"
105             });
106         });
107       });
108   },
110   function testAddResponseHeader() {
111     ignoreUnexpected = true;
112     expect();  // Used for initialization.
113     onRequest.addRules(
114       [{conditions: [new RequestMatcher()],
115         actions: [new AddResponseHeader({name: "Set-Cookie", value: "Bar=baz"})]
116        }],
117       function() {
118         navigateAndWait(getURLEchoUserAgent(), function() {
119           chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
120             chrome.test.assertTrue(request.pass, "Cookie was not added.");
121           });
122           chrome.tabs.executeScript(tabId,
123             {
124               code: "chrome.extension.sendRequest(" +
125                     "{pass: document.cookie.indexOf('Bar') != -1});"
126             });
127         });
128       });
129   },
131   function testRemoveResponseHeader() {
132     ignoreUnexpected = true;
133     expect();  // Used for initialization.
134     onRequest.addRules(
135       [{conditions: [new RequestMatcher()],
136         actions: [new RemoveResponseHeader({name: "Set-Cookie",
137                                             value: "FoO=bAR"})]
138        }],
139       function() {
140         navigateAndWait(getURLSetCookie(), function() {
141           chrome.test.listenOnce(chrome.extension.onRequest, function(request) {
142             chrome.test.assertTrue(request.pass, "Cookie was not removed.");
143           });
144           chrome.tabs.executeScript(tabId,
145             {
146               code: "chrome.extension.sendRequest(" +
147                     "{pass: document.cookie.indexOf('Foo') == -1});"
148             });
149         });
150       });
151   },
153   function testPriorities() {
154     ignoreUnexpected = true;
155     expect(
156       [
157         { label: "onCompleted",
158           event: "onCompleted",
159           details: {
160             url: getURLHttpSimple(),
161             statusCode: 200,
162             fromCache: false,
163             statusLine: "HTTP/1.1 200 OK",
164             ip: "127.0.0.1",
165           }
166         }
167       ],
168       [ ["onCompleted"] ]);
170     onRequest.addRules(
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})],
175          priority: 200}
176       ],
177       function() {navigateAndWait(getURLHttpSimple());}
178     );
179   },
181   function testEditRequestCookies() {
182     ignoreUnexpected = true;
183     expect();
184     var cookie1 = {name: "requestCookie1", value: "foo"};
185     var cookie2 = {name: "requestCookie2", value: "foo"};
186     onRequest.addRules(
187       [ {conditions: [new RequestMatcher({})],
188          actions: [
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"}})
196          ]}
197       ],
198       function() {
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));
203           });
204           chrome.tabs.executeScript(tabId, {code:
205               "function hasCookie(name, value) {" +
206               "  var entry = name + '=' + value;" +
207               "  return document.body.innerText.indexOf(entry) >= 0;" +
208               "};" +
209               "var result = {};" +
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);"});
215         });
216       }
217     );
218   },
220   function testEditResponseCookies() {
221     ignoreUnexpected = true;
222     expect();
223     onRequest.addRules(
224       [ {conditions: [new RequestMatcher({})],
225          actions: [
226            new AddResponseCookie({cookie: {name: "addedCookie", value: "Foo"}}),
227            new EditResponseCookie({filter: {name: "editedCookie"},
228                                    modification: {value: "bar"}}),
229            new RemoveResponseCookie({filter: {name: "deletedCookie"}})
230          ]}
231       ],
232       function() {
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));
237           });
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];" +
244               "}" +
245               "var result = {};" +
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);"});
252         });
253       }
254     );
255   },
257   function testRequestHeaders() {
258     ignoreUnexpected = true;
259     expect(
260       [
261         { label: "onErrorOccurred",
262           event: "onErrorOccurred",
263           details: {
264             url: getURLHttpSimple(),
265             fromCache: false,
266             error: "net::ERR_BLOCKED_BY_CLIENT"
267           }
268         },
269       ],
270       [ ["onErrorOccurred"] ]);
271     onRequest.addRules(
272       [ {'conditions': [
273            new RequestMatcher({
274              'url': {
275                  'pathSuffix': ".html",
276                  'ports': [testServerPort, [1000, 2000]],
277                  'schemes': ["http"]
278              },
279              'requestHeaders': [{ nameContains: "" }],
280              'excludeRequestHeaders': [{ valueContains: ["", "value123"] }]
281               })],
282          'actions': [new CancelRequest()]}
283       ],
284       function() {navigateAndWait(getURLHttpSimple());}
285     );
286   },
287   ]);