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() { |
| 60 // Returns the URL of a HTML document with a third-party resource. |
| 61 return getServerURL( |
| 62 "files/extensions/api_test/webrequest/declarative/third-party.html"); |
| 63 } |
| 64 |
59 function getURLSetCookie() { | 65 function getURLSetCookie() { |
60 return getServerURL('set-cookie?Foo=Bar'); | 66 return getServerURL('set-cookie?Foo=Bar'); |
61 } | 67 } |
62 | 68 |
63 function getURLSetCookie2() { | 69 function getURLSetCookie2() { |
64 return getServerURL('set-cookie?passedCookie=Foo&editedCookie=Foo&' + | 70 return getServerURL('set-cookie?passedCookie=Foo&editedCookie=Foo&' + |
65 'deletedCookie=Foo'); | 71 'deletedCookie=Foo'); |
66 } | 72 } |
67 | 73 |
68 function getURLEchoCookie() { | 74 function getURLEchoCookie() { |
69 return getServerURL('echoheader?Cookie'); | 75 return getServerURL('echoheader?Cookie'); |
70 } | 76 } |
71 | 77 |
72 function getURLHttpXHRData() { | 78 function getURLHttpXHRData() { |
73 return getServerURL("files/extensions/api_test/webrequest/xhr/data.json", | 79 return getServerURL("files/extensions/api_test/webrequest/xhr/data.json", |
74 "b.com"); | 80 "b.com"); |
75 } | 81 } |
76 | 82 |
77 function getURLHttpSimpleOnB() { | 83 function getURLHttpSimpleOnB() { |
78 return getServerURL("files/extensions/api_test/webrequest/simpleLoad/a.html", | 84 return getServerURL("files/extensions/api_test/webrequest/simpleLoad/a.html", |
79 "b.com"); | 85 "b.com"); |
80 } | 86 } |
81 | 87 |
| 88 // Shared test sections. |
| 89 function cancelThirdPartyExpected() { |
| 90 return [ |
| 91 { label: "onBeforeRequest", |
| 92 event: "onBeforeRequest", |
| 93 details: { |
| 94 url: getURLThirdParty(), |
| 95 frameUrl: getURLThirdParty() |
| 96 } |
| 97 }, |
| 98 { label: "onBeforeSendHeaders", |
| 99 event: "onBeforeSendHeaders", |
| 100 details: {url: getURLThirdParty()} |
| 101 }, |
| 102 { label: "onSendHeaders", |
| 103 event: "onSendHeaders", |
| 104 details: {url: getURLThirdParty()} |
| 105 }, |
| 106 { label: "onHeadersReceived", |
| 107 event: "onHeadersReceived", |
| 108 details: { |
| 109 url: getURLThirdParty(), |
| 110 statusLine: "HTTP/1.0 200 OK" |
| 111 } |
| 112 }, |
| 113 { label: "onResponseStarted", |
| 114 event: "onResponseStarted", |
| 115 details: { |
| 116 url: getURLThirdParty(), |
| 117 fromCache: false, |
| 118 ip: "127.0.0.1", |
| 119 statusCode: 200, |
| 120 statusLine: "HTTP/1.0 200 OK" |
| 121 } |
| 122 }, |
| 123 { label: "onCompleted", |
| 124 event: "onCompleted", |
| 125 details: { |
| 126 fromCache: false, |
| 127 ip: "127.0.0.1", |
| 128 url: getURLThirdParty(), |
| 129 statusCode: 200, |
| 130 statusLine: "HTTP/1.0 200 OK" |
| 131 } |
| 132 }, |
| 133 { label: "img-onBeforeRequest", |
| 134 event: "onBeforeRequest", |
| 135 details: { |
| 136 type: "image", |
| 137 url: "http://non_existing_third_party.com/image.png", |
| 138 frameUrl: getURLThirdParty() |
| 139 } |
| 140 }, |
| 141 { label: "img-onErrorOccurred", |
| 142 event: "onErrorOccurred", |
| 143 details: { |
| 144 error: "net::ERR_BLOCKED_BY_CLIENT", |
| 145 fromCache: false, |
| 146 type: "image", |
| 147 url: "http://non_existing_third_party.com/image.png" |
| 148 } |
| 149 }, |
| 150 ]; |
| 151 } |
| 152 |
| 153 function cancelThirdPartyExpectedOrder() { |
| 154 return [ |
| 155 ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders", |
| 156 "onHeadersReceived", "onResponseStarted", "onCompleted"], |
| 157 ["img-onBeforeRequest", "img-onErrorOccurred"] |
| 158 ]; |
| 159 } |
| 160 |
82 runTests([ | 161 runTests([ |
83 | 162 |
84 function testCancelRequest() { | 163 function testCancelRequest() { |
85 ignoreUnexpected = true; | 164 ignoreUnexpected = true; |
86 expect( | 165 expect( |
87 [ | 166 [ |
88 { label: "onErrorOccurred", | 167 { label: "onErrorOccurred", |
89 event: "onErrorOccurred", | 168 event: "onErrorOccurred", |
90 details: { | 169 details: { |
91 url: getURLHttpWithHeaders(), | 170 url: getURLHttpWithHeaders(), |
(...skipping 15 matching lines...) Expand all Loading... |
107 'contentType': ["text/plain"], | 186 'contentType': ["text/plain"], |
108 'excludeContentType': ["image/png"], | 187 'excludeContentType': ["image/png"], |
109 'responseHeaders': [{ nameContains: ["content", "type"] } ], | 188 'responseHeaders': [{ nameContains: ["content", "type"] } ], |
110 'excludeResponseHeaders': [{ valueContains: "nonsense" }] })], | 189 'excludeResponseHeaders': [{ valueContains: "nonsense" }] })], |
111 'actions': [new CancelRequest()]} | 190 'actions': [new CancelRequest()]} |
112 ], | 191 ], |
113 function() {navigateAndWait(getURLHttpWithHeaders());} | 192 function() {navigateAndWait(getURLHttpWithHeaders());} |
114 ); | 193 ); |
115 }, | 194 }, |
116 | 195 |
| 196 // Tests that "thirdPartyForCookies: true" matches third party requests. |
| 197 function testThirdParty() { |
| 198 ignoreUnexpected = false; |
| 199 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); |
| 200 onRequest.addRules( |
| 201 [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})], |
| 202 'actions': [new chrome.declarativeWebRequest.CancelRequest()]},], |
| 203 function() {navigateAndWait(getURLThirdParty());} |
| 204 ); |
| 205 }, |
| 206 |
| 207 // Tests that "thirdPartyForCookies: false" matches first party requests, |
| 208 // by cancelling all requests, and overriding the cancelling rule only for |
| 209 // requests matching "thirdPartyForCookies: false". |
| 210 function testFirstParty() { |
| 211 ignoreUnexpected = false; |
| 212 expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder()); |
| 213 onRequest.addRules( |
| 214 [ {'priority': 2, |
| 215 'conditions': [ |
| 216 new RequestMatcher({thirdPartyForCookies: false}) |
| 217 ], |
| 218 'actions': [ |
| 219 new chrome.declarativeWebRequest.IgnoreRules({ |
| 220 lowerPriorityThan: 2 }) |
| 221 ] |
| 222 }, |
| 223 {'priority': 1, |
| 224 'conditions': [new RequestMatcher({})], |
| 225 'actions': [new chrome.declarativeWebRequest.CancelRequest()] |
| 226 }, |
| 227 ], |
| 228 function() {navigateAndWait(getURLThirdParty());} |
| 229 ); |
| 230 }, |
| 231 |
117 function testRedirectRequest() { | 232 function testRedirectRequest() { |
118 ignoreUnexpected = true; | 233 ignoreUnexpected = true; |
119 expect( | 234 expect( |
120 [ | 235 [ |
121 { label: "onBeforeRequest-a", | 236 { label: "onBeforeRequest-a", |
122 event: "onBeforeRequest", | 237 event: "onBeforeRequest", |
123 details: { | 238 details: { |
124 type: "main_frame", | 239 type: "main_frame", |
125 url: getURLHttpComplex(), | 240 url: getURLHttpComplex(), |
126 frameUrl: getURLHttpComplex() | 241 frameUrl: getURLHttpComplex() |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 this.status + ", responseText: " + this.responseText); | 613 this.status + ", responseText: " + this.responseText); |
499 } | 614 } |
500 } | 615 } |
501 req.open("GET", getURLHttpXHRData(), asynchronous); | 616 req.open("GET", getURLHttpXHRData(), asynchronous); |
502 req.send(null); | 617 req.send(null); |
503 }); | 618 }); |
504 } | 619 } |
505 ); | 620 ); |
506 }, | 621 }, |
507 ]); | 622 ]); |
OLD | NEW |