blob: 4684dd3d998224698b37a8bbf2fae683b6e02cef [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../resources/inspector-test.js"></script>
<script>
function triggerOverrideLoad(urlSuffix) {
let url = "http://127.0.0.1:8000/inspector/network/resources/override.txt";
if (urlSuffix)
url += urlSuffix;
fetch(url).then(() => {
TestPage.dispatchEventToFrontend("LoadComplete");
});
}
function test()
{
let suite = InspectorTest.createAsyncSuite("Network.interceptRequestWithResponse");
async function logResource(resource) {
let responseSource = resource.responseSource === WI.Resource.ResponseSource.InspectorOverride ? String(resource.responseSource) : "<not-InspectorOverride>";
InspectorTest.log(` URL: ${resource.url}`);
InspectorTest.log(` MIME Type: ${resource.mimeType}`);
InspectorTest.log(` Status: ${resource.statusCode} ${resource.statusText}`);
InspectorTest.log(` Response Source: ${responseSource}`);
InspectorTest.log(` Response Headers:`);
let keys = Object.keys(resource.responseHeaders);
keys.sort();
for (let name of keys) {
let value = resource.responseHeaders[name];
if (!name.startsWith("X-") && !name.startsWith("Content-"))
value = "<filtered>";
InspectorTest.log(` ${name}: ${value}`);
}
let {rawContent, rawBase64Encoded} = await resource.requestContent();
InspectorTest.log(` Content: ${rawBase64Encoded ? "[base64] " : ""}${rawContent}`);
}
function addTestCase({name, description, expression, overrides}) {
suite.addTestCase({
name,
description,
async test() {
let localResourceOverrides = overrides.map((override) => {
InspectorTest.log("Creating Local Resource Override for: " + override.url);
let localResourceOverride = WI.LocalResourceOverride.create(override.url, WI.LocalResourceOverride.InterceptType.ResponseSkippingNetwork, override);
WI.networkManager.addLocalResourceOverride(localResourceOverride);
return localResourceOverride;
});
InspectorTest.log("Triggering load...");
let [resourceWasAddedEvent, responseReceivedEvent, loadCompleteEvent] = await Promise.all([
WI.Frame.awaitEvent(WI.Frame.Event.ResourceWasAdded),
WI.Resource.awaitEvent(WI.Resource.Event.ResponseReceived),
InspectorTest.awaitEvent("LoadComplete"),
InspectorTest.evaluateInPage(expression),
]);
InspectorTest.log("Resource Loaded:");
let resource = resourceWasAddedEvent.data.resource;
await logResource(resource);
for (let localResourceOverride of localResourceOverrides)
WI.networkManager.removeLocalResourceOverride(localResourceOverride);
}
});
}
addTestCase({
name: "Network.interceptRequestWithResponse.Text",
description: "Intercept request with text content.",
expression: `triggerOverrideLoad()`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "text/plain",
responseContent: `PASS - OVERRIDDEN TEXT`,
responseBase64Encoded: false,
responseStatusCode: 987,
responseStatusText: "Override Status Text",
responseHeaders: {
"X-Override-Header-1": "Override-Header-Value-1",
"X-Override-Header-2": "Override-Header-Value-2",
},
}],
});
addTestCase({
name: "Network.interceptRequestWithResponse.JavaScript",
description: "Intercept request with javascript content.",
expression: `triggerOverrideLoad()`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "application/javascript",
responseContent: `/* PASS */ (function() { /* OVERRIDDEN */ })();`,
responseBase64Encoded: false,
responseStatusCode: 200,
responseStatusText: "Super OK",
responseHeaders: {
"X-Custom-Header": "Header value",
},
}],
});
addTestCase({
name: "Network.interceptRequestWithResponse.Image",
description: "Intercept request with image content.",
expression: `triggerOverrideLoad()`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "image/png",
responseContent: btoa("<data>"),
responseBase64Encoded: true,
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {},
}],
});
addTestCase({
name: "Network.interceptRequestWithResponse.URL.QueryString",
description: "Test overrides with different query strings.",
expression: `triggerOverrideLoad("?s=2")`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "text/plain",
responseCase64Encoded: false,
responseBontent: "PASS",
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {"X-Expected": "PASS"},
}],
});
addTestCase({
name: "Network.interceptRequestWithResponse.URL.Fragment",
description: "Test override for a load with a fragment.",
expression: `triggerOverrideLoad("#frag")`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "text/plain",
responseContent: "PASS",
responseBase64Encoded: false,
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {"X-Expected": "PASS"},
}],
});
addTestCase({
name: "Network.interceptRequestWithResponse.URL.CaseSensitive",
description: "Test override for a load with a fragment.",
expression: `triggerOverrideLoad("?CaSe=SeNsItIvE")`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt?case=sensitive",
responseMIMEType: "text/plain",
responseContent: "PASS",
responseBase64Encoded: false,
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {"X-Expected": "PASS"},
isCaseSensitive: false,
}]
});
addTestCase({
name: "Network.interceptRequestWithResponse.URL.IsRegex",
description: "Test override for a load with a fragment.",
expression: `triggerOverrideLoad("?t=123456789")`,
overrides: [{
url: "\\/override\\.txt\\?t=\\d+",
responseMIMEType: "text/plain",
responseContent: "PASS",
responseBase64Encoded: false,
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {"X-Expected": "PASS"},
isRegex: true,
}]
});
addTestCase({
name: "Network.interceptRequestWithResponse.URL.IsCaseSensitiveRegex",
description: "Test override for a load with a fragment.",
expression: `triggerOverrideLoad("?t=123456789")`,
overrides: [{
url: "\\/OvErRiDe\\.TxT\\?t=\\d+",
responseMIMEType: "text/plain",
responseContent: "PASS",
responseBase64Encoded: false,
responseStatusCode: 200,
responseStatusText: "OK",
responseHeaders: {"X-Expected": "PASS"},
isCaseSensitive: false,
isRegex: true,
}]
});
addTestCase({
name: "Network.interceptRequestWithResponse.404",
description: "Test for a 404 override.",
expression: `triggerOverrideLoad()`,
overrides: [{
url: "http://127.0.0.1:8000/inspector/network/resources/override.txt",
responseMIMEType: "text/plain",
responseContent: "PASS",
responseBase64Encoded: false,
responseStatusCode: 404,
responseStatusText: "Not Found",
responseHeaders: {"X-Expected": "PASS"},
}],
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Test request interception with response.</p>
<script src="resources/override.js"></script>
</body>
</html>