| 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 = |
| 7 chrome.declarativeWebRequest.AddResponseHeader; |
| 6 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher; | 8 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher; |
| 7 var CancelRequest = chrome.declarativeWebRequest.CancelRequest; | 9 var CancelRequest = chrome.declarativeWebRequest.CancelRequest; |
| 8 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest; | 10 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest; |
| 9 var RedirectToTransparentImage = | 11 var RedirectToTransparentImage = |
| 10 chrome.declarativeWebRequest.RedirectToTransparentImage; | 12 chrome.declarativeWebRequest.RedirectToTransparentImage; |
| 11 var RedirectToEmptyDocument = | 13 var RedirectToEmptyDocument = |
| 12 chrome.declarativeWebRequest.RedirectToEmptyDocument; | 14 chrome.declarativeWebRequest.RedirectToEmptyDocument; |
| 13 var SetRequestHeader = | 15 var SetRequestHeader = |
| 14 chrome.declarativeWebRequest.SetRequestHeader; | 16 chrome.declarativeWebRequest.SetRequestHeader; |
| 15 var RemoveRequestHeader = | 17 var RemoveRequestHeader = |
| 16 chrome.declarativeWebRequest.RemoveRequestHeader; | 18 chrome.declarativeWebRequest.RemoveRequestHeader; |
| 19 var RemoveResponseHeader = |
| 20 chrome.declarativeWebRequest.RemoveResponseHeader; |
| 17 var IgnoreRules = | 21 var IgnoreRules = |
| 18 chrome.declarativeWebRequest.IgnoreRules; | 22 chrome.declarativeWebRequest.IgnoreRules; |
| 19 | 23 |
| 20 // Constants as functions, not to be called until after runTests. | 24 // Constants as functions, not to be called until after runTests. |
| 21 function getURLEchoUserAgent() { | 25 function getURLEchoUserAgent() { |
| 22 return getServerURL('echoheader?User-Agent'); | 26 return getServerURL('echoheader?User-Agent'); |
| 23 } | 27 } |
| 24 | 28 |
| 25 function getURLHttpSimple() { | 29 function getURLHttpSimple() { |
| 26 return getServerURL("files/extensions/api_test/webrequest/simpleLoad/a.html"); | 30 return getServerURL("files/extensions/api_test/webrequest/simpleLoad/a.html"); |
| 27 } | 31 } |
| 28 | 32 |
| 29 function getURLHttpComplex() { | 33 function getURLHttpComplex() { |
| 30 return getServerURL( | 34 return getServerURL( |
| 31 "files/extensions/api_test/webrequest/complexLoad/a.html"); | 35 "files/extensions/api_test/webrequest/complexLoad/a.html"); |
| 32 } | 36 } |
| 33 | 37 |
| 34 function getURLHttpRedirectTest() { | 38 function getURLHttpRedirectTest() { |
| 35 return getServerURL( | 39 return getServerURL( |
| 36 "files/extensions/api_test/webrequest/declarative/a.html"); | 40 "files/extensions/api_test/webrequest/declarative/a.html"); |
| 37 } | 41 } |
| 38 | 42 |
| 43 function getURLSetCookie() { |
| 44 return getServerURL('set-cookie?Foo=Bar'); |
| 45 } |
| 46 |
| 39 runTests([ | 47 runTests([ |
| 40 function testCancelRequest() { | 48 function testCancelRequest() { |
| 41 ignoreUnexpected = true; | 49 ignoreUnexpected = true; |
| 42 expect( | 50 expect( |
| 43 [ | 51 [ |
| 44 { label: "onErrorOccurred", | 52 { label: "onErrorOccurred", |
| 45 event: "onErrorOccurred", | 53 event: "onErrorOccurred", |
| 46 details: { | 54 details: { |
| 47 url: getURLHttpSimple(), | 55 url: getURLHttpSimple(), |
| 48 fromCache: false, | 56 fromCache: false, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 }); | 223 }); |
| 216 chrome.tabs.executeScript(tabId, | 224 chrome.tabs.executeScript(tabId, |
| 217 { | 225 { |
| 218 code: "chrome.extension.sendRequest(" + | 226 code: "chrome.extension.sendRequest(" + |
| 219 "{pass: document.body.innerText.indexOf('Mozilla') == -1});" | 227 "{pass: document.body.innerText.indexOf('Mozilla') == -1});" |
| 220 }); | 228 }); |
| 221 }); | 229 }); |
| 222 }); | 230 }); |
| 223 }, | 231 }, |
| 224 | 232 |
| 233 function testAddResponseHeader() { |
| 234 ignoreUnexpected = true; |
| 235 expect(); // Used for initialization. |
| 236 onRequest.addRules( |
| 237 [{conditions: [new RequestMatcher()], |
| 238 actions: [new AddResponseHeader({name: "Set-Cookie", value: "Bar=baz"})] |
| 239 }], |
| 240 function() { |
| 241 navigateAndWait(getURLEchoUserAgent(), function() { |
| 242 chrome.test.listenOnce(chrome.extension.onRequest, function(request) { |
| 243 chrome.test.assertTrue(request.pass, "Cookie was not added."); |
| 244 }); |
| 245 chrome.tabs.executeScript(tabId, |
| 246 { |
| 247 code: "chrome.extension.sendRequest(" + |
| 248 "{pass: document.cookie.indexOf('Bar') != -1});" |
| 249 }); |
| 250 }); |
| 251 }); |
| 252 }, |
| 253 |
| 254 function testRemoveResponseHeader() { |
| 255 ignoreUnexpected = true; |
| 256 expect(); // Used for initialization. |
| 257 onRequest.addRules( |
| 258 [{conditions: [new RequestMatcher()], |
| 259 actions: [new RemoveResponseHeader({name: "Set-Cookie", |
| 260 value: "FoO=bAR"})] |
| 261 }], |
| 262 function() { |
| 263 navigateAndWait(getURLSetCookie(), function() { |
| 264 chrome.test.listenOnce(chrome.extension.onRequest, function(request) { |
| 265 chrome.test.assertTrue(request.pass, "Cookie was not removed."); |
| 266 }); |
| 267 chrome.tabs.executeScript(tabId, |
| 268 { |
| 269 code: "chrome.extension.sendRequest(" + |
| 270 "{pass: document.cookie.indexOf('Foo') == -1});" |
| 271 }); |
| 272 }); |
| 273 }); |
| 274 }, |
| 275 |
| 225 function testPriorities() { | 276 function testPriorities() { |
| 226 ignoreUnexpected = true; | 277 ignoreUnexpected = true; |
| 227 expect( | 278 expect( |
| 228 [ | 279 [ |
| 229 { label: "onCompleted", | 280 { label: "onCompleted", |
| 230 event: "onCompleted", | 281 event: "onCompleted", |
| 231 details: { | 282 details: { |
| 232 url: getURLHttpSimple(), | 283 url: getURLHttpSimple(), |
| 233 statusCode: 200, | 284 statusCode: 200, |
| 234 fromCache: false, | 285 fromCache: false, |
| 235 statusLine: "HTTP/1.0 200 OK", | 286 statusLine: "HTTP/1.0 200 OK", |
| 236 ip: "127.0.0.1", | 287 ip: "127.0.0.1", |
| 237 } | 288 } |
| 238 } | 289 } |
| 239 ], | 290 ], |
| 240 [ ["onCompleted"] ]); | 291 [ ["onCompleted"] ]); |
| 241 | 292 |
| 242 onRequest.addRules( | 293 onRequest.addRules( |
| 243 [ {conditions: [new RequestMatcher({url: {pathContains: "simpleLoad"}})], | 294 [ {conditions: [new RequestMatcher({url: {pathContains: "simpleLoad"}})], |
| 244 actions: [new CancelRequest()]}, | 295 actions: [new CancelRequest()]}, |
| 245 {conditions: [new RequestMatcher({url: {pathContains: "a.html"}})], | 296 {conditions: [new RequestMatcher({url: {pathContains: "a.html"}})], |
| 246 actions: [new IgnoreRules({lowerPriorityThan: 200})], | 297 actions: [new IgnoreRules({lowerPriorityThan: 200})], |
| 247 priority: 200} | 298 priority: 200} |
| 248 ], | 299 ], |
| 249 function() {navigateAndWait(getURLHttpSimple());} | 300 function() {navigateAndWait(getURLHttpSimple());} |
| 250 ); | 301 ); |
| 251 }, | 302 }, |
| 252 | 303 |
| 253 ]); | 304 ]); |
| OLD | NEW |