<html>
<head>
<script type="text/javascript">

function log(message)
{
    document.getElementById('console').appendChild(document.createTextNode(message + "\n"));
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
    testRunner.setAlwaysAcceptCookies(true);
}

var cookieSet = false;

window.onmessage = function(evt)
{
    if (evt.data != "done") {
        alert("Unexpected message: " + evt.data);
        return;
    }

    if (!cookieSet) {
        cookieSet = true;
        runTest();
    }
}

function startTest() {
    // Set a cookie for localhost:8000.
    window.frames[0].postMessage("sendXHR setFooCookie", "*");
}

function stopTest() {
    // Clean up all cookies for localhost:8000.
    window.frames[0].postMessage("resetCookiesAndNotifyDone", "*");
    if (window.testRunner)
        testRunner.notifyDone();
}

function runTest() {
    try {
        var xhr = new XMLHttpRequest;
        xhr.open("PUT", "http://localhost:8000/xmlhttprequest/resources/basic-auth/access-control-auth-basic.py?uid=fooUser", true, "fooUser", "barPass");
        xhr.withCredentials = true;
        xhr.onerror = function (e) {
            log("FAILED: received error");
            stopTest();
        };
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                log((xhr.status == 401) ? "PASSED" : "FAILED: credential send!");
                stopTest();
            }
        };
        xhr.send();
    } catch(e) {
        log("FAILED: got exception " + e.message);
    }
}

</script>
</head>
<body onload="startTest();">
<p>Test case for bug <a href="https://bugs.webkit.org/show_bug.cgi?id=37781">37781</a>: [XHR] Cross-Origin asynchronous request with credential raises NETWORK_ERR</p>
<pre id='console'></pre>

<iframe id='testFrame' src="http://localhost:8000/cookies/resources/third-party-cookie-relaxing-iframe.html"></iframe>

</body>
</html>
