<!doctype html>
<html>
<head>
<title>Testing XMLHttpRequest.getReponseHeader behavior</title>
<script src="/js-test-resources/js-test-pre.js"></script>
<script type="text/javascript">
description("Test the required behavior of XMLHttpRequest.getAllResponseHeaders()");

window.jsTestIsAsync = true;

var xhr = new XMLHttpRequest();

var savedHeaders = null;

var headerValues;
function testGetAllResponseHeaders(xhr, expectEmpty) {
    shouldNotThrow("{state: " + xhr.readyState + "}; headerValues = xhr.getAllResponseHeaders();");
    if (expectEmpty && headerValues !== "")
        testFailed("Expected the empty string, got: '" + headerValues + "'");
    else
        testPassed("headerValues is " + (!expectEmpty ? "not " : "") + "the empty string");
    return headerValues;
}

var responseHeaders;
xhr.onreadystatechange = function() {
    var rState = this.readyState;
    responseHeaders = testGetAllResponseHeaders(this, rState <= XMLHttpRequest.OPENED);
    if (responseHeaders) {
        if (savedHeaders) {
            shouldBe("responseHeaders", "savedHeaders");
        } else {
            if (/^Set-Cookie:|^Set-Cookie2:/im.test(responseHeaders)) {
                testFailed("Did not expect to find a Set-Cookie{2} header, got: '" + responseHeaders + "'");
            } else {
                // Do not print list for automated tests to avoid false failures.
                if (self.testRunner)
                    testPassed("Header values appears to be conforming.");
                else
                    testPassed("Header values appears ok: " + JSON.stringify(headerValues));
            }
        }
        savedHeaders = responseHeaders;
    } else {
        if (rState > XMLHttpRequest.OPENED)
            testFailed("In ready state " + rState + ", unexpected empty value.");
        else if (responseHeaders !== "")
            testFailed("In ready state " + rState + ", expected the empty string, got: " + JSON.stringify(responseHeaders) + ".");
        else
            testPassed("getAllResponseHeaders() result is empty in ready state " + rState + ".");
    }

    if (rState == XMLHttpRequest.DONE)
        finishJSTest();
}

function runTest() {
    // Test for readyState = 0
    testGetAllResponseHeaders(xhr, true);
    shouldNotThrow('xhr.open("GET", "resources/1251.html", true);');
    // Test for readyState = 1
    testGetAllResponseHeaders(xhr, true);
    shouldNotThrow("xhr.send(null);");
}
runTest();
</script>
<script src="/js-test-resources/js-test-post.js"></script>
</head>
</html>
