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