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",
87 { label
: "onResponseStarted",
88 event
: "onResponseStarted",
90 url
: getURLOfHTMLWithThirdParty(),
94 statusLine
: "HTTP/1.1 200 OK"
97 { label
: "onCompleted",
102 url
: getURLOfHTMLWithThirdParty(),
104 statusLine
: "HTTP/1.1 200 OK"
107 { label
: "img-onBeforeRequest",
108 event
: "onBeforeRequest",
111 url
: "http://non_existing_third_party.com/image.png",
112 frameUrl
: getURLOfHTMLWithThirdParty()
115 { label
: "img-onErrorOccurred",
116 event
: "onErrorOccurred",
118 error
: "net::ERR_BLOCKED_BY_CLIENT",
121 url
: "http://non_existing_third_party.com/image.png"
127 function cancelThirdPartyExpectedOrder() {
129 ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
130 "onHeadersReceived", "onResponseStarted", "onCompleted"],
131 ["img-onBeforeRequest", "img-onErrorOccurred"]
137 function testCancelRequest() {
138 ignoreUnexpected
= true;
141 { label
: "onErrorOccurred",
142 event
: "onErrorOccurred",
144 url
: getURLHttpWithHeaders(),
146 error
: "net::ERR_BLOCKED_BY_CLIENT"
150 [ ["onErrorOccurred"] ]);
155 'pathSuffix': ".html",
156 'ports': [testServerPort
, [1000, 2000]],
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;
178 { label
: "onBeforeRequest",
179 event
: "onBeforeRequest",
181 url
: getURLHttpWithHeaders(),
182 frameUrl
: getURLHttpWithHeaders()
185 { label
: "onBeforeSendHeaders",
186 event
: "onBeforeSendHeaders",
188 url
: getURLHttpWithHeaders(),
191 { label
: "onSendHeaders",
192 event
: "onSendHeaders",
194 url
: getURLHttpWithHeaders(),
197 { label
: "onHeadersReceived",
198 event
: "onHeadersReceived",
200 statusLine
: "HTTP/1.1 200 OK",
201 url
: getURLHttpWithHeaders(),
205 { label
: "onErrorOccurred",
206 event
: "onErrorOccurred",
208 url
: getURLHttpWithHeaders(),
210 error
: "net::ERR_BLOCKED_BY_CLIENT"
214 [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
215 "onHeadersReceived", "onErrorOccurred"] ]);
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());
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());
244 new RequestMatcher({thirdPartyForCookies
: false})
247 new chrome
.declarativeWebRequest
.IgnoreRules({
248 lowerPriorityThan
: 2 })
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;
266 { label
: "onBeforeRequest",
267 event
: "onBeforeRequest",
269 url
: getURLOfHTMLWithThirdParty(),
270 frameUrl
: getURLOfHTMLWithThirdParty()
273 { label
: "onErrorOccurred",
274 event
: "onErrorOccurred",
276 url
: getURLOfHTMLWithThirdParty(),
278 error
: "net::ERR_BLOCKED_BY_CLIENT"
282 [ ["onBeforeRequest", "onErrorOccurred"] ]);
286 firstPartyForCookiesUrl
: {
287 hostEquals
: testServer
291 'actions': [new chrome
.declarativeWebRequest
.CancelRequest()]
294 function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
298 function testRedirectRequest() {
299 ignoreUnexpected
= true;
302 { label
: "onBeforeRequest-a",
303 event
: "onBeforeRequest",
306 url
: getURLHttpComplex(),
307 frameUrl
: getURLHttpComplex()
310 { label
: "onBeforeRedirect",
311 event
: "onBeforeRedirect",
313 url
: getURLHttpComplex(),
314 redirectUrl
: getURLHttpSimple(),
316 statusLine
: "HTTP/1.1 307 Internal Redirect",
320 { label
: "onBeforeRequest-b",
321 event
: "onBeforeRequest",
324 url
: getURLHttpSimple(),
325 frameUrl
: getURLHttpSimple(),
328 { label
: "onCompleted",
329 event
: "onCompleted",
332 url
: getURLHttpSimple(),
335 statusLine
: "HTTP/1.1 200 OK",
339 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
343 [ {'conditions': [new RequestMatcher({'url': {'pathSuffix': ".html"}})],
345 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
347 function() {navigateAndWait(getURLHttpComplex());}
351 function testRedirectRequest2() {
352 ignoreUnexpected
= true;
355 { label
: "onCompleted",
356 event
: "onCompleted",
359 url
: getURLHttpRedirectTest(),
362 statusLine
: "HTTP/1.1 200 OK",
365 // We cannot wait for onCompleted signals because these are not sent
367 { label
: "onBeforeRedirect-1",
368 event
: "onBeforeRedirect",
371 "extensions/api_test/webrequest/declarative/image.png"),
372 redirectUrl
: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA" +
373 "AAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJ" +
376 statusLine
: "HTTP/1.1 307 Internal Redirect",
381 { label
: "onBeforeRedirect-2",
382 event
: "onBeforeRedirect",
387 "extensions/api_test/webrequest/declarative/frame.html"),
388 redirectUrl
: "data:text/html,",
390 statusLine
: "HTTP/1.1 307 Internal Redirect",
396 [ ["onCompleted"], ["onBeforeRedirect-1"], ["onBeforeRedirect-2"] ]);
400 new RequestMatcher({url
: {pathSuffix
: "image.png"}})],
401 actions
: [new RedirectToTransparentImage()]},
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;
416 { label
: "onBeforeRequest-a",
417 event
: "onBeforeRequest",
420 url
: getURLHttpWithHeaders(),
421 frameUrl
: getURLHttpWithHeaders()
424 { label
: "onBeforeRedirect",
425 event
: "onBeforeRedirect",
427 url
: getURLHttpWithHeaders(),
428 redirectUrl
: getURLHttpSimple(),
429 statusLine
: "HTTP/1.1 302 Found",
435 { label
: "onBeforeRequest-b",
436 event
: "onBeforeRequest",
439 url
: getURLHttpSimple(),
440 frameUrl
: getURLHttpSimple(),
443 { label
: "onCompleted",
444 event
: "onCompleted",
447 url
: getURLHttpSimple(),
450 statusLine
: "HTTP/1.1 200 OK",
454 [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
458 [ {'conditions': [new RequestMatcher({'contentType': ["text/plain"]})],
460 new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
462 function() {navigateAndWait(getURLHttpWithHeaders());}
466 function testRedirectByRegEx() {
467 ignoreUnexpected
= true;
470 { label
: "onCompleted",
471 event
: "onCompleted",
474 url
: getURLHttpSimpleB(),
477 statusLine
: "HTTP/1.1 200 OK",
481 [ ["onCompleted"] ]);
484 [ {conditions
: [new RequestMatcher({url
: {pathSuffix
: ".html"}})],
486 new RedirectByRegEx({from: "^(.*)/a.html$", to
: "$1/b.html"})]}
488 function() {navigateAndWait(getURLHttpSimple());}
492 function testRegexFilter() {
493 ignoreUnexpected
= true;
496 { label
: "onErrorOccurred",
497 event
: "onErrorOccurred",
499 url
: getURLHttpSimple(),
501 error
: "net::ERR_BLOCKED_BY_CLIENT"
505 [ ["onErrorOccurred"] ]);
510 'urlMatches': 'simple[A-Z].*a\.html$',
514 'actions': [new CancelRequest()]}
516 function() {navigateAndWait(getURLHttpSimple());}