<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../resources/inspector-test.js"></script>
<script src="resources/resource-size-test.js"></script>
<script>
function triggerNetworkLoadWithRequestData() {
    let url = "resources/data.json?" + Math.random();
    let headerString512Bytes = "12345678";
    for (i = 0; i < 6; ++i)
        headerString512Bytes += headerString512Bytes;

    fetch(url, {
        method: "POST",
        headers: {
            "X-Header-1": headerString512Bytes, // 512 + 10 + ":" + "\r\n" whitespace
            "X-Header-2": headerString512Bytes, // 512 + 10 + ":" + "\r\n" whitespace
        },
        body: headerString512Bytes + headerString512Bytes + "abc" // (512 + 512 + 3) = 1027.
    }).then((response) => {
        return response.text();
    }).then(() => {
        TestPage.dispatchEventToFrontend("LoadComplete");
    });
}

function test()
{
    let suite = InspectorTest.createAsyncSuite("Resource.Size.Network");

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.text",
        description: "Sizes of a plain text resource",
        url: "resources/lorem.txt?" + Math.random(),
        statusCode: 200,
        compressed: false,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.text.empty",
        description: "Sizes of an empty text resource",
        url: "resources/empty.txt?" + Math.random(),
        statusCode: 200,
        compressed: false,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.text.gzipped",
        description: "Sizes of a gzipped text resource",
        url: "resources/gzipped-lorem.py?" + Math.random(),
        statusCode: 200,
        compressed: true,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.text.gzipped.no-content-length",
        description: "Sizes of a gzipped text resource without a content-length attribute.",
        url: "resources/gzipped-lorem-no-content-length.py?" + Math.random(),
        statusCode: 200,
        compressed: true,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.image",
        description: "Sizes of an image resource",
        url: "/resources/square100.png?" + Math.random(),
        statusCode: 200,
        compressed: false,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.404",
        description: "Sizes of a 404 response.",
        url: "resources/404.py?" + Math.random(),
        statusCode: 404,
        compressed: false,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
    });

    addResourceSizeTest(suite, {
        name: "Resource.Size.Network.requestData",
        description: "Sizes of a resource with request data",
        statusCode: 200,
        compressed: false,
        responseSource: WI.Resource.ResponseSource.Network,
        headers: true,
        resourceLoader() {
            InspectorTest.evaluateInPage(`triggerNetworkLoadWithRequestData()`);
            return Promise.all([
                WI.Frame.awaitEvent(WI.Frame.Event.ResourceWasAdded),
                InspectorTest.awaitEvent("LoadComplete"),
            ]).then(([resourceWasAddedEvent, loadCompleteEvent]) => {
                return resourceWasAddedEvent.data.resource;
            });
        },
        extraChecks(resource) {
            let minimumRequestHeadersSize = 512 + 10 + 512 + 10 + 16;
            InspectorTest.expectGreaterThan(resource.requestHeadersTransferSize, minimumRequestHeadersSize, `requestHeadersTransferSize should be > ${minimumRequestHeadersSize} bytes.`);
        }
    });

    suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Test for Resource size values (transfer size, decoded size, header size, request and response) for resources served over the network.</p>
</body>
</html>
