Elim cr-checkbox
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest / test_declarative1.js
blobad96b3ba4e50413a18c17a33de3b9df802fbed91
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()
71 { label: "onBeforeSendHeaders",
72 event: "onBeforeSendHeaders",
73 details: {url: getURLOfHTMLWithThirdParty()}
75 { label: "onSendHeaders",
76 event: "onSendHeaders",
77 details: {url: getURLOfHTMLWithThirdParty()}
79 { label: "onHeadersReceived",
80 event: "onHeadersReceived",
81 details: {
82 url: getURLOfHTMLWithThirdParty(),
83 statusLine: "HTTP/1.1 200 OK",
84 statusCode: 200
87 { label: "onResponseStarted",
88 event: "onResponseStarted",
89 details: {
90 url: getURLOfHTMLWithThirdParty(),
91 fromCache: false,
92 ip: "127.0.0.1",
93 statusCode: 200,
94 statusLine: "HTTP/1.1 200 OK"
97 { label: "onCompleted",
98 event: "onCompleted",
99 details: {
100 fromCache: false,
101 ip: "127.0.0.1",
102 url: getURLOfHTMLWithThirdParty(),
103 statusCode: 200,
104 statusLine: "HTTP/1.1 200 OK"
107 { label: "img-onBeforeRequest",
108 event: "onBeforeRequest",
109 details: {
110 type: "image",
111 url: "http://non_existing_third_party.com/image.png",
112 frameUrl: getURLOfHTMLWithThirdParty()
115 { label: "img-onErrorOccurred",
116 event: "onErrorOccurred",
117 details: {
118 error: "net::ERR_BLOCKED_BY_CLIENT",
119 fromCache: false,
120 type: "image",
121 url: "http://non_existing_third_party.com/image.png"
127 function cancelThirdPartyExpectedOrder() {
128 return [
129 ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
130 "onHeadersReceived", "onResponseStarted", "onCompleted"],
131 ["img-onBeforeRequest", "img-onErrorOccurred"]
135 runTests([
137 function testCancelRequest() {
138 ignoreUnexpected = true;
139 expect(
141 { label: "onErrorOccurred",
142 event: "onErrorOccurred",
143 details: {
144 url: getURLHttpWithHeaders(),
145 fromCache: false,
146 error: "net::ERR_BLOCKED_BY_CLIENT"
150 [ ["onErrorOccurred"] ]);
151 onRequest.addRules(
152 [ {'conditions': [
153 new RequestMatcher({
154 'url': {
155 'pathSuffix': ".html",
156 'ports': [testServerPort, [1000, 2000]],
157 'schemes': ["http"]
159 'resourceType': ["main_frame"],
160 'contentType': ["text/plain"],
161 'excludeContentType': ["image/png"],
162 'responseHeaders': [{ nameContains: ["content", "type"] }],
163 'excludeResponseHeaders': [{ valueContains: "nonsense" }],
164 'stages': ["onHeadersReceived", "onAuthRequired"] })],
165 'actions': [new CancelRequest()]}
167 function() {navigateAndWait(getURLHttpWithHeaders());}
171 // Postpone cancelling of the request until onHeadersReceived by using
172 // 'stages'. If not for the stages, the request would be already cancelled
173 // during onBeforeRequest.
174 function testPostponeCancelRequest() {
175 ignoreUnexpected = false;
176 expect(
178 { label: "onBeforeRequest",
179 event: "onBeforeRequest",
180 details: {
181 url: getURLHttpWithHeaders(),
182 frameUrl: getURLHttpWithHeaders()
185 { label: "onBeforeSendHeaders",
186 event: "onBeforeSendHeaders",
187 details: {
188 url: getURLHttpWithHeaders(),
191 { label: "onSendHeaders",
192 event: "onSendHeaders",
193 details: {
194 url: getURLHttpWithHeaders(),
197 { label: "onHeadersReceived",
198 event: "onHeadersReceived",
199 details: {
200 statusLine: "HTTP/1.1 200 OK",
201 url: getURLHttpWithHeaders(),
202 statusCode: 200
205 { label: "onErrorOccurred",
206 event: "onErrorOccurred",
207 details: {
208 url: getURLHttpWithHeaders(),
209 fromCache: false,
210 error: "net::ERR_BLOCKED_BY_CLIENT"
214 [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
215 "onHeadersReceived", "onErrorOccurred"] ]);
216 onRequest.addRules(
217 [ {'conditions': [
218 new RequestMatcher({ 'stages': ["onHeadersReceived"] })],
219 'actions': [new CancelRequest()]}
221 function() {navigateAndWait(getURLHttpWithHeaders());}
224 // Tests that "thirdPartyForCookies: true" matches third party requests.
225 function testThirdParty() {
226 ignoreUnexpected = false;
227 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
228 onRequest.addRules(
229 [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})],
230 'actions': [new chrome.declarativeWebRequest.CancelRequest()]},],
231 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
235 // Tests that "thirdPartyForCookies: false" matches first party requests,
236 // by cancelling all requests, and overriding the cancelling rule only for
237 // requests matching "thirdPartyForCookies: false".
238 function testFirstParty() {
239 ignoreUnexpected = false;
240 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
241 onRequest.addRules(
242 [ {'priority': 2,
243 'conditions': [
244 new RequestMatcher({thirdPartyForCookies: false})
246 'actions': [
247 new chrome.declarativeWebRequest.IgnoreRules({
248 lowerPriorityThan: 2 })
251 {'priority': 1,
252 'conditions': [new RequestMatcher({})],
253 'actions': [new chrome.declarativeWebRequest.CancelRequest()]
256 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
260 function testFirstPartyForCookiesUrl() {
261 // This is an end-to-end test for firstPartyForCookies. The choice of URL to
262 // navigate to is purely arbitrary.
263 ignoreUnexpected = false;
264 expect(
266 { label: "onBeforeRequest",
267 event: "onBeforeRequest",
268 details: {
269 url: getURLOfHTMLWithThirdParty(),
270 frameUrl: getURLOfHTMLWithThirdParty()
273 { label: "onErrorOccurred",
274 event: "onErrorOccurred",
275 details: {
276 url: getURLOfHTMLWithThirdParty(),
277 fromCache: false,
278 error: "net::ERR_BLOCKED_BY_CLIENT"
282 [ ["onBeforeRequest", "onErrorOccurred"] ]);
283 onRequest.addRules(
284 [ {'conditions': [
285 new RequestMatcher({
286 firstPartyForCookiesUrl: {
287 hostEquals: testServer
291 'actions': [new chrome.declarativeWebRequest.CancelRequest()]
294 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
298 function testRedirectRequest() {
299 ignoreUnexpected = true;
300 expect(
302 { label: "onBeforeRequest-a",
303 event: "onBeforeRequest",
304 details: {
305 type: "main_frame",
306 url: getURLHttpComplex(),
307 frameUrl: getURLHttpComplex()
310 { label: "onBeforeRedirect",
311 event: "onBeforeRedirect",
312 details: {
313 url: getURLHttpComplex(),
314 redirectUrl: getURLHttpSimple(),
315 fromCache: false,
316 statusLine: "HTTP/1.1 307 Internal Redirect",
317 statusCode: 307,
320 { label: "onBeforeRequest-b",
321 event: "onBeforeRequest",
322 details: {
323 type: "main_frame",
324 url: getURLHttpSimple(),
325 frameUrl: getURLHttpSimple(),
328 { label: "onCompleted",
329 event: "onCompleted",
330 details: {
331 ip: "127.0.0.1",
332 url: getURLHttpSimple(),
333 fromCache: false,
334 statusCode: 200,
335 statusLine: "HTTP/1.1 200 OK",
339 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
340 "onCompleted"] ]);
342 onRequest.addRules(
343 [ {'conditions': [new RequestMatcher({'url': {'pathSuffix': ".html"}})],
344 'actions': [
345 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
347 function() {navigateAndWait(getURLHttpComplex());}
351 function testRedirectRequest2() {
352 ignoreUnexpected = true;
353 expect(
355 { label: "onCompleted",
356 event: "onCompleted",
357 details: {
358 ip: "127.0.0.1",
359 url: getURLHttpRedirectTest(),
360 fromCache: false,
361 statusCode: 200,
362 statusLine: "HTTP/1.1 200 OK",
365 // We cannot wait for onCompleted signals because these are not sent
366 // for data:// URLs.
367 { label: "onBeforeRedirect-1",
368 event: "onBeforeRedirect",
369 details: {
370 url: getServerURL(
371 "extensions/api_test/webrequest/declarative/image.png"),
372 redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA" +
373 "AAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJ" +
374 "ggg==",
375 fromCache: false,
376 statusLine: "HTTP/1.1 307 Internal Redirect",
377 statusCode: 307,
378 type: "image",
381 { label: "onBeforeRedirect-2",
382 event: "onBeforeRedirect",
383 details: {
384 frameId: 1,
385 parentFrameId: 0,
386 url: getServerURL(
387 "extensions/api_test/webrequest/declarative/frame.html"),
388 redirectUrl: "data:text/html,",
389 fromCache: false,
390 statusLine: "HTTP/1.1 307 Internal Redirect",
391 statusCode: 307,
392 type: "sub_frame",
396 [ ["onCompleted"], ["onBeforeRedirect-1"], ["onBeforeRedirect-2"] ]);
398 onRequest.addRules(
399 [ {conditions: [
400 new RequestMatcher({url: {pathSuffix: "image.png"}})],
401 actions: [new RedirectToTransparentImage()]},
402 {conditions: [
403 new RequestMatcher({url: {pathSuffix: "frame.html"}})],
404 actions: [new RedirectToEmptyDocument()]},
406 function() {navigateAndWait(getURLHttpRedirectTest());}
410 // Tests that a request is redirected during the onHeadersReceived stage
411 // when the conditions include a RequestMatcher with a contentType.
412 function testRedirectRequestByContentType() {
413 ignoreUnexpected = true;
414 expect(
416 { label: "onBeforeRequest-a",
417 event: "onBeforeRequest",
418 details: {
419 type: "main_frame",
420 url: getURLHttpWithHeaders(),
421 frameUrl: getURLHttpWithHeaders()
424 { label: "onBeforeRedirect",
425 event: "onBeforeRedirect",
426 details: {
427 url: getURLHttpWithHeaders(),
428 redirectUrl: getURLHttpSimple(),
429 statusLine: "HTTP/1.1 302 Found",
430 statusCode: 302,
431 fromCache: false,
432 ip: "127.0.0.1",
435 { label: "onBeforeRequest-b",
436 event: "onBeforeRequest",
437 details: {
438 type: "main_frame",
439 url: getURLHttpSimple(),
440 frameUrl: getURLHttpSimple(),
443 { label: "onCompleted",
444 event: "onCompleted",
445 details: {
446 ip: "127.0.0.1",
447 url: getURLHttpSimple(),
448 fromCache: false,
449 statusCode: 200,
450 statusLine: "HTTP/1.1 200 OK",
454 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
455 "onCompleted"] ]);
457 onRequest.addRules(
458 [ {'conditions': [new RequestMatcher({'contentType': ["text/plain"]})],
459 'actions': [
460 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
462 function() {navigateAndWait(getURLHttpWithHeaders());}
466 function testRedirectByRegEx() {
467 ignoreUnexpected = true;
468 expect(
470 { label: "onCompleted",
471 event: "onCompleted",
472 details: {
473 ip: "127.0.0.1",
474 url: getURLHttpSimpleB(),
475 fromCache: false,
476 statusCode: 200,
477 statusLine: "HTTP/1.1 200 OK",
481 [ ["onCompleted"] ]);
483 onRequest.addRules(
484 [ {conditions: [new RequestMatcher({url: {pathSuffix: ".html"}})],
485 actions: [
486 new RedirectByRegEx({from: "^(.*)/a.html$", to: "$1/b.html"})]}
488 function() {navigateAndWait(getURLHttpSimple());}
492 function testRegexFilter() {
493 ignoreUnexpected = true;
494 expect(
496 { label: "onErrorOccurred",
497 event: "onErrorOccurred",
498 details: {
499 url: getURLHttpSimple(),
500 fromCache: false,
501 error: "net::ERR_BLOCKED_BY_CLIENT"
505 [ ["onErrorOccurred"] ]);
506 onRequest.addRules(
507 [ {'conditions': [
508 new RequestMatcher({
509 'url': {
510 'urlMatches': 'simple[A-Z].*a\.html$',
511 'schemes': ["http"]
513 })],
514 'actions': [new CancelRequest()]}
516 function() {navigateAndWait(getURLHttpSimple());}