<!DOCTYPE html>
<html>
<head>
    <script src="/js-test-resources/js-test.js"></script>
    <script src="resources/util.js"></script>
    <title>Tests for Capped Cache Max-Age</title>
</head>
<body>
<script>
    description("Tests that cache max-age is capped for prevalent resources.");
    jsTestIsAsync = true;

    var loadedUrl;
    function firstFetch() {
        // This should create a capped cache entry.
        fetch("http://localhost:8000/resourceLoadStatistics/resources/cached-permanent-redirect.py",
            {
                cache: "reload",
                headers: {
                    "X-WebKit": "1234",
                }
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/resourceLoadStatistics/resources/echo-query.py?value=1234");
            secondFetch();
        }).catch(function(error) {
            testFailed(error.message);
            setEnableFeature(false, finishJSTest);
        });
    }

    function secondFetch() {
        // This should not trigger a successful cache hit and instead create another capped cache entry.
        fetch("http://localhost:8000/resourceLoadStatistics/resources/cached-permanent-redirect.py",
            {
                cache: "force-cache",
                headers: {
                    "X-WebKit": "4321",
                }
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/resourceLoadStatistics/resources/echo-query.py?value=4321");
            thirdFetch();
        }).catch(function(error) {
            testFailed(error.message);
            setEnableFeature(false, finishJSTest);
        });
    }

    function thirdFetch() {
        // This should not trigger a successful cache hit and instead create another capped cache entry.
        fetch("http://localhost:8000/resourceLoadStatistics/resources/cached-permanent-redirect.py",
            {
                cache: "force-cache",
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/resourceLoadStatistics/resources/echo-query.py?value=");
            fourthFetch();
        }).catch(function(error) {
            testFailed(error.message);
            setEnableFeature(false, finishJSTest);
        });
    }

    function fourthFetch() {
        // This should not trigger a successful cache hit but clear capping.
        fetch("http://localhost:8000/resourceLoadStatistics/resources/cached-permanent-redirect.py",
            {
                cache: "force-cache",
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/resourceLoadStatistics/resources/echo-query.py?value=");
            fifthFetch();
        }).catch(function(error) {
            testFailed(error.message);
            setEnableFeature(false, finishJSTest);
        });
    }

    function fifthFetch() {
        // This should trigger a regular, successful cache hit.
        fetch("http://localhost:8000/resourceLoadStatistics/resources/cached-permanent-redirect.py",
            {
                headers: {
                    "X-WebKit": "1234",
                }
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/resourceLoadStatistics/resources/echo-query.py?value=");
            setEnableFeature(false, finishJSTest);
        }).catch(function(error) {
            testFailed(error.message);
            setEnableFeature(false, finishJSTest);
        });
    }

    setEnableFeature(true, function() {
        if (testRunner.isStatisticsPrevalentResource("http://localhost"))
            testFailed("Localhost was classified as prevalent resource before the test started.");

        testRunner.setStatisticsCacheMaxAgeCap(0);
        testRunner.setStatisticsPrevalentResource("http://localhost", true, function() {
            testRunner.statisticsUpdateCookieBlocking(firstFetch);
        });
    });
</script>
</body>
</html>
