OLD | NEW |
| (Empty) |
1 var initialize_InterceptionTest = function() { | |
2 | |
3 var interceptionRequestParams = {}; | |
4 var requestIdToFilename = {}; | |
5 var filenameToInterceptionId = {}; | |
6 var loggedMessages = {}; | |
7 var InterceptionIdToCanonicalInterceptionId = {}; | |
8 var idToCanoncalId = {}; | |
9 var nextId = 1; | |
10 | |
11 function getNextId() | |
12 { | |
13 return "ID " + nextId++; | |
14 } | |
15 | |
16 function canonicalId(id) | |
17 { | |
18 if (!idToCanoncalId.hasOwnProperty(id)) | |
19 idToCanoncalId[id] = getNextId(); | |
20 return idToCanoncalId[id]; | |
21 } | |
22 | |
23 function log(id, message) | |
24 { | |
25 testRunner.logToStderr("id " + id + " " + message); | |
26 if (!loggedMessages.hasOwnProperty(id)) | |
27 loggedMessages[id] = []; | |
28 loggedMessages[id].push(message); | |
29 } | |
30 | |
31 function completeTest(message) | |
32 { | |
33 // The order in which network events occur is not fully deterministic so we | |
34 // sort based on the interception ID to try and make the test non-flaky. | |
35 for (var property in loggedMessages) { | |
36 if (loggedMessages.hasOwnProperty(property)) { | |
37 var messages = loggedMessages[property]; | |
38 for (var i = 0; i < messages.length; i++) { | |
39 InspectorTest.log(messages[i]); | |
40 } | |
41 } | |
42 } | |
43 | |
44 if (message) | |
45 InspectorTest.log(message); | |
46 | |
47 InspectorTest.completeTest(); | |
48 } | |
49 | |
50 InspectorTest.startInterceptionTest = function(requestInterceptedDict, | |
51 numConsoleLogsToWaitFor) { | |
52 if (typeof numConsoleLogsToWaitFor === "undefined") | |
53 numConsoleLogsToWaitFor = 0; | |
54 | |
55 InspectorTest.eventHandler["Network.requestIntercepted"] = onRequestIntercep
ted; | |
56 InspectorTest.eventHandler["Network.loadingFailed"] = onLoadingFailed; | |
57 InspectorTest.eventHandler["Network.requestWillBeSent"] = onRequestWillBeSen
t; | |
58 InspectorTest.eventHandler["Network.responseReceived"] = onResponseReceived; | |
59 InspectorTest.eventHandler["Runtime.consoleAPICalled"] = onConsoleAPICalled; | |
60 InspectorTest.eventHandler["Page.frameStoppedLoading"] = onStop; | |
61 | |
62 var frameStoppedLoading = false; | |
63 | |
64 function getInterceptionId(filename) { | |
65 if (!filenameToInterceptionId.hasOwnProperty(filename)) { | |
66 filenameToInterceptionId[filename] = getNextId() | |
67 } | |
68 return filenameToInterceptionId[filename]; | |
69 } | |
70 | |
71 function enableNetwork() | |
72 { | |
73 InspectorTest.log("Test started"); | |
74 InspectorTest.sendCommand("Network.enable", {}, didEnableNetwork); | |
75 } | |
76 | |
77 function didEnableNetwork(messageObject) | |
78 { | |
79 if (messageObject.error) { | |
80 completeTest("FAIL: Couldn't enable network agent" + | |
81 messageObject.error.message); | |
82 return; | |
83 } | |
84 InspectorTest.log("Network agent enabled"); | |
85 InspectorTest.sendCommand( | |
86 "Network.enableRequestInterception", {"enabled": true}, | |
87 didEnableRequestInterception); | |
88 } | |
89 | |
90 function didEnableRequestInterception(messageObject) | |
91 { | |
92 if (messageObject.error) { | |
93 completeTest("FAIL: Couldn't enable fetch interception" + | |
94 messageObject.error.message); | |
95 return; | |
96 } | |
97 InspectorTest.log("Request interception enabled"); | |
98 InspectorTest.sendCommand("Page.enable", {}, didEnablePage); | |
99 } | |
100 | |
101 function didEnablePage(messageObject) | |
102 { | |
103 if (messageObject.error) { | |
104 completeTest("FAIL: Couldn't enable page agent" + | |
105 messageObject.error.message); | |
106 return; | |
107 } | |
108 InspectorTest.log("Page agent enabled"); | |
109 | |
110 InspectorTest.sendCommand("Runtime.enable", {}, didEnableRuntime); | |
111 } | |
112 | |
113 function didEnableRuntime(messageObject) | |
114 { | |
115 if (messageObject.error) { | |
116 completeTest("FAIL: Couldn't enable runtime agent" + | |
117 messageObject.error.message); | |
118 return; | |
119 } | |
120 InspectorTest.log("Runtime agent enabled"); | |
121 | |
122 InspectorTest.sendCommand( | |
123 "Runtime.evaluate", { "expression": "appendIframe()"}); | |
124 } | |
125 | |
126 function onRequestIntercepted(event) | |
127 { | |
128 var filename = event.params.request.url.split('/').pop(); | |
129 var id = canonicalId(event.params.interceptionId); | |
130 filenameToInterceptionId[filename] = id; | |
131 if (!requestInterceptedDict.hasOwnProperty(filename)) { | |
132 completeTest("FAILED: unexpected request interception " + | |
133 JSON.stringify(event.params)); | |
134 return; | |
135 } | |
136 if (event.params.hasOwnProperty("authChallenge")) { | |
137 log(id, "Auth required for " + id); | |
138 requestInterceptedDict[filename + '+Auth'](event); | |
139 return; | |
140 } else if (event.params.hasOwnProperty("redirectUrl")) { | |
141 log(id, "Network.requestIntercepted " + id + " " + | |
142 event.params.redirectStatusCode + " redirect " + | |
143 interceptionRequestParams[id].url.split('/').pop() + | |
144 " -> " + event.params.redirectUrl.split('/').pop()); | |
145 interceptionRequestParams[id].url = event.params.redirectUrl; | |
146 } else { | |
147 interceptionRequestParams[id] = event.params.request; | |
148 log(id, "Network.requestIntercepted " + id + " " + | |
149 event.params.request.method + " " + filename + " type: " + | |
150 event.params.resourceType); | |
151 } | |
152 requestInterceptedDict[filename](event); | |
153 } | |
154 | |
155 function onLoadingFailed(event) | |
156 { | |
157 var filename = requestIdToFilename[event.params.requestId]; | |
158 var id = getInterceptionId(filename); | |
159 log(id, "Network.loadingFailed " + filename + " " + | |
160 event.params.errorText); | |
161 } | |
162 | |
163 function onRequestWillBeSent(event) | |
164 { | |
165 var filename = event.params.request.url.split('/').pop(); | |
166 requestIdToFilename[event.params.requestId] = filename; | |
167 } | |
168 | |
169 function onResponseReceived(event) | |
170 { | |
171 var response = event.params.response; | |
172 var filename = response.url.split('/').pop(); | |
173 var id = getInterceptionId(filename); | |
174 log(id, "Network.responseReceived " + filename + " " + response.status + | |
175 " " + response.mimeType); | |
176 } | |
177 | |
178 function onStop() | |
179 { | |
180 frameStoppedLoading = true; | |
181 log(getNextId(), "Page.frameStoppedLoading"); | |
182 | |
183 maybeCompleteTest(); | |
184 } | |
185 | |
186 function onConsoleAPICalled(messageObject) | |
187 { | |
188 if (messageObject.params.type !== "log") | |
189 return; | |
190 | |
191 numConsoleLogsToWaitFor--; | |
192 maybeCompleteTest(); | |
193 } | |
194 | |
195 // Wait until we've seen Page.frameStoppedLoading and the expected number of | |
196 // console logs. | |
197 function maybeCompleteTest() { | |
198 if (numConsoleLogsToWaitFor === 0 && frameStoppedLoading) | |
199 completeTest(); | |
200 } | |
201 | |
202 enableNetwork(); | |
203 } | |
204 | |
205 InspectorTest.allowRequest = function(event) { | |
206 var id = canonicalId(event.params.interceptionId); | |
207 log(id, "allowRequest " + id); | |
208 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
209 "interceptionId": event.params.interceptionId | |
210 }); | |
211 } | |
212 | |
213 InspectorTest.modifyRequest = function(event, params) { | |
214 var id = canonicalId(event.params.interceptionId); | |
215 var mods = []; | |
216 for (property in params) { | |
217 if (!params.hasOwnProperty(property)) | |
218 continue; | |
219 if (property === "url") { | |
220 var newUrl = params["url"]; | |
221 var filename = interceptionRequestParams[id].url; | |
222 mods.push("url " + filename.split('/').pop() + " -> " + newUrl); | |
223 var directoryPath = | |
224 filename.substring(0, filename.lastIndexOf('/') + 1); | |
225 params["url"] = directoryPath + newUrl; | |
226 } else { | |
227 mods.push(property + " " + | |
228 JSON.stringify(interceptionRequestParams[id][property]) + | |
229 " -> " + JSON.stringify(params[property])); | |
230 } | |
231 } | |
232 | |
233 log(id, "modifyRequest " + id + ": " + mods.join("; ")); | |
234 params["interceptionId"] = event.params.interceptionId; | |
235 InspectorTest.sendCommand("Network.continueInterceptedRequest", params); | |
236 } | |
237 | |
238 InspectorTest.blockRequest = function(event, errorReason) { | |
239 var id = canonicalId(event.params.interceptionId); | |
240 log(id, "blockRequest " + id + " " + errorReason); | |
241 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
242 "interceptionId": event.params.interceptionId, | |
243 "errorReason": errorReason | |
244 }); | |
245 } | |
246 | |
247 InspectorTest.mockResponse = function(event, rawResponse) { | |
248 var id = canonicalId(event.params.interceptionId); | |
249 log(id, "mockResponse " + id); | |
250 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
251 "interceptionId": event.params.interceptionId, | |
252 "rawResponse": btoa(rawResponse) | |
253 }); | |
254 } | |
255 | |
256 InspectorTest.disableRequestInterception = function(event) { | |
257 var id = canonicalId(event.params.interceptionId); | |
258 log(id, "----- disableRequestInterception -----"); | |
259 InspectorTest.sendCommand("Network.enableRequestInterception", { | |
260 "enabled": false, | |
261 }); | |
262 } | |
263 | |
264 InspectorTest.cancelAuth = function(event) { | |
265 var id = canonicalId(event.params.interceptionId); | |
266 log(id, "----- Cancel Auth -----"); | |
267 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
268 "interceptionId": event.params.interceptionId, | |
269 "authChallengeResponse": {"response": "CancelAuth"} | |
270 }); | |
271 } | |
272 | |
273 InspectorTest.defaultAuth = function(event) { | |
274 var id = canonicalId(event.params.interceptionId); | |
275 log(id, "----- Use Default Auth -----"); | |
276 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
277 "interceptionId": event.params.interceptionId, | |
278 "authChallengeResponse": {"response": "Default"} | |
279 }); | |
280 } | |
281 | |
282 InspectorTest.provideAuthCredentials = function(event, username, password) { | |
283 var id = canonicalId(event.params.interceptionId); | |
284 log(id, "----- Provide Auth Credentials -----"); | |
285 InspectorTest.sendCommand("Network.continueInterceptedRequest", { | |
286 "interceptionId": event.params.interceptionId, | |
287 "authChallengeResponse": { | |
288 "response": "ProvideCredentials", | |
289 "username": username, | |
290 "password": password | |
291 } | |
292 }); | |
293 } | |
294 | |
295 } | |
OLD | NEW |