Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest / test_declarative1.js
blob591f329c4253d49846f62c8daef266dddec2a6f8
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 getURLHttpSimple() {
33   return getServerURL("extensions/api_test/webrequest/simpleLoad/a.html");
36 function getURLHttpSimpleB() {
37   return getServerURL("extensions/api_test/webrequest/simpleLoad/b.html");
40 function getURLHttpComplex() {
41   return getServerURL(
42       "extensions/api_test/webrequest/complexLoad/a.html");
45 function getURLHttpRedirectTest() {
46   return getServerURL(
47       "extensions/api_test/webrequest/declarative/a.html");
50 function getURLHttpWithHeaders() {
51   return getServerURL(
52       "extensions/api_test/webrequest/declarative/headers.html");
55 function getURLOfHTMLWithThirdParty() {
56   // Returns the URL of a HTML document with a third-party resource.
57   return getServerURL(
58       "extensions/api_test/webrequest/declarative/third-party.html");
61 // Shared test sections.
62 function cancelThirdPartyExpected() {
63     return [
64       { label: "onBeforeRequest",
65         event: "onBeforeRequest",
66         details: {
67           url: getURLOfHTMLWithThirdParty(),
68           frameUrl: getURLOfHTMLWithThirdParty()
69         }
70       },
71       { label: "onBeforeSendHeaders",
72         event: "onBeforeSendHeaders",
73         details: {url: getURLOfHTMLWithThirdParty()}
74       },
75       { label: "onSendHeaders",
76         event: "onSendHeaders",
77         details: {url: getURLOfHTMLWithThirdParty()}
78       },
79       { label: "onHeadersReceived",
80         event: "onHeadersReceived",
81         details: {
82           url: getURLOfHTMLWithThirdParty(),
83           statusLine: "HTTP/1.1 200 OK"
84         }
85       },
86       { label: "onResponseStarted",
87         event: "onResponseStarted",
88         details: {
89           url: getURLOfHTMLWithThirdParty(),
90           fromCache: false,
91           ip: "127.0.0.1",
92           statusCode: 200,
93           statusLine: "HTTP/1.1 200 OK"
94         }
95       },
96       { label: "onCompleted",
97         event: "onCompleted",
98         details: {
99           fromCache: false,
100           ip: "127.0.0.1",
101           url: getURLOfHTMLWithThirdParty(),
102           statusCode: 200,
103           statusLine: "HTTP/1.1 200 OK"
104         }
105       },
106       { label: "img-onBeforeRequest",
107         event: "onBeforeRequest",
108         details: {
109           type: "image",
110           url: "http://non_existing_third_party.com/image.png",
111           frameUrl: getURLOfHTMLWithThirdParty()
112         }
113       },
114       { label: "img-onErrorOccurred",
115         event: "onErrorOccurred",
116         details: {
117           error: "net::ERR_BLOCKED_BY_CLIENT",
118           fromCache: false,
119           type: "image",
120           url: "http://non_existing_third_party.com/image.png"
121         }
122       },
123     ];
126 function cancelThirdPartyExpectedOrder() {
127     return [
128       ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
129        "onHeadersReceived", "onResponseStarted", "onCompleted"],
130       ["img-onBeforeRequest", "img-onErrorOccurred"]
131     ];
134 runTests([
136   function testCancelRequest() {
137     ignoreUnexpected = true;
138     expect(
139       [
140         { label: "onErrorOccurred",
141           event: "onErrorOccurred",
142           details: {
143             url: getURLHttpWithHeaders(),
144             fromCache: false,
145             error: "net::ERR_BLOCKED_BY_CLIENT"
146           }
147         },
148       ],
149       [ ["onErrorOccurred"] ]);
150     onRequest.addRules(
151       [ {'conditions': [
152            new RequestMatcher({
153              'url': {
154                  'pathSuffix': ".html",
155                  'ports': [testServerPort, [1000, 2000]],
156                  'schemes': ["http"]
157              },
158              'resourceType': ["main_frame"],
159              'contentType': ["text/plain"],
160              'excludeContentType': ["image/png"],
161              'responseHeaders': [{ nameContains: ["content", "type"] }],
162              'excludeResponseHeaders': [{ valueContains: "nonsense" }],
163              'stages': ["onHeadersReceived", "onAuthRequired"] })],
164          'actions': [new CancelRequest()]}
165       ],
166       function() {navigateAndWait(getURLHttpWithHeaders());}
167     );
168   },
170   // Postpone cancelling of the request until onHeadersReceived by using
171   // 'stages'. If not for the stages, the request would be already cancelled
172   // during onBeforeRequest.
173   function testPostponeCancelRequest() {
174     ignoreUnexpected = false;
175     expect(
176       [
177         { label: "onBeforeRequest",
178           event: "onBeforeRequest",
179           details: {
180             url: getURLHttpWithHeaders(),
181             frameUrl: getURLHttpWithHeaders()
182           }
183         },
184         { label: "onBeforeSendHeaders",
185           event: "onBeforeSendHeaders",
186           details: {
187             url: getURLHttpWithHeaders(),
188           }
189         },
190         { label: "onSendHeaders",
191           event: "onSendHeaders",
192           details: {
193             url: getURLHttpWithHeaders(),
194           }
195         },
196         { label: "onHeadersReceived",
197           event: "onHeadersReceived",
198           details: {
199             statusLine: "HTTP/1.1 200 OK",
200             url: getURLHttpWithHeaders(),
201           }
202         },
203         { label: "onErrorOccurred",
204           event: "onErrorOccurred",
205           details: {
206             url: getURLHttpWithHeaders(),
207             fromCache: false,
208             error: "net::ERR_BLOCKED_BY_CLIENT"
209           }
210         },
211       ],
212       [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
213          "onHeadersReceived", "onErrorOccurred"] ]);
214     onRequest.addRules(
215       [ {'conditions': [
216            new RequestMatcher({ 'stages': ["onHeadersReceived"] })],
217          'actions': [new CancelRequest()]}
218       ],
219       function() {navigateAndWait(getURLHttpWithHeaders());}
220     );
221   },
222   // Tests that "thirdPartyForCookies: true" matches third party requests.
223   function testThirdParty() {
224     ignoreUnexpected = false;
225     expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
226     onRequest.addRules(
227       [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})],
228          'actions': [new chrome.declarativeWebRequest.CancelRequest()]},],
229       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
230     );
231   },
233   // Tests that "thirdPartyForCookies: false" matches first party requests,
234   // by cancelling all requests, and overriding the cancelling rule only for
235   // requests matching "thirdPartyForCookies: false".
236   function testFirstParty() {
237     ignoreUnexpected = false;
238     expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
239     onRequest.addRules(
240       [ {'priority': 2,
241          'conditions': [
242            new RequestMatcher({thirdPartyForCookies: false})
243          ],
244          'actions': [
245            new chrome.declarativeWebRequest.IgnoreRules({
246               lowerPriorityThan: 2 })
247          ]
248         },
249         {'priority': 1,
250          'conditions': [new RequestMatcher({})],
251          'actions': [new chrome.declarativeWebRequest.CancelRequest()]
252         },
253       ],
254       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
255     );
256   },
258   function testFirstPartyForCookiesUrl() {
259     // This is an end-to-end test for firstPartyForCookies. The choice of URL to
260     // navigate to is purely arbitrary.
261     ignoreUnexpected = false;
262     expect(
263       [
264         { label: "onBeforeRequest",
265           event: "onBeforeRequest",
266           details: {
267             url: getURLOfHTMLWithThirdParty(),
268             frameUrl: getURLOfHTMLWithThirdParty()
269           }
270         },
271         { label: "onErrorOccurred",
272           event: "onErrorOccurred",
273           details: {
274             url: getURLOfHTMLWithThirdParty(),
275             fromCache: false,
276             error: "net::ERR_BLOCKED_BY_CLIENT"
277           }
278         },
279       ],
280       [ ["onBeforeRequest", "onErrorOccurred"] ]);
281     onRequest.addRules(
282       [ {'conditions': [
283            new RequestMatcher({
284              firstPartyForCookiesUrl: {
285                hostEquals: testServer
286              }
287            })
288          ],
289          'actions': [new chrome.declarativeWebRequest.CancelRequest()]
290         },
291       ],
292       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
293     );
294   },
296   function testRedirectRequest() {
297     ignoreUnexpected = true;
298     expect(
299       [
300         { label: "onBeforeRequest-a",
301           event: "onBeforeRequest",
302           details: {
303             type: "main_frame",
304             url: getURLHttpComplex(),
305             frameUrl: getURLHttpComplex()
306           },
307         },
308         { label: "onBeforeRedirect",
309           event: "onBeforeRedirect",
310           details: {
311             url: getURLHttpComplex(),
312             redirectUrl: getURLHttpSimple(),
313             fromCache: false,
314             statusLine: "HTTP/1.1 307 Internal Redirect",
315             statusCode: 307,
316           }
317         },
318         { label: "onBeforeRequest-b",
319           event: "onBeforeRequest",
320           details: {
321             type: "main_frame",
322             url: getURLHttpSimple(),
323             frameUrl: getURLHttpSimple(),
324           },
325         },
326         { label: "onCompleted",
327           event: "onCompleted",
328           details: {
329             ip: "127.0.0.1",
330             url: getURLHttpSimple(),
331             fromCache: false,
332             statusCode: 200,
333             statusLine: "HTTP/1.1 200 OK",
334           }
335         },
336       ],
337       [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
338          "onCompleted"] ]);
340     onRequest.addRules(
341       [ {'conditions': [new RequestMatcher({'url': {'pathSuffix': ".html"}})],
342          'actions': [
343              new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
344       ],
345       function() {navigateAndWait(getURLHttpComplex());}
346     );
347   },
349   function testRedirectRequest2() {
350     ignoreUnexpected = true;
351     expect(
352       [
353         { label: "onCompleted",
354           event: "onCompleted",
355           details: {
356             ip: "127.0.0.1",
357             url: getURLHttpRedirectTest(),
358             fromCache: false,
359             statusCode: 200,
360             statusLine: "HTTP/1.1 200 OK",
361           }
362         },
363         // We cannot wait for onCompleted signals because these are not sent
364         // for data:// URLs.
365         { label: "onBeforeRedirect-1",
366           event: "onBeforeRedirect",
367           details: {
368             url: getServerURL(
369                 "extensions/api_test/webrequest/declarative/image.png"),
370             redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA" +
371                 "AAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJ" +
372                 "ggg==",
373             fromCache: false,
374             statusLine: "HTTP/1.1 307 Internal Redirect",
375             statusCode: 307,
376             type: "image",
377           }
378         },
379         { label: "onBeforeRedirect-2",
380           event: "onBeforeRedirect",
381           details: {
382             frameId: 1,
383             parentFrameId: 0,
384             url: getServerURL(
385                 "extensions/api_test/webrequest/declarative/frame.html"),
386             redirectUrl: "data:text/html,",
387             fromCache: false,
388             statusLine: "HTTP/1.1 307 Internal Redirect",
389             statusCode: 307,
390             type: "sub_frame",
391           }
392         },
393       ],
394       [ ["onCompleted"], ["onBeforeRedirect-1"], ["onBeforeRedirect-2"] ]);
396     onRequest.addRules(
397       [ {conditions: [
398              new RequestMatcher({url: {pathSuffix: "image.png"}})],
399          actions: [new RedirectToTransparentImage()]},
400         {conditions: [
401              new RequestMatcher({url: {pathSuffix: "frame.html"}})],
402          actions: [new RedirectToEmptyDocument()]},
403       ],
404       function() {navigateAndWait(getURLHttpRedirectTest());}
405     );
406   },
408   // Tests that a request is redirected during the onHeadersReceived stage
409   // when the conditions include a RequestMatcher with a contentType.
410   function testRedirectRequestByContentType() {
411     ignoreUnexpected = true;
412     expect(
413       [
414         { label: "onBeforeRequest-a",
415           event: "onBeforeRequest",
416           details: {
417             type: "main_frame",
418             url: getURLHttpWithHeaders(),
419             frameUrl: getURLHttpWithHeaders()
420           },
421         },
422         { label: "onBeforeRedirect",
423           event: "onBeforeRedirect",
424           details: {
425             url: getURLHttpWithHeaders(),
426             redirectUrl: getURLHttpSimple(),
427             statusLine: "HTTP/1.1 302 Found",
428             statusCode: 302,
429             fromCache: false,
430             ip: "127.0.0.1",
431           }
432         },
433         { label: "onBeforeRequest-b",
434           event: "onBeforeRequest",
435           details: {
436             type: "main_frame",
437             url: getURLHttpSimple(),
438             frameUrl: getURLHttpSimple(),
439           },
440         },
441         { label: "onCompleted",
442           event: "onCompleted",
443           details: {
444             ip: "127.0.0.1",
445             url: getURLHttpSimple(),
446             fromCache: false,
447             statusCode: 200,
448             statusLine: "HTTP/1.1 200 OK",
449           }
450         },
451       ],
452       [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
453          "onCompleted"] ]);
455     onRequest.addRules(
456       [ {'conditions': [new RequestMatcher({'contentType': ["text/plain"]})],
457          'actions': [
458              new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
459       ],
460       function() {navigateAndWait(getURLHttpWithHeaders());}
461     );
462   },
464   function testRedirectByRegEx() {
465     ignoreUnexpected = true;
466     expect(
467       [
468         { label: "onCompleted",
469           event: "onCompleted",
470           details: {
471             ip: "127.0.0.1",
472             url: getURLHttpSimpleB(),
473             fromCache: false,
474             statusCode: 200,
475             statusLine: "HTTP/1.1 200 OK",
476           }
477         },
478       ],
479       [ ["onCompleted"] ]);
481     onRequest.addRules(
482       [ {conditions: [new RequestMatcher({url: {pathSuffix: ".html"}})],
483          actions: [
484              new RedirectByRegEx({from: "^(.*)/a.html$", to: "$1/b.html"})]}
485       ],
486       function() {navigateAndWait(getURLHttpSimple());}
487     );
488   },
490   function testRegexFilter() {
491     ignoreUnexpected = true;
492     expect(
493       [
494         { label: "onErrorOccurred",
495           event: "onErrorOccurred",
496           details: {
497             url: getURLHttpSimple(),
498             fromCache: false,
499             error: "net::ERR_BLOCKED_BY_CLIENT"
500           }
501         },
502       ],
503       [ ["onErrorOccurred"] ]);
504     onRequest.addRules(
505       [ {'conditions': [
506            new RequestMatcher({
507              'url': {
508                  'urlMatches': 'simple[A-Z].*a\.html$',
509                  'schemes': ["http"]
510              },
511            })],
512          'actions': [new CancelRequest()]}
513       ],
514       function() {navigateAndWait(getURLHttpSimple());}
515     );
516   },
517   ]);