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 enableNetwork() |
| 65 { |
| 66 InspectorTest.log("Test started"); |
| 67 InspectorTest.sendCommand("Network.enable", {}, didEnableNetwork); |
| 68 } |
| 69 |
| 70 function didEnableNetwork(messageObject) |
| 71 { |
| 72 if (messageObject.error) { |
| 73 completeTest("FAIL: Couldn't enable network agent" + |
| 74 messageObject.error.message); |
| 75 return; |
| 76 } |
| 77 InspectorTest.log("Network agent enabled"); |
| 78 InspectorTest.sendCommand( |
| 79 "Network.enableRequestInterception", {"enabled": true}, |
| 80 didEnableRequestInterception); |
| 81 } |
| 82 |
| 83 function didEnableRequestInterception(messageObject) |
| 84 { |
| 85 if (messageObject.error) { |
| 86 completeTest("FAIL: Couldn't enable fetch interception" + |
| 87 messageObject.error.message); |
| 88 return; |
| 89 } |
| 90 InspectorTest.log("Request interception enabled"); |
| 91 InspectorTest.sendCommand("Page.enable", {}, didEnablePage); |
| 92 } |
| 93 |
| 94 function didEnablePage(messageObject) |
| 95 { |
| 96 if (messageObject.error) { |
| 97 completeTest("FAIL: Couldn't enable page agent" + |
| 98 messageObject.error.message); |
| 99 return; |
| 100 } |
| 101 InspectorTest.log("Page agent enabled"); |
| 102 |
| 103 InspectorTest.sendCommand("Runtime.enable", {}, didEnableRuntime); |
| 104 } |
| 105 |
| 106 function didEnableRuntime(messageObject) |
| 107 { |
| 108 if (messageObject.error) { |
| 109 completeTest("FAIL: Couldn't enable runtime agent" + |
| 110 messageObject.error.message); |
| 111 return; |
| 112 } |
| 113 InspectorTest.log("Runtime agent enabled"); |
| 114 |
| 115 InspectorTest.sendCommand( |
| 116 "Runtime.evaluate", { "expression": "appendIframe()"}); |
| 117 } |
| 118 |
| 119 function onRequestIntercepted(event) |
| 120 { |
| 121 var filename = event.params.request.url.split('/').pop(); |
| 122 var id = canonicalId(event.params.InterceptionId); |
| 123 filenameToInterceptionId[filename] = id; |
| 124 if (!requestInterceptedDict.hasOwnProperty(filename)) { |
| 125 completeTest("FAILED: unexpected request interception " + |
| 126 JSON.stringify(event.params)); |
| 127 return; |
| 128 } |
| 129 if (event.params.hasOwnProperty("redirectUrl")) { |
| 130 log(id, "Network.requestIntercepted " + id + " " + |
| 131 event.params.redirectStatusCode + " redirect " + |
| 132 interceptionRequestParams[id].url.split('/').pop() + |
| 133 " -> " + event.params.redirectUrl.split('/').pop()); |
| 134 interceptionRequestParams[id].url = event.params.redirectUrl; |
| 135 } else { |
| 136 interceptionRequestParams[id] = event.params.request; |
| 137 log(id, "Network.requestIntercepted " + id + " " + |
| 138 event.params.request.method + " " + filename); |
| 139 } |
| 140 requestInterceptedDict[filename](event); |
| 141 } |
| 142 |
| 143 function onLoadingFailed(event) |
| 144 { |
| 145 var filename = requestIdToFilename[event.params.requestId]; |
| 146 var id = filenameToInterceptionId[filename]; |
| 147 log(id, "Network.loadingFailed " + filename + " " + |
| 148 event.params.errorText); |
| 149 } |
| 150 |
| 151 function onRequestWillBeSent(event) |
| 152 { |
| 153 var filename = event.params.request.url.split('/').pop(); |
| 154 requestIdToFilename[event.params.requestId] = filename; |
| 155 } |
| 156 |
| 157 function onResponseReceived(event) |
| 158 { |
| 159 var response = event.params.response; |
| 160 var filename = response.url.split('/').pop(); |
| 161 var id = filenameToInterceptionId[filename]; |
| 162 log(id, "Network.responseReceived " + filename + " " + response.status + |
| 163 " " + response.mimeType); |
| 164 } |
| 165 |
| 166 function onStop() |
| 167 { |
| 168 frameStoppedLoading = true; |
| 169 log(getNextId(), "Page.frameStoppedLoading"); |
| 170 |
| 171 maybeCompleteTest(); |
| 172 } |
| 173 |
| 174 function onConsoleAPICalled(messageObject) |
| 175 { |
| 176 if (messageObject.params.type !== "log") |
| 177 return; |
| 178 |
| 179 numConsoleLogsToWaitFor--; |
| 180 maybeCompleteTest(); |
| 181 } |
| 182 |
| 183 // Wait until we've seen Page.frameStoppedLoading and the expected number of |
| 184 // console logs. |
| 185 function maybeCompleteTest() { |
| 186 if (numConsoleLogsToWaitFor === 0 && frameStoppedLoading) |
| 187 completeTest(); |
| 188 } |
| 189 |
| 190 enableNetwork(); |
| 191 } |
| 192 |
| 193 InspectorTest.allowRequest = function(event) { |
| 194 var id = canonicalId(event.params.InterceptionId); |
| 195 log(id, "allowRequest " + id); |
| 196 InspectorTest.sendCommand("Network.continueInterceptedRequest", { |
| 197 "interceptionId": event.params.InterceptionId |
| 198 }); |
| 199 } |
| 200 |
| 201 InspectorTest.modifyRequest = function(event, params) { |
| 202 var id = canonicalId(event.params.InterceptionId); |
| 203 var mods = []; |
| 204 for (property in params) { |
| 205 if (!params.hasOwnProperty(property)) |
| 206 continue; |
| 207 if (property === "url") { |
| 208 var newUrl = params["url"]; |
| 209 var filename = interceptionRequestParams[id].url; |
| 210 mods.push("url " + filename.split('/').pop() + " -> " + newUrl); |
| 211 var directoryPath = |
| 212 filename.substring(0, filename.lastIndexOf('/') + 1); |
| 213 params["url"] = directoryPath + newUrl; |
| 214 } else { |
| 215 mods.push(property + " " + |
| 216 JSON.stringify(interceptionRequestParams[id][property]) + |
| 217 " -> " + JSON.stringify(params[property])); |
| 218 } |
| 219 } |
| 220 |
| 221 log(id, "modifyRequest " + id + ": " + mods.join("; ")); |
| 222 params["interceptionId"] = event.params.InterceptionId; |
| 223 InspectorTest.sendCommand("Network.continueInterceptedRequest", params); |
| 224 } |
| 225 |
| 226 InspectorTest.blockRequest = function(event, errorReason) { |
| 227 var id = canonicalId(event.params.InterceptionId); |
| 228 log(id, "blockRequest " + id + " " + errorReason); |
| 229 InspectorTest.sendCommand("Network.continueInterceptedRequest", { |
| 230 "interceptionId": event.params.InterceptionId, |
| 231 "errorReason": errorReason |
| 232 }); |
| 233 } |
| 234 |
| 235 InspectorTest.mockResponse = function(event, rawResponse) { |
| 236 var id = canonicalId(event.params.InterceptionId); |
| 237 log(id, "mockResponse " + id); |
| 238 InspectorTest.sendCommand("Network.continueInterceptedRequest", { |
| 239 "interceptionId": event.params.InterceptionId, |
| 240 "rawResponse": btoa(rawResponse) |
| 241 }); |
| 242 } |
| 243 |
| 244 } |
OLD | NEW |