<!DOCTYPE html><!-- webkit-test-runner [ isAppInitiated=false ] -->
<html>
<head>
    <script src="/js-test-resources/js-test.js"></script>
</head>
<body onload="fetchAndRedirectWithCORSPreflightCheck()">
<script>
    description("Tests that CORS preflight checks for HTTP redirects are marked non app initiated.");
    jsTestIsAsync = true;

    function log(message)
    {
        document.getElementById('console').appendChild(document.createTextNode(message + '\n'));
    }
    
    function askForAttribution() {
        var didLoadAppBoundRequest = testRunner.didLoadAppInitiatedRequest();
        var didLoadNonAppBoundRequest = testRunner.didLoadNonAppInitiatedRequest();

        if (didLoadAppBoundRequest) {
            log("FAIL did load app initiated request");
            finishJSTest();
            return;
        }

        if (!didLoadNonAppBoundRequest) {
            log("FAIL did not load non app initiated request");
            finishJSTest();
            return;
        }

        log("PASS successfully loaded only non app initiated requests");

        finishJSTest();
    }
    
    var loadedUrl;
    function fetchAndRedirectWithCORSPreflightCheck() {
        fetch("http://localhost:8000/app-privacy-report/resources/redirect-with-cors-preflight-check.py",
            {
                headers: {
                    "X-WebKit": "1234",
                }
            }
        ).then(function(response) {
            loadedUrl = response.url;
            shouldBeEqualToString("loadedUrl", "http://localhost:8000/app-privacy-report/resources/cors-preflight.py?value=1234");
            askForAttribution();
        }).catch(function(error) {
            testFailed(error.message);
            finishJSTest();
        });
    }
</script>
</body>
</html>
