<html>
<head>
<script>
var repeatCount = 0;

function sendRequest() {
    var firstReq = new XMLHttpRequest();
    firstReq.open("GET", "/resources/download-json-with-delay.py?iteration=100&delay=1");

    firstReq.onreadystatechange = function() {
        if (firstReq.readyState == firstReq.HEADERS_RECEIVED) {
            var secondReq = new XMLHttpRequest();
            secondReq.open("GET", "/resources/download-json-with-delay.py?iteration=10&delay=0");

            secondReq.onload = function() {
                if (repeatCount++ < 40)
                    sendRequest();
                else {
                    document.getElementById('result').innerText = "PASS";
                    testRunner.notifyDone();
                }
            }

            secondReq.onerror = function() {
                document.getElementById('result').innerText = "FAIL";
                testRunner.notifyDone();
            }

            firstReq.abort();
            secondReq.send();
        }
    }

    firstReq.send();
}

function runTest() {
    if (window.testRunner) {
        testRunner.waitUntilDone();
        testRunner.dumpAsText();
    }

    sendRequest();
}
</script>
</head>
<body onload="runTest()">
<p>This is a test for https://bugs.webkit.org/show_bug.cgi?id=191650.</p>
<p>In case that several requests are closed and created pretty fast, a result of old request sometimes goes to a wrong request.</p>
<p id="result">RUNNING</p>
</body>
</html>
