<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../resources/inspector-test.js"></script>
<script src="../../../cookies/resources/cookie-utilities.js"></script>
<script>
function test()
{
    function HARJSONFilter(key, value) {
        // Filter out the creator.version / browser.version but leave a top level version.
        if ((key === "creator" || key === "browser") && value.version) {
            value.version = "<filtered>";
            return value;
        }

        // Cache may or may not have been used so remove optional fields which may or may not be set.
        if (key === "connection")
            return undefined;
        if (key === "serverIPAddress")
            return undefined;
        if (key === "_transferSize")
            return undefined;
        if (key === "_fetchType")
            return undefined;

        // If cached the protocol might not be available.
        if (key === "httpVersion")
            return "<filtered>";

        // Headers include dynamic data.
        if (key === "headers")
            return "<filtered>";

        // Dates would change between test runs.
        if (key.endsWith("DateTime"))
            return "<filtered>";

        // If any files are modified, we'd need to update the test without this filter.
        if (key == "size")
            return "<filtered>";

        // Size data may or may not be available, but could change based on headers.
        if (key.endsWith("Size"))
            return "<filtered>";

        // Reduce the file contents.
        if (key === "text") {
            InspectorTest.assert(value.length > 0, "The file should have contents.");
            return "<filtered>";
        }

        // Sort cookies by name to make cookie order deterministic between test runs. We assume
        // that cookies have names that consist of only ASCII characters.
        if (key === "cookies") {
            value.sort((a, b) => { return a.name === b.name ? 0 : (a.name < b.name ? -1 : 1); });
            return value;
        }

        // Since cache may or may not be used, timing data may be variable.
        // NOTE: SSL should always be -1 for this test case.
        if (key === "time")
            return "<filtered>";
        if (key === "timings") {
            value.blocked = "<filtered>";
            value.dns = "<filtered>";
            value.connect = "<filtered>";
            value.send = "<filtered>";
            value.wait = "<filtered>";
            value.receive = "<filtered>";
        }

        // PageTimings can be variable.
        if (key === "onContentLoad" || key === "onLoad")
            return "<filtered>";

        return value;
    }

    let suite = InspectorTest.createAsyncSuite("HAR.Page");

    suite.addTestCase({
        name: "HAR.Basic.Page",
        description: "Should be able to generate a HAR with all of this test page's resources.",
        async test() {
            await NetworkAgent.setResourceCachingDisabled(true);

            await Promise.all([
                InspectorTest.awaitEvent("LoadComplete"),
                InspectorTest.reloadPage({ignoreCache: true}),
            ]);

            await Promise.all([
                InspectorTest.awaitEvent("SetupComplete"),
                InspectorTest.evaluateInPage(`setup()`),
            ]);

            let resources = [WI.networkManager.mainFrame.mainResource, ...WI.networkManager.mainFrame.resourceCollection];
            let har = await WI.HARBuilder.buildArchive(resources);
            InspectorTest.json(har, HARJSONFilter);

            await Promise.all([
                InspectorTest.awaitEvent("CleanComplete"),
                InspectorTest.evaluateInPage(`cleanup()`),
            ]);
        }
    });

    suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>HAR Page Test.</p>
<script>
async function setup()
{
    await resetCookies();
    await setCookie("simple", "simple");
    await setCookie("path", "path", {"path": "/A/B/C"}); // This cookie will not be sent in subsequent requests from this page.

    // Secure cookies will only be sent in subsequent requests from this page if the page was served over HTTPS.
    await setCookie("secure", "secure", {"secure": null});
    await setCookie("secure-and-http-only", "secure-and-http-only", {"secure": null, "HttpOnly": null});

    await setCookie("with-expiration", "with-expiration", {"Expires": new Date("01/04/5000").toGMTString()});
    await setCookie("http-only", "http-only", {"HttpOnly": null});
    await setCookie("same-site-strict", "same-site-strict", {"SameSite": "Strict", "path": "/"});
    await setCookie("same-site-ignored-because-no-value", "same-site-ignored-because-no-value", {"SameSite": null, "path": "/"});
    await setCookie("same-site-ignored-because-unknown-value", "same-site-ignored-because-unknown-value", {"SameSite": "invalid", "path": "/"});
    await setCookie("same-site-lax", "same-site-lax", {"SameSite": "Lax", "path": "/"});
    TestPage.dispatchEventToFrontend("SetupComplete");
}

async function cleanup()
{
    await resetCookies();
    TestPage.dispatchEventToFrontend("CleanComplete");
}

window.addEventListener("load", () => {
    TestPage.dispatchEventToFrontend("LoadComplete");
});
</script>
</body>
</html>
