OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var onRequest = chrome.declarativeWebRequest.onRequest; | 5 var onRequest = chrome.declarativeWebRequest.onRequest; |
6 var AddResponseHeader = | 6 var AddResponseHeader = |
7 chrome.declarativeWebRequest.AddResponseHeader; | 7 chrome.declarativeWebRequest.AddResponseHeader; |
8 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher; | 8 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher; |
9 var CancelRequest = chrome.declarativeWebRequest.CancelRequest; | 9 var CancelRequest = chrome.declarativeWebRequest.CancelRequest; |
10 var RedirectByRegEx = chrome.declarativeWebRequest.RedirectByRegEx; | 10 var RedirectByRegEx = chrome.declarativeWebRequest.RedirectByRegEx; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 function getURLHttpRedirectTest() { | 49 function getURLHttpRedirectTest() { |
50 return getServerURL( | 50 return getServerURL( |
51 "files/extensions/api_test/webrequest/declarative/a.html"); | 51 "files/extensions/api_test/webrequest/declarative/a.html"); |
52 } | 52 } |
53 | 53 |
54 function getURLHttpWithHeaders() { | 54 function getURLHttpWithHeaders() { |
55 return getServerURL( | 55 return getServerURL( |
56 "files/extensions/api_test/webrequest/declarative/headers.html"); | 56 "files/extensions/api_test/webrequest/declarative/headers.html"); |
57 } | 57 } |
58 | 58 |
59 function getURLThirdParty() { | 59 function getURLOfHTMLWithThirdParty() { |
60 // Returns the URL of a HTML document with a third-party resource. | 60 // Returns the URL of a HTML document with a third-party resource. |
61 return getServerURL( | 61 return getServerURL( |
62 "files/extensions/api_test/webrequest/declarative/third-party.html"); | 62 "files/extensions/api_test/webrequest/declarative/third-party.html"); |
63 } | 63 } |
64 | 64 |
65 function getURLSetCookie() { | 65 function getURLSetCookie() { |
66 return getServerURL('set-cookie?Foo=Bar'); | 66 return getServerURL('set-cookie?Foo=Bar'); |
67 } | 67 } |
68 | 68 |
69 function getURLSetCookie2() { | 69 function getURLSetCookie2() { |
70 return getServerURL('set-cookie?passedCookie=Foo&editedCookie=Foo&' + | 70 return getServerURL('set-cookie?passedCookie=Foo&editedCookie=Foo&' + |
71 'deletedCookie=Foo'); | 71 'deletedCookie=Foo'); |
72 } | 72 } |
73 | 73 |
74 function getURLEchoCookie() { | 74 function getURLEchoCookie() { |
75 return getServerURL('echoheader?Cookie'); | 75 return getServerURL('echoheader?Cookie'); |
76 } | 76 } |
77 | 77 |
78 function getURLHttpXHRData() { | 78 function getURLHttpXHRData() { |
79 return getServerURL("files/extensions/api_test/webrequest/xhr/data.json", | 79 return getServerURL("files/extensions/api_test/webrequest/xhr/data.json", |
80 "b.com"); | 80 "b.com"); |
81 } | 81 } |
82 | 82 |
83 // Shared test sections. | 83 // Shared test sections. |
84 function cancelThirdPartyExpected() { | 84 function cancelThirdPartyExpected() { |
85 return [ | 85 return [ |
86 { label: "onBeforeRequest", | 86 { label: "onBeforeRequest", |
87 event: "onBeforeRequest", | 87 event: "onBeforeRequest", |
88 details: { | 88 details: { |
89 url: getURLThirdParty(), | 89 url: getURLOfHTMLWithThirdParty(), |
90 frameUrl: getURLThirdParty() | 90 frameUrl: getURLOfHTMLWithThirdParty() |
91 } | 91 } |
92 }, | 92 }, |
93 { label: "onBeforeSendHeaders", | 93 { label: "onBeforeSendHeaders", |
94 event: "onBeforeSendHeaders", | 94 event: "onBeforeSendHeaders", |
95 details: {url: getURLThirdParty()} | 95 details: {url: getURLOfHTMLWithThirdParty()} |
96 }, | 96 }, |
97 { label: "onSendHeaders", | 97 { label: "onSendHeaders", |
98 event: "onSendHeaders", | 98 event: "onSendHeaders", |
99 details: {url: getURLThirdParty()} | 99 details: {url: getURLOfHTMLWithThirdParty()} |
100 }, | 100 }, |
101 { label: "onHeadersReceived", | 101 { label: "onHeadersReceived", |
102 event: "onHeadersReceived", | 102 event: "onHeadersReceived", |
103 details: { | 103 details: { |
104 url: getURLThirdParty(), | 104 url: getURLOfHTMLWithThirdParty(), |
105 statusLine: "HTTP/1.0 200 OK" | 105 statusLine: "HTTP/1.0 200 OK" |
106 } | 106 } |
107 }, | 107 }, |
108 { label: "onResponseStarted", | 108 { label: "onResponseStarted", |
109 event: "onResponseStarted", | 109 event: "onResponseStarted", |
110 details: { | 110 details: { |
111 url: getURLThirdParty(), | 111 url: getURLOfHTMLWithThirdParty(), |
112 fromCache: false, | 112 fromCache: false, |
113 ip: "127.0.0.1", | 113 ip: "127.0.0.1", |
114 statusCode: 200, | 114 statusCode: 200, |
115 statusLine: "HTTP/1.0 200 OK" | 115 statusLine: "HTTP/1.0 200 OK" |
116 } | 116 } |
117 }, | 117 }, |
118 { label: "onCompleted", | 118 { label: "onCompleted", |
119 event: "onCompleted", | 119 event: "onCompleted", |
120 details: { | 120 details: { |
121 fromCache: false, | 121 fromCache: false, |
122 ip: "127.0.0.1", | 122 ip: "127.0.0.1", |
123 url: getURLThirdParty(), | 123 url: getURLOfHTMLWithThirdParty(), |
124 statusCode: 200, | 124 statusCode: 200, |
125 statusLine: "HTTP/1.0 200 OK" | 125 statusLine: "HTTP/1.0 200 OK" |
126 } | 126 } |
127 }, | 127 }, |
128 { label: "img-onBeforeRequest", | 128 { label: "img-onBeforeRequest", |
129 event: "onBeforeRequest", | 129 event: "onBeforeRequest", |
130 details: { | 130 details: { |
131 type: "image", | 131 type: "image", |
132 url: "http://non_existing_third_party.com/image.png", | 132 url: "http://non_existing_third_party.com/image.png", |
133 frameUrl: getURLThirdParty() | 133 frameUrl: getURLOfHTMLWithThirdParty() |
134 } | 134 } |
135 }, | 135 }, |
136 { label: "img-onErrorOccurred", | 136 { label: "img-onErrorOccurred", |
137 event: "onErrorOccurred", | 137 event: "onErrorOccurred", |
138 details: { | 138 details: { |
139 error: "net::ERR_BLOCKED_BY_CLIENT", | 139 error: "net::ERR_BLOCKED_BY_CLIENT", |
140 fromCache: false, | 140 fromCache: false, |
141 type: "image", | 141 type: "image", |
142 url: "http://non_existing_third_party.com/image.png" | 142 url: "http://non_existing_third_party.com/image.png" |
143 } | 143 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 function() {navigateAndWait(getURLHttpWithHeaders());} | 241 function() {navigateAndWait(getURLHttpWithHeaders());} |
242 ); | 242 ); |
243 }, | 243 }, |
244 // Tests that "thirdPartyForCookies: true" matches third party requests. | 244 // Tests that "thirdPartyForCookies: true" matches third party requests. |
245 function testThirdParty() { | 245 function testThirdParty() { |
246 ignoreUnexpected = false; | 246 ignoreUnexpected = false; |
247 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); | 247 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); |
248 onRequest.addRules( | 248 onRequest.addRules( |
249 [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})], | 249 [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})], |
250 'actions': [new chrome.declarativeWebRequest.CancelRequest()]},], | 250 'actions': [new chrome.declarativeWebRequest.CancelRequest()]},], |
251 function() {navigateAndWait(getURLThirdParty());} | 251 function() {navigateAndWait(getURLOfHTMLWithThirdParty());} |
252 ); | 252 ); |
253 }, | 253 }, |
254 | 254 |
255 // Tests that "thirdPartyForCookies: false" matches first party requests, | 255 // Tests that "thirdPartyForCookies: false" matches first party requests, |
256 // by cancelling all requests, and overriding the cancelling rule only for | 256 // by cancelling all requests, and overriding the cancelling rule only for |
257 // requests matching "thirdPartyForCookies: false". | 257 // requests matching "thirdPartyForCookies: false". |
258 function testFirstParty() { | 258 function testFirstParty() { |
259 ignoreUnexpected = false; | 259 ignoreUnexpected = false; |
260 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); | 260 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); |
261 onRequest.addRules( | 261 onRequest.addRules( |
262 [ {'priority': 2, | 262 [ {'priority': 2, |
263 'conditions': [ | 263 'conditions': [ |
264 new RequestMatcher({thirdPartyForCookies: false}) | 264 new RequestMatcher({thirdPartyForCookies: false}) |
265 ], | 265 ], |
266 'actions': [ | 266 'actions': [ |
267 new chrome.declarativeWebRequest.IgnoreRules({ | 267 new chrome.declarativeWebRequest.IgnoreRules({ |
268 lowerPriorityThan: 2 }) | 268 lowerPriorityThan: 2 }) |
269 ] | 269 ] |
270 }, | 270 }, |
271 {'priority': 1, | 271 {'priority': 1, |
272 'conditions': [new RequestMatcher({})], | 272 'conditions': [new RequestMatcher({})], |
273 'actions': [new chrome.declarativeWebRequest.CancelRequest()] | 273 'actions': [new chrome.declarativeWebRequest.CancelRequest()] |
274 }, | 274 }, |
275 ], | 275 ], |
276 function() {navigateAndWait(getURLThirdParty());} | 276 function() {navigateAndWait(getURLOfHTMLWithThirdParty());} |
277 ); | 277 ); |
278 }, | 278 }, |
279 | 279 |
| 280 // Tests "firstPartyForCookiesUrl" by using it to override a cancelling rule. |
| 281 function testFirstPartyForCookiesUrl() { |
| 282 // This test sets up three rules: |
| 283 // Rule 1 is a priority 1 cancel-all rule to fire onBeforeRequest. |
| 284 // Rule 2 is a priority 2 rule to ignore rule 1 for requests satisfying the |
| 285 // first-party filter. This rule is the core of this test. |
| 286 // Rule 3 is a priority 3 canceling rule for non-existing URLs. It will |
| 287 // fire later, in stage onBeforeSendHeaders. This is necessary to avoid |
| 288 // platform-dependent HTTP 404 responses. |
| 289 ignoreUnexpected = false; |
| 290 expect( |
| 291 [ |
| 292 // This block of events is a successfull fetch of the main frame doc. |
| 293 { label: "onBeforeRequest", |
| 294 event: "onBeforeRequest", |
| 295 details: { |
| 296 url: getURLOfHTMLWithThirdParty(), |
| 297 frameUrl: getURLOfHTMLWithThirdParty() |
| 298 } |
| 299 }, |
| 300 // Once we are here, rule 1 has been ignored. |
| 301 { label: "onBeforeSendHeaders", |
| 302 event: "onBeforeSendHeaders", |
| 303 details: {url: getURLOfHTMLWithThirdParty()} |
| 304 }, |
| 305 // Rule 3 does not apply, because the requested resource exists. |
| 306 { label: "onSendHeaders", |
| 307 event: "onSendHeaders", |
| 308 details: {url: getURLOfHTMLWithThirdParty()} |
| 309 }, |
| 310 { label: "onHeadersReceived", |
| 311 event: "onHeadersReceived", |
| 312 details: { |
| 313 url: getURLOfHTMLWithThirdParty(), |
| 314 statusLine: "HTTP/1.0 200 OK" |
| 315 } |
| 316 }, |
| 317 { label: "onResponseStarted", |
| 318 event: "onResponseStarted", |
| 319 details: { |
| 320 url: getURLOfHTMLWithThirdParty(), |
| 321 fromCache: false, |
| 322 ip: "127.0.0.1", |
| 323 statusCode: 200, |
| 324 statusLine: "HTTP/1.0 200 OK" |
| 325 } |
| 326 }, |
| 327 { label: "onCompleted", |
| 328 event: "onCompleted", |
| 329 details: { |
| 330 fromCache: false, |
| 331 ip: "127.0.0.1", |
| 332 url: getURLOfHTMLWithThirdParty(), |
| 333 statusCode: 200, |
| 334 statusLine: "HTTP/1.0 200 OK" |
| 335 } |
| 336 }, |
| 337 |
| 338 // This block of events is a fetch of a non-existing sub-resource. |
| 339 { label: "img-onBeforeRequest", |
| 340 event: "onBeforeRequest", |
| 341 details: { |
| 342 type: "image", |
| 343 url: "http://non_existing_third_party.com/image.png", |
| 344 frameUrl: getURLOfHTMLWithThirdParty() |
| 345 } |
| 346 }, |
| 347 // Once we are here, rule 1 has been ignored. |
| 348 { label: "img-onBeforeSendHeaders", |
| 349 event: "onBeforeSendHeaders", |
| 350 details: { |
| 351 type: "image", |
| 352 url: "http://non_existing_third_party.com/image.png", |
| 353 } |
| 354 }, |
| 355 // Rule 3 has fired, and saved us the trouble of getting a 404. |
| 356 { label: "img-onErrorOccurred", |
| 357 event: "onErrorOccurred", |
| 358 details: { |
| 359 url: "http://non_existing_third_party.com/image.png", |
| 360 type: "image", |
| 361 fromCache: false, |
| 362 error: "net::ERR_BLOCKED_BY_CLIENT" |
| 363 } |
| 364 }, |
| 365 ], |
| 366 [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", |
| 367 "onHeadersReceived", "onResponseStarted", "onCompleted"], |
| 368 ["img-onBeforeRequest", "img-onBeforeSendHeaders", |
| 369 "img-onErrorOccurred"] ]); |
| 370 onRequest.addRules( |
| 371 [ // This rule cancels everyhting, and sooner than the very last rule |
| 372 // would. The test is reduced to making sure this (1st) rule was ignored |
| 373 // as a consequence of the 2nd rule. |
| 374 {'priority': 1, |
| 375 'conditions': [new RequestMatcher({ |
| 376 'stages': ["onBeforeRequest"] |
| 377 })], |
| 378 'actions': [new chrome.declarativeWebRequest.CancelRequest()] |
| 379 }, |
| 380 {'priority': 2, |
| 381 'conditions': [ |
| 382 new RequestMatcher({ |
| 383 firstPartyForCookiesUrl: { |
| 384 hostEquals: testServer |
| 385 } |
| 386 }) |
| 387 ], |
| 388 'actions': [ |
| 389 new chrome.declarativeWebRequest.IgnoreRules({ |
| 390 lowerPriorityThan: 2 }) |
| 391 ] |
| 392 }, |
| 393 // This rule cancels the request with a 3rd party URL just before it is |
| 394 // sent. This is necessary, because sending it would generate different |
| 395 // HTTP 4xx codes on different platforms, making it impossible to |
| 396 // present a single list of expected events. |
| 397 {'priority': 3, |
| 398 'conditions': [new RequestMatcher({ |
| 399 'url': { 'hostEquals': 'non_existing_third_party.com' }, |
| 400 'stages': ["onBeforeSendHeaders"] |
| 401 })], |
| 402 'actions': [new chrome.declarativeWebRequest.CancelRequest()] |
| 403 }, |
| 404 ], |
| 405 function() {navigateAndWait(getURLOfHTMLWithThirdParty());} |
| 406 ); |
| 407 }, |
| 408 |
280 function testRedirectRequest() { | 409 function testRedirectRequest() { |
281 ignoreUnexpected = true; | 410 ignoreUnexpected = true; |
282 expect( | 411 expect( |
283 [ | 412 [ |
284 { label: "onBeforeRequest-a", | 413 { label: "onBeforeRequest-a", |
285 event: "onBeforeRequest", | 414 event: "onBeforeRequest", |
286 details: { | 415 details: { |
287 type: "main_frame", | 416 type: "main_frame", |
288 url: getURLHttpComplex(), | 417 url: getURLHttpComplex(), |
289 frameUrl: getURLHttpComplex() | 418 frameUrl: getURLHttpComplex() |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 }, | 823 }, |
695 'requestHeaders': [{ nameContains: "" }], | 824 'requestHeaders': [{ nameContains: "" }], |
696 'excludeRequestHeaders': [{ valueContains: ["", "value123"] }] | 825 'excludeRequestHeaders': [{ valueContains: ["", "value123"] }] |
697 })], | 826 })], |
698 'actions': [new CancelRequest()]} | 827 'actions': [new CancelRequest()]} |
699 ], | 828 ], |
700 function() {navigateAndWait(getURLHttpSimple());} | 829 function() {navigateAndWait(getURLHttpSimple());} |
701 ); | 830 ); |
702 }, | 831 }, |
703 ]); | 832 ]); |
OLD | NEW |