Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/interception-test.js |
| diff --git a/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/interception-test.js b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/interception-test.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5dddfb4fcd4d9ab7a9be3c430f95cec72f06a8d4 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/interception-test.js |
| @@ -0,0 +1,244 @@ |
| +var initialize_InterceptionTest = function() { |
| + |
| +var interceptionRequestParams = {}; |
| +var requestIdToFilename = {}; |
| +var filenameToInterceptionId = {}; |
| +var loggedMessages = {}; |
| +var InterceptionIdToCanonicalInterceptionId = {}; |
| +var idToCanoncalId = {}; |
| +var nextId = 1; |
| + |
| +function getNextId() |
| +{ |
| + return "ID " + nextId++; |
| +} |
| + |
| +function canonicalId(id) |
| +{ |
| + if (!idToCanoncalId.hasOwnProperty(id)) |
| + idToCanoncalId[id] = getNextId(); |
| + return idToCanoncalId[id]; |
| +} |
| + |
| +function log(id, message) |
| +{ |
| + testRunner.logToStderr("id " + id + " " + message); |
| + if (!loggedMessages.hasOwnProperty(id)) |
| + loggedMessages[id] = []; |
| + loggedMessages[id].push(message); |
| +} |
| + |
| +function completeTest(message) |
| +{ |
| + // The order in which network events occur is not fully deterministic so we |
| + // sort based on the interception ID to try and make the test non-flaky. |
|
dgozman
2017/05/25 21:29:36
Should we sort by url instead? That would be non-f
alex clarke (OOO till 29th)
2017/05/26 19:37:03
AFAIK the order of requests is deterministic.
|
| + for (var property in loggedMessages) { |
| + if (loggedMessages.hasOwnProperty(property)) { |
| + var messages = loggedMessages[property]; |
| + for (var i = 0; i < messages.length; i++) { |
| + InspectorTest.log(messages[i]); |
| + } |
| + } |
| + } |
| + |
| + if (message) |
| + InspectorTest.log(message); |
| + |
| + InspectorTest.completeTest(); |
| +} |
| + |
| +InspectorTest.startInterceptionTest = function(requestInterceptedDict, |
| + numConsoleLogsToWaitFor) { |
| + if (typeof numConsoleLogsToWaitFor === "undefined") |
| + numConsoleLogsToWaitFor = 0; |
| + |
| + InspectorTest.eventHandler["Network.requestIntercepted"] = onRequestIntercepted; |
| + InspectorTest.eventHandler["Network.loadingFailed"] = onLoadingFailed; |
| + InspectorTest.eventHandler["Network.requestWillBeSent"] = onRequestWillBeSent; |
| + InspectorTest.eventHandler["Network.responseReceived"] = onResponseReceived; |
| + InspectorTest.eventHandler["Runtime.consoleAPICalled"] = onConsoleAPICalled; |
| + InspectorTest.eventHandler["Page.frameStoppedLoading"] = onStop; |
| + |
| + var frameStoppedLoading = false; |
| + |
| + function enableNetwork() |
| + { |
| + InspectorTest.log("Test started"); |
| + InspectorTest.sendCommand("Network.enable", {}, didEnableNetwork); |
| + } |
| + |
| + function didEnableNetwork(messageObject) |
| + { |
| + if (messageObject.error) { |
| + completeTest("FAIL: Couldn't enable network agent" + |
| + messageObject.error.message); |
| + return; |
| + } |
| + InspectorTest.log("Network agent enabled"); |
| + InspectorTest.sendCommand( |
| + "Network.enableFetchInterception", {"enabled": true}, |
| + didEnableFetchInterception); |
| + } |
| + |
| + function didEnableFetchInterception(messageObject) |
| + { |
| + if (messageObject.error) { |
| + completeTest("FAIL: Couldn't enable fetch interception" + |
| + messageObject.error.message); |
| + return; |
| + } |
| + InspectorTest.log("Fetch interception enabled"); |
| + InspectorTest.sendCommand("Page.enable", {}, didEnablePage); |
| + } |
| + |
| + function didEnablePage(messageObject) |
| + { |
| + if (messageObject.error) { |
| + completeTest("FAIL: Couldn't enable page agent" + |
| + messageObject.error.message); |
| + return; |
| + } |
| + InspectorTest.log("Page agent enabled"); |
| + |
| + InspectorTest.sendCommand("Runtime.enable", {}, didEnableRuntime); |
| + } |
| + |
| + function didEnableRuntime(messageObject) |
| + { |
| + if (messageObject.error) { |
| + completeTest("FAIL: Couldn't enable runtime agent" + |
| + messageObject.error.message); |
| + return; |
| + } |
| + InspectorTest.log("Runtime agent enabled"); |
| + |
| + InspectorTest.sendCommand( |
| + "Runtime.evaluate", { "expression": "appendIframe()"}); |
| + } |
| + |
| + function onRequestIntercepted(event) |
| + { |
| + var filename = event.params.request.url.split('/').pop(); |
| + var id = canonicalId(event.params.InterceptionId); |
| + filenameToInterceptionId[filename] = id; |
| + if (!requestInterceptedDict.hasOwnProperty(filename)) { |
| + completeTest("FAILED: unexpected request interception " + |
| + JSON.stringify(event.params)); |
| + return; |
| + } |
| + if (event.params.hasOwnProperty("redirectUrl")) { |
| + log(id, "Network.requestIntercepted " + id + " " + |
| + event.params.redirectStatusCode + " redirect " + |
| + interceptionRequestParams[id].url.split('/').pop() + |
| + " -> " + event.params.redirectUrl.split('/').pop()); |
| + interceptionRequestParams[id].url = event.params.redirectUrl; |
| + } else { |
| + interceptionRequestParams[id] = event.params.request; |
| + log(id, "Network.requestIntercepted " + id + " " + |
| + event.params.request.method + " " + filename); |
| + } |
| + requestInterceptedDict[filename](event); |
| + } |
| + |
| + function onLoadingFailed(event) |
| + { |
| + var filename = requestIdToFilename[event.params.requestId]; |
| + var id = filenameToInterceptionId[filename]; |
| + log(id, "Network.loadingFailed " + filename + " " + |
| + event.params.errorText); |
| + } |
| + |
| + function onRequestWillBeSent(event) |
| + { |
| + var filename = event.params.request.url.split('/').pop(); |
| + requestIdToFilename[event.params.requestId] = filename; |
| + } |
| + |
| + function onResponseReceived(event) |
| + { |
| + var response = event.params.response; |
| + var filename = response.url.split('/').pop(); |
| + var id = filenameToInterceptionId[filename]; |
| + log(id, "Network.responseReceived " + filename + " " + response.status + |
| + " " + response.mimeType); |
| + } |
| + |
| + function onStop() |
| + { |
| + frameStoppedLoading = true; |
| + log(getNextId(), "Page.frameStoppedLoading"); |
| + |
| + maybeCompleteTest(); |
| + } |
| + |
| + function onConsoleAPICalled(messageObject) |
| + { |
| + if (messageObject.params.type !== "log") |
| + return; |
| + |
| + numConsoleLogsToWaitFor--; |
| + maybeCompleteTest(); |
| + } |
| + |
| + // Wait until we've seen Page.frameStoppedLoading and the expected number of |
| + // console logs. |
| + function maybeCompleteTest() { |
| + if (numConsoleLogsToWaitFor === 0 && frameStoppedLoading) |
| + completeTest(); |
| + } |
| + |
| + enableNetwork(); |
| +} |
| + |
| +InspectorTest.allowRequest = function(event) { |
| + var id = canonicalId(event.params.InterceptionId); |
| + log(id, "allowRequest " + id); |
| + InspectorTest.sendCommand("Network.continueRequest", { |
| + "interceptionId": event.params.InterceptionId |
| + }); |
| +} |
| + |
| +InspectorTest.modifyRequest = function(event, params) { |
| + var id = canonicalId(event.params.InterceptionId); |
| + var mods = []; |
| + for (property in params) { |
| + if (!params.hasOwnProperty(property)) |
| + continue; |
| + if (property === "url") { |
| + var newUrl = params["url"]; |
| + var filename = interceptionRequestParams[id].url; |
| + mods.push("url " + filename.split('/').pop() + " -> " + newUrl); |
| + var directoryPath = |
| + filename.substring(0, filename.lastIndexOf('/') + 1); |
| + params["url"] = directoryPath + newUrl; |
| + } else { |
| + mods.push(property + " " + |
| + JSON.stringify(interceptionRequestParams[id][property]) + |
| + " -> " + JSON.stringify(params[property])); |
| + } |
| + } |
| + |
| + log(id, "modifyRequest " + id + ": " + mods.join("; ")); |
| + params["interceptionId"] = event.params.InterceptionId; |
| + InspectorTest.sendCommand("Network.continueRequest", params); |
| +} |
| + |
| +InspectorTest.blockRequest = function(event, errorReason) { |
| + var id = canonicalId(event.params.InterceptionId); |
| + log(id, "blockRequest " + id + " " + errorReason); |
| + InspectorTest.sendCommand("Network.continueRequest", { |
| + "interceptionId": event.params.InterceptionId, |
| + "errorReason": errorReason |
| + }); |
| +} |
| + |
| +InspectorTest.mockResponse = function(event, rawResponse) { |
| + var id = canonicalId(event.params.InterceptionId); |
| + log(id, "mockResponse " + id); |
| + InspectorTest.sendCommand("Network.continueRequest", { |
| + "interceptionId": event.params.InterceptionId, |
| + "rawResponse": btoa(rawResponse) |
| + }); |
| +} |
| + |
| +} |