<!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.getResponseHeader()");

window.jsTestIsAsync = true;

var savedHeader = null;
var headerName = "Content-Type";
var nullTests = [ "SeT-COoKie",
                  "sEt-coOkIE2",
                  "xxx-mytest-headerabc",
                  null,
                  "Content-Type:" ];

var xhr = new XMLHttpRequest();

var headerValue;
function testGetResponseHeader(xhr, headerName, expectNull) {
    shouldNotThrow("{state: " + xhr.readyState + "}; headerValue = xhr.getResponseHeader('" + headerName + "');");
    if (expectNull)
        shouldBeNull("headerValue");
    else
        shouldBeNonNull("headerValue");

    return headerValue;
}

var result;
xhr.onreadystatechange = function() {
    var rState = this.readyState;
    result = testGetResponseHeader(this, headerName, rState <= XMLHttpRequest.OPENED);
    if (result) {
        if (savedHeader)
            shouldBe("result", "savedHeader");

        savedHeader = result;

        for (var i = 0; i < nullTests.length; ++i)
            testGetResponseHeader(this, nullTests[i], true);
    } else {
        if (rState > XMLHttpRequest.OPENED)
            testFailed("In ready state " + rState + ", unexpected null value for '" + headerName + "'.");
        else if (result !== null)
            testFailed("In ready state " + rState + ", expected null for '" + headerName + ", got: " + JSON.stringify(result) + ".");
        else
            testPassed("Header '" + headerName + "' is null in ready state " + rState + ".");
    }

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

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