<html>
<head>
   <title>Testing cache reloading strategy according redirections with different cache state</title>
    <script src="/js-test-resources/testharness.js"></script>
    <script src="/js-test-resources/testharnessreport.js"></script>
</head>
<body>
<iframe id="iframe1"></iframe>
<iframe id="iframe2"></iframe>
<div id="log"></div>
<script>

var test = async_test();
test.step(function() {

var counter = 0;
var resource = "No";
var baseURL = "resources/cache-control-redirect.py?code=302&url=cacheable-random-text.py&random_id=1";
var testCases = [
{name : "Should reuse resource (cacheable redirection)", expected : true, url : baseURL + "&cache_control=public, max-age=1000", reloadDelay : 0},
{name : "Should reload resource (no-store redirection)", expected : false, url: baseURL + "&cache_control=no-store&max_age=1000", reloadDelay : 0},
{name : "Should reload resource (no-cache redirection)", expected : false, url: baseURL + "&cache_control=no-cache&max_age=1000", reloadDelay : 0},
{name : "Should reload resource (must-revalidate redirection)", expected : false, url: baseURL + "&cache_control=must-revalidate&max_age=1000", reloadDelay : 0},
{name : "Should reload resource (expired redirection)", expected : false,  url: baseURL + "&cache_control=public, max-age=1", reloadDelay : 1000},
{name : "Should reload resource (implicit expiration)", expected : false,  url: baseURL + "", reloadDelay : 100},
{name : "Should reuse resource (301 cacheable redirection)", expected : true, url : baseURL + "&code=301&cache_control=public, max-age=1000", reloadDelay : 0},
{name : "Should reuse resource (301 no-store redirection)", expected : false, url : baseURL + "&code=301&cache_control=no-store", reloadDelay : 0},
{name : "Should reuse resource (301 implicit expiration)", expected : true,  url: baseURL + "&code=301", reloadDelay : 100},
];

function loadResource()
{
    setFrame1Loader();
	document.getElementById('iframe1').src = testCases[counter].url;
}

function reloadResource()
{
    setFrame2Loader();
	document.getElementById('iframe2').src  = testCases[counter].url;
}

function setFrame1Loader() 
{
    document.getElementById('iframe2').onload  = null;
    document.getElementById('iframe1').onload  = test.step_func(function()
    {
        resource = document.getElementById('iframe1').contentDocument.body.innerHTML;
        setTimeout(test.step_func(reloadResource), testCases[counter].reloadDelay);
    });
}

function setFrame2Loader() 
{
    document.getElementById('iframe1').onload  = null;
    document.getElementById('iframe2').onload  = test.step_func(function ()
    {
        var testResult = resource == document.getElementById('iframe2').contentDocument.body.innerHTML;
        assert_true(testResult == testCases[counter].expected, testCases[counter].name);

        document.getElementById('iframe1').contentDocument.body.innerHTML = "frame 1";
        document.getElementById('iframe2').contentDocument.body.innerHTML = "frame 2";
        if (++counter < testCases.length)
            loadResource();
        else
            test.done();
    });
}

document.onload = setTimeout(loadResource, 1);

});


</script>
</body>
</html>
