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..1e7d8d1943fd3e54e4c4bb2ce73a479648ac627f |
--- /dev/null |
+++ b/third_party/WebKit/LayoutTests/http/tests/inspector-protocol/network/interception-test.js |
@@ -0,0 +1,210 @@ |
+var initialize_InterceptionTest = function() { |
+ |
+var interceptionRequestParams = {}; |
+var requestIdToFilename = {}; |
+var loggedMessages = {}; |
+var requestIdToCanonicalInterceptionId = {}; |
+var idToCanoncalId = {}; |
+var nextId = 1; |
+ |
+function canonicalId(id) { |
+ if (!idToCanoncalId.hasOwnProperty(id)) |
+ idToCanoncalId[id] = "ID " + nextId++; |
+ return idToCanoncalId[id]; |
+} |
+ |
+function log(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. |
+ 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]); |
+ } |
+ } |
+ } |
+ InspectorTest.log(message); |
+ InspectorTest.completeTest(); |
+} |
+ |
+InspectorTest.startInterceptionTest = function(interceptedRequestDict, |
+ interceptedRedirectDict) { |
+ InspectorTest.eventHandler["Network.interceptedRequest"] = onInterceptedRequest; |
+ InspectorTest.eventHandler["Network.interceptedRedirect"] = onInterceptedRedirect; |
+ InspectorTest.eventHandler["Network.loadingFailed"] = onLoadingFailed; |
+ InspectorTest.eventHandler["Network.requestWillBeSent"] = onRequestWillBeSent; |
+ InspectorTest.eventHandler["Network.responseReceived"] = onResponseReceived; |
+ InspectorTest.eventHandler["Page.frameStoppedLoading"] = onStop; |
+ |
+ 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.evaluate", { "expression": "appendIframe()"}); |
+ } |
+ |
+ function onInterceptedRequest(event) |
+ { |
+ var filename = event.params.request.url.split('/').pop(); |
+ var id = canonicalId(event.params.id); |
+ if (event.params.request.headers.hasOwnProperty( |
+ "X-DevTools-Request-Id")) { |
+ requestIdToCanonicalInterceptionId[ |
+ event.params.request.headers["X-DevTools-Request-Id"]] = id; |
+ } |
+ interceptionRequestParams[id] = event.params.request; |
+ if (interceptedRequestDict.hasOwnProperty(filename)) { |
+ log(id, "Network.interceptedRequest " + id + " " + |
+ event.params.request.method + " " + filename); |
+ interceptedRequestDict[filename](event); |
+ } else { |
+ if (event.params.request.url.startsWith("blob:")) { |
+ // Silently allow blob: requests. |
+ InspectorTest.sendCommand( |
+ "Network.allowRequest", { "id": event.params.id }); |
+ return; |
+ } |
+ completeTest("FAILED: unexpected request interception " + |
+ JSON.stringify(event.params)); |
+ } |
+ } |
+ |
+ function onInterceptedRedirect(event) |
+ { |
+ var redirectFilename = event.params.redirectUrl.split('/').pop(); |
+ var id = canonicalId(event.params.id); |
+ if (interceptedRedirectDict.hasOwnProperty(redirectFilename)) { |
+ log(id, "Network.interceptedRedirect " + id + " " + |
+ event.params.responseStatusCode + " " + |
+ interceptionRequestParams[id].url.split('/').pop() + |
+ " -> " + redirectFilename); |
+ interceptedRedirectDict[redirectFilename](event); |
+ } else { |
+ completeTest("FAILED: unexpected redirect interception " + |
+ JSON.stringify(event.params)); |
+ } |
+ interceptionRequestParams[id].url = event.params.redirectUrl; |
+ } |
+ |
+ function onLoadingFailed(event) |
+ { |
+ var id = requestIdToCanonicalInterceptionId[event.params.requestId]; |
+ log(id, "Network.loadingFailed " + id + " " + |
+ requestIdToFilename[event.params.requestId] + " " + |
+ 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 = canonicalId(event.params.interceptId); |
+ log(id, "Network.responseReceived " + filename + " " + response.status + |
+ " " + response.mimeType + " interceptId = " + id); |
+ } |
+ |
+ function onStop() |
+ { |
+ completeTest("Page.frameStoppedLoading"); |
+ } |
+ |
+ enableNetwork(); |
+} |
+ |
+InspectorTest.allowRequest = function(event) { |
+ var id = canonicalId(event.params.id); |
+ log(id, "Network.allowRequest " + id); |
+ InspectorTest.sendCommand( |
+ "Network.allowRequest", { "id": event.params.id }); |
+} |
+ |
+InspectorTest.modifyRequest = function(event, params) { |
+ var id = canonicalId(event.params.id); |
+ 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, "Network.modifyRequest " + id + ": " + mods.join("; ")); |
+ params["id"] = event.params.id; |
+ InspectorTest.sendCommand("Network.modifyRequest", params); |
+} |
+ |
+InspectorTest.blockRequest = function(event, errorReason) { |
+ var id = canonicalId(event.params.id); |
+ log(id, "Network.blockRequest " + id + " " + errorReason); |
+ InspectorTest.sendCommand("Network.blockRequest", { |
+ "id": event.params.id, "errorReason": errorReason}); |
+} |
+ |
+InspectorTest.mockResponse = function(event, rawResponse) { |
+ var id = canonicalId(event.params.id); |
+ log(id, "Network.mockResponse " + id); |
+ InspectorTest.sendCommand("Network.mockResponse", { |
+ "id": event.params.id, |
+ "rawResponse": rawResponse}); |
+} |
+ |
+} |