OLD | NEW |
(Empty) | |
| 1 var initialize_InterceptionTest = function() { |
| 2 |
| 3 var interceptionRequestParams = {}; |
| 4 var requestIdToFilename = {}; |
| 5 var loggedMessages = {}; |
| 6 var requestIdToCanonicalInterceptionId = {}; |
| 7 var idToCanoncalId = {}; |
| 8 var nextId = 1; |
| 9 |
| 10 function getNextId() |
| 11 { |
| 12 return "ID " + nextId++; |
| 13 } |
| 14 |
| 15 function canonicalId(id) |
| 16 { |
| 17 if (!idToCanoncalId.hasOwnProperty(id)) |
| 18 idToCanoncalId[id] = getNextId(); |
| 19 return idToCanoncalId[id]; |
| 20 } |
| 21 |
| 22 function log(id, message) |
| 23 { |
| 24 if (!loggedMessages.hasOwnProperty(id)) |
| 25 loggedMessages[id] = []; |
| 26 loggedMessages[id].push(message); |
| 27 } |
| 28 |
| 29 function completeTest(message) |
| 30 { |
| 31 // The order in which network events occur is not fully deterministic so we |
| 32 // sort based on the interception ID to try and make the test non-flaky. |
| 33 for (var property in loggedMessages) { |
| 34 if (loggedMessages.hasOwnProperty(property)) { |
| 35 var messages = loggedMessages[property]; |
| 36 for (var i = 0; i < messages.length; i++) { |
| 37 InspectorTest.log(messages[i]); |
| 38 } |
| 39 } |
| 40 } |
| 41 |
| 42 if (message) |
| 43 InspectorTest.log(message); |
| 44 |
| 45 InspectorTest.completeTest(); |
| 46 } |
| 47 |
| 48 InspectorTest.startInterceptionTest = function(interceptedRequestDict, |
| 49 interceptedRedirectDict, |
| 50 numConsoleLogsToWaitFor) { |
| 51 if (typeof numConsoleLogsToWaitFor === "undefined") |
| 52 numConsoleLogsToWaitFor = 0; |
| 53 |
| 54 InspectorTest.eventHandler["Network.interceptedRequest"] = onInterceptedRequ
est; |
| 55 InspectorTest.eventHandler["Network.interceptedRedirect"] = onInterceptedRed
irect; |
| 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.enableFetchInterception", {"enabled": true}, |
| 80 didEnableFetchInterception); |
| 81 } |
| 82 |
| 83 function didEnableFetchInterception(messageObject) |
| 84 { |
| 85 if (messageObject.error) { |
| 86 completeTest("FAIL: Couldn't enable fetch interception" + |
| 87 messageObject.error.message); |
| 88 return; |
| 89 } |
| 90 InspectorTest.log("Fetch 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 onInterceptedRequest(event) |
| 120 { |
| 121 var filename = event.params.request.url.split('/').pop(); |
| 122 var id = canonicalId(event.params.requestId); |
| 123 interceptionRequestParams[id] = event.params.request; |
| 124 if (interceptedRequestDict.hasOwnProperty(filename)) { |
| 125 log(id, "Network.interceptedRequest " + id + " " + |
| 126 event.params.request.method + " " + filename); |
| 127 interceptedRequestDict[filename](event); |
| 128 } else { |
| 129 completeTest("FAILED: unexpected request interception " + |
| 130 JSON.stringify(event.params)); |
| 131 } |
| 132 } |
| 133 |
| 134 function onInterceptedRedirect(event) |
| 135 { |
| 136 var redirectFilename = event.params.redirectUrl.split('/').pop(); |
| 137 var id = canonicalId(event.params.requestId); |
| 138 if (interceptedRedirectDict.hasOwnProperty(redirectFilename)) { |
| 139 log(id, "Network.interceptedRedirect " + id + " " + |
| 140 event.params.responseStatusCode + " " + |
| 141 interceptionRequestParams[id].url.split('/').pop() + |
| 142 " -> " + redirectFilename); |
| 143 interceptedRedirectDict[redirectFilename](event); |
| 144 } else { |
| 145 completeTest("FAILED: unexpected redirect interception " + |
| 146 JSON.stringify(event.params)); |
| 147 } |
| 148 interceptionRequestParams[id].url = event.params.redirectUrl; |
| 149 } |
| 150 |
| 151 function onLoadingFailed(event) |
| 152 { |
| 153 var id = canonicalId(event.params.requestId); |
| 154 log(id, "Network.loadingFailed " + id + " " + |
| 155 requestIdToFilename[event.params.requestId] + " " + |
| 156 event.params.errorText); |
| 157 } |
| 158 |
| 159 function onRequestWillBeSent(event) |
| 160 { |
| 161 var filename = event.params.request.url.split('/').pop(); |
| 162 requestIdToFilename[event.params.requestId] = filename; |
| 163 } |
| 164 |
| 165 function onResponseReceived(event) |
| 166 { |
| 167 var response = event.params.response; |
| 168 var filename = response.url.split('/').pop(); |
| 169 var id = canonicalId(event.params.requestId); |
| 170 log(id, "Network.responseReceived " + filename + " " + response.status + |
| 171 " " + response.mimeType + " requestId = " + id); |
| 172 } |
| 173 |
| 174 function onStop() |
| 175 { |
| 176 frameStoppedLoading = true; |
| 177 log(getNextId(), "Page.frameStoppedLoading"); |
| 178 |
| 179 maybeCompleteTest(); |
| 180 } |
| 181 |
| 182 function onConsoleAPICalled(messageObject) |
| 183 { |
| 184 if (messageObject.params.type !== "log") |
| 185 return; |
| 186 |
| 187 numConsoleLogsToWaitFor--; |
| 188 maybeCompleteTest(); |
| 189 } |
| 190 |
| 191 // Wait until we've seen Page.frameStoppedLoading and the expected number of |
| 192 // console logs. |
| 193 function maybeCompleteTest() { |
| 194 if (numConsoleLogsToWaitFor === 0 && frameStoppedLoading) |
| 195 completeTest(); |
| 196 } |
| 197 |
| 198 enableNetwork(); |
| 199 } |
| 200 |
| 201 InspectorTest.allowRequest = function(event) { |
| 202 var id = canonicalId(event.params.requestId); |
| 203 log(id, "Network.allowRequest " + id); |
| 204 InspectorTest.sendCommand( |
| 205 "Network.allowRequest", { "requestId": event.params.requestId }); |
| 206 } |
| 207 |
| 208 InspectorTest.modifyRequest = function(event, params) { |
| 209 var id = canonicalId(event.params.requestId); |
| 210 var mods = []; |
| 211 for (property in params) { |
| 212 if (!params.hasOwnProperty(property)) |
| 213 continue; |
| 214 if (property === "url") { |
| 215 var newUrl = params["url"]; |
| 216 var filename = interceptionRequestParams[id].url; |
| 217 mods.push("url " + filename.split('/').pop() + " -> " + newUrl); |
| 218 var directoryPath = |
| 219 filename.substring(0, filename.lastIndexOf('/') + 1); |
| 220 params["url"] = directoryPath + newUrl; |
| 221 } else { |
| 222 mods.push(property + " " + |
| 223 JSON.stringify(interceptionRequestParams[id][property]) + |
| 224 " -> " + JSON.stringify(params[property])); |
| 225 } |
| 226 } |
| 227 |
| 228 log(id, "Network.modifyRequest " + id + ": " + mods.join("; ")); |
| 229 params["requestId"] = event.params.requestId; |
| 230 InspectorTest.sendCommand("Network.modifyRequest", params); |
| 231 } |
| 232 |
| 233 InspectorTest.blockRequest = function(event, errorReason) { |
| 234 var id = canonicalId(event.params.requestId); |
| 235 log(id, "Network.blockRequest " + id + " " + errorReason); |
| 236 InspectorTest.sendCommand("Network.blockRequest", { |
| 237 "requestId": event.params.requestId, "errorReason": errorReason}); |
| 238 } |
| 239 |
| 240 InspectorTest.mockResponse = function(event, rawResponse) { |
| 241 var id = canonicalId(event.params.requestId); |
| 242 log(id, "Network.mockResponse " + id); |
| 243 InspectorTest.sendCommand("Network.mockResponse", { |
| 244 "requestId": event.params.requestId, |
| 245 "rawResponse": rawResponse}); |
| 246 } |
| 247 |
| 248 } |
OLD | NEW |