<!DOCTYPE html>
<html>
<head>
<script src="../../../../js-test-resources/js-test.js"></script>
<script src="../../../cookies/resources/cookie-utilities.js"></script>
<script>
window.jsTestIsAsync = true;

var cookieValue;

function clearCookie()
{
    document.cookie = "foo=0; Max-Age=0; path=/"; // The key "foo" must match the key used in the WebSocket Set-Cookie header.
}

function echoCookie()
{
    return document.cookie;
}

async function testSameOriginCookie()
{
    clearCookie();
    document.cookie = "foo=should_be_overwritten_by_websocket_set_cookie; path=/";
    await setCookieUsingWebSocketFromHost("127.0.0.1");
    cookieValue = echoCookie();
    shouldBeEqualToString("cookieValue", "foo=bar");
}

async function testCrossOriginCookie()
{
    clearCookie();
    await setCookieUsingWebSocketFromHost("localhost");
    cookieValue = echoCookie();
    shouldBeEmptyString("cookieValue");
}

async function runTests()
{
    switch (document.location.hash) {
        case "":
            await testSameOriginCookie();
            // Test that a third-party without pre-existing cookies does not write first-party cookies.
            await testCrossOriginCookie();
            // Navigate to localhost to set first-party cookie 'setAsFirstParty'.
            document.location.href = "http://localhost:8000/websocket/tests/hybi/websocket-cookie-overwrite-behavior.html#setCookieAsFirstParty";
            break;
        case "#setCookieAsFirstParty":
            await setCookie("setAsFirstParty", "value");
            // Navigate back to 127.0.0.1 to test third-party cookie.
            document.location.href = "http://127.0.0.1:8000/websocket/tests/hybi/websocket-cookie-overwrite-behavior.html#didSetCookieAsFirstParty";
            break;
        case "#didSetCookieAsFirstParty":
            testRunner.dumpChildFramesAsText();
            // Test that a third-party with a pre-existing cookie does not write first-party cookies.
            debug("Setting third-party cookie 'foo' through cross-origin WebSocket handshake and checking that it doesn't write first-party cookies.");
            await testCrossOriginCookie();
            let iframeElement = document.createElement("iframe");
            iframeElement.src = "http://localhost:8000/cookies/resources/echo-cookies.py";
            iframeElement.onload = finishJSTest;
            debug("<br>Opening localhost third-party iframe to check its cookies.");
            document.body.appendChild(iframeElement);
            break;
    }
}
</script>
</head>
<body>
<script>
description("Tests WebSocket Set-Cookie overwriting behavior with respect to a document cookie.");
runTests();
</script>
</body>
</html>
