Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/test/data/extensions/api_test/webrequest/framework.js

Issue 2449913002: Support WebSocket in WebRequest API. (Closed)
Patch Set: Refactor tests; add test; update documentation. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 getURL = chrome.extension.getURL; 5 var getURL = chrome.extension.getURL;
6 var deepEq = chrome.test.checkDeepEq; 6 var deepEq = chrome.test.checkDeepEq;
7 var expectedEventData; 7 var expectedEventData;
8 var capturedEventData; 8 var capturedEventData;
9 var capturedUnexpectedData; 9 var capturedUnexpectedData;
10 var expectedEventOrder; 10 var expectedEventOrder;
11 var tabId; 11 var tabId;
12 var tabIdMap; 12 var tabIdMap;
13 var frameIdMap; 13 var frameIdMap;
14 var testWebSocketPort;
14 var testServerPort; 15 var testServerPort;
15 var testServer = "www.a.com"; 16 var testServer = "www.a.com";
16 var defaultScheme = "http"; 17 var defaultScheme = "http";
17 var eventsCaptured; 18 var eventsCaptured;
18 var listeners = { 19 var listeners = {
19 'onBeforeRequest': [], 20 'onBeforeRequest': [],
20 'onBeforeSendHeaders': [], 21 'onBeforeSendHeaders': [],
21 'onAuthRequired': [], 22 'onAuthRequired': [],
22 'onSendHeaders': [], 23 'onSendHeaders': [],
23 'onHeadersReceived': [], 24 'onHeadersReceived': [],
24 'onResponseStarted': [], 25 'onResponseStarted': [],
25 'onBeforeRedirect': [], 26 'onBeforeRedirect': [],
26 'onCompleted': [], 27 'onCompleted': [],
27 'onErrorOccurred': [] 28 'onErrorOccurred': []
28 }; 29 };
29 30
30 // If true, don't bark on events that were not registered via expect(). 31 // If true, don't bark on events that were not registered via expect().
31 // These events are recorded in capturedUnexpectedData instead of 32 // These events are recorded in capturedUnexpectedData instead of
32 // capturedEventData. 33 // capturedEventData.
33 var ignoreUnexpected = false; 34 var ignoreUnexpected = false;
34 35
35 // This is a debugging aid to print all received events as well as the 36 // This is a debugging aid to print all received events as well as the
36 // information whether they were expected. 37 // information whether they were expected.
37 var logAllRequests = false; 38 var logAllRequests = false;
38 39
40 // Runs the |tests| using the |tab| as a default tab.
41 function runTestsForTab(tests, tab) {
42 tabId = tab.id;
43 tabIdMap = {"-1": -1};
44 tabIdMap[tabId] = 0;
45 chrome.test.getConfig(function(config) {
46 testServerPort = config.testServer.port;
47 testWebSocketPort = config.testWebSocketPort;
48 chrome.test.runTests(tests);
49 });
50 }
51
52 // Creates an "about:blank" tab and runs |tests| with this tab as default.
39 function runTests(tests) { 53 function runTests(tests) {
40 var waitForAboutBlank = function(_, info, tab) { 54 var waitForAboutBlank = function(_, info, tab) {
41 if (info.status == "complete" && tab.url == "about:blank") { 55 if (info.status == "complete" && tab.url == "about:blank") {
42 tabId = tab.id;
43 tabIdMap = {"-1": -1};
44 tabIdMap[tabId] = 0;
45 chrome.tabs.onUpdated.removeListener(waitForAboutBlank); 56 chrome.tabs.onUpdated.removeListener(waitForAboutBlank);
46 chrome.test.getConfig(function(config) { 57 runTestsForTab(tests, tab);
47 testServerPort = config.testServer.port;
48 chrome.test.runTests(tests);
49 });
50 } 58 }
51 }; 59 };
52 chrome.tabs.onUpdated.addListener(waitForAboutBlank); 60 chrome.tabs.onUpdated.addListener(waitForAboutBlank);
53 chrome.tabs.create({url: "about:blank"}); 61 chrome.tabs.create({url: "about:blank"});
54 } 62 }
55 63
56 // Returns an URL from the test server, fixing up the port. Must be called 64 // Returns an URL from the test server, fixing up the port. Must be called
57 // from within a test case passed to runTests. 65 // from within a test case passed to runTests.
58 function getServerURL(path, opt_host, opt_scheme) { 66 function getServerURL(path, opt_host, opt_scheme) {
59 if (!testServerPort) 67 if (!testServerPort)
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 didMatchTabAndFrameId && 205 didMatchTabAndFrameId &&
198 exp.details.method === details.method && 206 exp.details.method === details.method &&
199 exp.details.url === details.url && 207 exp.details.url === details.url &&
200 exp.details.type === details.type; 208 exp.details.type === details.type;
201 }); 209 });
202 } 210 }
203 211
204 function captureEvent(name, details, callback) { 212 function captureEvent(name, details, callback) {
205 // Ignore system-level requests like safebrowsing updates and favicon fetches 213 // Ignore system-level requests like safebrowsing updates and favicon fetches
206 // since they are unpredictable. 214 // since they are unpredictable.
207 if ((details.type == "other" && !details.url.includes('dont-ignore-me')) || 215 // TODO(pkalinnikov): Remove the startsWith('ws') check once a new type gets
Devlin 2017/02/14 01:21:38 When will this be?
pkalinnikov 2017/02/14 13:49:51 I was thinking to address this in a follow-up CL.
Devlin 2017/02/14 15:55:24 I don't think this should be too complicated, and
pkalinnikov 2017/02/15 19:15:00 Added new type in a separate CL.
216 // introduced for the WebSocket requests.
217 if ((details.type == "other" && !details.url.includes('dont-ignore-me') &&
218 !details.url.startsWith('ws')) ||
208 isUnexpectedDetachedRequest(name, details) || 219 isUnexpectedDetachedRequest(name, details) ||
209 details.url.match(/\/favicon.ico$/) || 220 details.url.match(/\/favicon.ico$/) ||
210 details.url.match(/https:\/\/dl.google.com/)) 221 details.url.match(/https:\/\/dl.google.com/)) {
211 return; 222 return;
223 }
212 224
213 // Pull the extra per-event options out of the expected data. These let 225 // Pull the extra per-event options out of the expected data. These let
214 // us specify special return values per event. 226 // us specify special return values per event.
215 var currentIndex = capturedEventData.length; 227 var currentIndex = capturedEventData.length;
216 var extraOptions; 228 var extraOptions;
217 var retval; 229 var retval;
218 if (expectedEventData.length > currentIndex) { 230 if (expectedEventData.length > currentIndex) {
219 retval = 231 retval =
220 expectedEventData[currentIndex].retval_function ? 232 expectedEventData[currentIndex].retval_function ?
221 expectedEventData[currentIndex].retval_function(name, details) : 233 expectedEventData[currentIndex].retval_function(name, details) :
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 helper('onHeadersReceived'); 425 helper('onHeadersReceived');
414 helper('onResponseStarted'); 426 helper('onResponseStarted');
415 helper('onBeforeRedirect'); 427 helper('onBeforeRedirect');
416 helper('onCompleted'); 428 helper('onCompleted');
417 helper('onErrorOccurred'); 429 helper('onErrorOccurred');
418 } 430 }
419 431
420 function resetDeclarativeRules() { 432 function resetDeclarativeRules() {
421 chrome.declarativeWebRequest.onRequest.removeRules(); 433 chrome.declarativeWebRequest.onRequest.removeRules();
422 } 434 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698