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 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() {
42 "extensions/api_test/webrequest/complexLoad/a.html");
45 function getURLHttpRedirectTest() {
47 "extensions/api_test/webrequest/declarative/a.html");
50 function getURLHttpWithHeaders() {
52 "extensions/api_test/webrequest/declarative/headers.html");
55 function getURLOfHTMLWithThirdParty() {
56 // Returns the URL of a HTML document with a third-party resource.
58 "extensions/api_test/webrequest/declarative/third-party.html");
61 // Shared test sections.
62 function cancelThirdPartyExpected() {
64 { label: "onBeforeRequest",
65 event: "onBeforeRequest",
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",
82 url: getURLOfHTMLWithThirdParty(),
83 statusLine: "HTTP/1.1 200 OK"
86 { label: "onResponseStarted",
87 event: "onResponseStarted",
89 url: getURLOfHTMLWithThirdParty(),
93 statusLine: "HTTP/1.1 200 OK"
96 { label: "onCompleted",
101 url: getURLOfHTMLWithThirdParty(),
103 statusLine: "HTTP/1.1 200 OK"
106 { label: "img-onBeforeRequest",
107 event: "onBeforeRequest",
110 url: "http://non_existing_third_party.com/image.png",
111 frameUrl: getURLOfHTMLWithThirdParty()
114 { label: "img-onErrorOccurred",
115 event: "onErrorOccurred",
117 error: "net::ERR_BLOCKED_BY_CLIENT",
120 url: "http://non_existing_third_party.com/image.png"
126 function cancelThirdPartyExpectedOrder() {
128 ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
129 "onHeadersReceived", "onResponseStarted", "onCompleted"],
130 ["img-onBeforeRequest", "img-onErrorOccurred"]
136 function testCancelRequest() {
137 ignoreUnexpected = true;
140 { label: "onErrorOccurred",
141 event: "onErrorOccurred",
143 url: getURLHttpWithHeaders(),
145 error: "net::ERR_BLOCKED_BY_CLIENT"
149 [ ["onErrorOccurred"] ]);
154 'pathSuffix': ".html",
155 'ports': [testServerPort, [1000, 2000]],
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()]}
166 function() {navigateAndWait(getURLHttpWithHeaders());}
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;
177 { label: "onBeforeRequest",
178 event: "onBeforeRequest",
180 url: getURLHttpWithHeaders(),
181 frameUrl: getURLHttpWithHeaders()
184 { label: "onBeforeSendHeaders",
185 event: "onBeforeSendHeaders",
187 url: getURLHttpWithHeaders(),
190 { label: "onSendHeaders",
191 event: "onSendHeaders",
193 url: getURLHttpWithHeaders(),
196 { label: "onHeadersReceived",
197 event: "onHeadersReceived",
199 statusLine: "HTTP/1.1 200 OK",
200 url: getURLHttpWithHeaders(),
203 { label: "onErrorOccurred",
204 event: "onErrorOccurred",
206 url: getURLHttpWithHeaders(),
208 error: "net::ERR_BLOCKED_BY_CLIENT"
212 [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
213 "onHeadersReceived", "onErrorOccurred"] ]);
216 new RequestMatcher({ 'stages': ["onHeadersReceived"] })],
217 'actions': [new CancelRequest()]}
219 function() {navigateAndWait(getURLHttpWithHeaders());}
222 // Tests that "thirdPartyForCookies: true" matches third party requests.
223 function testThirdParty() {
224 ignoreUnexpected = false;
225 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
227 [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})],
228 'actions': [new chrome.declarativeWebRequest.CancelRequest()]},],
229 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
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());
242 new RequestMatcher({thirdPartyForCookies: false})
245 new chrome.declarativeWebRequest.IgnoreRules({
246 lowerPriorityThan: 2 })
250 'conditions': [new RequestMatcher({})],
251 'actions': [new chrome.declarativeWebRequest.CancelRequest()]
254 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
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;
264 { label: "onBeforeRequest",
265 event: "onBeforeRequest",
267 url: getURLOfHTMLWithThirdParty(),
268 frameUrl: getURLOfHTMLWithThirdParty()
271 { label: "onErrorOccurred",
272 event: "onErrorOccurred",
274 url: getURLOfHTMLWithThirdParty(),
276 error: "net::ERR_BLOCKED_BY_CLIENT"
280 [ ["onBeforeRequest", "onErrorOccurred"] ]);
284 firstPartyForCookiesUrl: {
285 hostEquals: testServer
289 'actions': [new chrome.declarativeWebRequest.CancelRequest()]
292 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
296 function testRedirectRequest() {
297 ignoreUnexpected = true;
300 { label: "onBeforeRequest-a",
301 event: "onBeforeRequest",
304 url: getURLHttpComplex(),
305 frameUrl: getURLHttpComplex()
308 { label: "onBeforeRedirect",
309 event: "onBeforeRedirect",
311 url: getURLHttpComplex(),
312 redirectUrl: getURLHttpSimple(),
314 statusLine: "HTTP/1.1 307 Internal Redirect",
318 { label: "onBeforeRequest-b",
319 event: "onBeforeRequest",
322 url: getURLHttpSimple(),
323 frameUrl: getURLHttpSimple(),
326 { label: "onCompleted",
327 event: "onCompleted",
330 url: getURLHttpSimple(),
333 statusLine: "HTTP/1.1 200 OK",
337 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
341 [ {'conditions': [new RequestMatcher({'url': {'pathSuffix': ".html"}})],
343 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
345 function() {navigateAndWait(getURLHttpComplex());}
349 function testRedirectRequest2() {
350 ignoreUnexpected = true;
353 { label: "onCompleted",
354 event: "onCompleted",
357 url: getURLHttpRedirectTest(),
360 statusLine: "HTTP/1.1 200 OK",
363 // We cannot wait for onCompleted signals because these are not sent
365 { label: "onBeforeRedirect-1",
366 event: "onBeforeRedirect",
369 "extensions/api_test/webrequest/declarative/image.png"),
370 redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA" +
371 "AAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJ" +
374 statusLine: "HTTP/1.1 307 Internal Redirect",
379 { label: "onBeforeRedirect-2",
380 event: "onBeforeRedirect",
385 "extensions/api_test/webrequest/declarative/frame.html"),
386 redirectUrl: "data:text/html,",
388 statusLine: "HTTP/1.1 307 Internal Redirect",
394 [ ["onCompleted"], ["onBeforeRedirect-1"], ["onBeforeRedirect-2"] ]);
398 new RequestMatcher({url: {pathSuffix: "image.png"}})],
399 actions: [new RedirectToTransparentImage()]},
401 new RequestMatcher({url: {pathSuffix: "frame.html"}})],
402 actions: [new RedirectToEmptyDocument()]},
404 function() {navigateAndWait(getURLHttpRedirectTest());}
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;
414 { label: "onBeforeRequest-a",
415 event: "onBeforeRequest",
418 url: getURLHttpWithHeaders(),
419 frameUrl: getURLHttpWithHeaders()
422 { label: "onBeforeRedirect",
423 event: "onBeforeRedirect",
425 url: getURLHttpWithHeaders(),
426 redirectUrl: getURLHttpSimple(),
427 statusLine: "HTTP/1.1 302 Found",
433 { label: "onBeforeRequest-b",
434 event: "onBeforeRequest",
437 url: getURLHttpSimple(),
438 frameUrl: getURLHttpSimple(),
441 { label: "onCompleted",
442 event: "onCompleted",
445 url: getURLHttpSimple(),
448 statusLine: "HTTP/1.1 200 OK",
452 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
456 [ {'conditions': [new RequestMatcher({'contentType': ["text/plain"]})],
458 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
460 function() {navigateAndWait(getURLHttpWithHeaders());}
464 function testRedirectByRegEx() {
465 ignoreUnexpected = true;
468 { label: "onCompleted",
469 event: "onCompleted",
472 url: getURLHttpSimpleB(),
475 statusLine: "HTTP/1.1 200 OK",
479 [ ["onCompleted"] ]);
482 [ {conditions: [new RequestMatcher({url: {pathSuffix: ".html"}})],
484 new RedirectByRegEx({from: "^(.*)/a.html$", to: "$1/b.html"})]}
486 function() {navigateAndWait(getURLHttpSimple());}
490 function testRegexFilter() {
491 ignoreUnexpected = true;
494 { label: "onErrorOccurred",
495 event: "onErrorOccurred",
497 url: getURLHttpSimple(),
499 error: "net::ERR_BLOCKED_BY_CLIENT"
503 [ ["onErrorOccurred"] ]);
508 'urlMatches': 'simple[A-Z].*a\.html$',
512 'actions': [new CancelRequest()]}
514 function() {navigateAndWait(getURLHttpSimple());}