﻿<html>
<body>
<p>Tests for <a href="https://bugs.webkit.org/show_bug.cgi?id=8210"> bug 8210</a> - 
Conditional XMLHttpRequest gets should pass 304 responses unchanged.</p>
<script>
    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }
    
    var console_messages = document.createElement("ul");
    document.body.appendChild(console_messages);
    
    function log(message)
    {
        var item = document.createElement("li");
        item.appendChild(document.createTextNode(message));
        console_messages.appendChild(item);
    }

    // sync
    req = new XMLHttpRequest;
    req.open("GET", "resources/reply.xml", false);
    log("Fetching the resource once to cache the response...");
    req.send(null);
    lastModified = req.getResponseHeader("Last-Modified");
    eTag = req.getResponseHeader("ETag");
    
    req.open("GET", "resources/reply.xml", false);
    req.send(null);
    if (req.responseText == '<?xml version="1.0" encoding="windows-1251"?>\n<node>Проверка</node>\n')
        log("Automatically cached response body is correct; status = " + req.status);
    else
        log("Automatically cached response body is different from the expected one; status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Modified-Since", lastModified);
    req.send(null);
    log("If-Modified-Since override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-MODIFIED-Since", lastModified);
    req.send(null);
    log("If-MODIFIED-Since override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Modified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
    req.send(null);
    log("If-Modified-Since override (modified): status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Unmodified-Since", lastModified);
    req.send(null);
    log("If-Unmodified-Since override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Unmodified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
    req.send(null);
    log("If-Unmodified-Since override (modified): status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Match", eTag);
    req.send(null);
    log("If-Match override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Match", "xyzzy");
    req.send(null);
    log("If-Match override (none match): status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-None-Match", eTag);
    req.send(null);
    log("If-None-Match override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-None-Match", "xyzzy");
    req.send(null);
    log("If-None-Match override (none match): status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Range", eTag);
    req.setRequestHeader("Range", "bytes=52-60");
    req.send(null);
    log("If-Range override: status = " + req.status);

    req.open("GET", "resources/reply.xml", false);
    req.setRequestHeader("If-Range", "xyzzy");
    req.setRequestHeader("Range", "bytes=52-60");
    req.send(null);
    log("If-Range override (none match): status = " + req.status);

    req.open("GET", "resources/304.pl", false);
    req.setRequestHeader("If-Modified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
    req.send(null);
    log("If-Modified-Since override (uncached response): status = " + req.status);

    // async
    var asyncStep = 1;

    req.onreadystatechange = processStateChange;
    req.open("GET", "resources/reply.xml", true);
    req.send(null);

    function processStateChange() {
    
      if (req.readyState == 4){
          if (asyncStep == 1) {
            asyncStep = 2;
            if (req.responseText == '<?xml version="1.0" encoding="windows-1251"?>\n<node>Проверка</node>\n')
                log("async: Automatically cached response body is correct; status = " + req.status);
            else
                log("async: Automatically cached response body is different from the expected one; status = " + req.status + "; req.responseText='" + req.responseText + "' (expected '<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<node>Проверка</node>\n')");

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Modified-Since", lastModified);
            req.send(null);
          } else if (asyncStep == 2) {
            asyncStep = 3;
            log("async: If-Modified-Since override: status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Modified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
            req.send(null);
          } else if (asyncStep == 3) {
            asyncStep = 4;
            log("async: If-Modified-Since override (modified): status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Unmodified-Since", lastModified);
            req.send(null);
          } else if (asyncStep == 4) {
            asyncStep = 5;
            log("async: If-Unmodified-Since override: status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Unmodified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
            req.send(null);
          } else if (asyncStep == 5) {
            asyncStep = 6;
            log("async: If-Unmodified-Since override (modified): status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Match", eTag);
            req.send(null);
          } else if (asyncStep == 6) {
            asyncStep = 7;
            log("async: If-Match override: status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Match", "xyzzy");
            req.send(null);
          } else if (asyncStep == 7) {
            asyncStep = 8;
            log("async: If-Match override (none match): status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-None-Match", eTag);
            req.send(null);
          } else if (asyncStep == 8) {
            asyncStep = 9;
            log("async: If-None-Match override: status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-None-Match", "xyzzy");
            req.send(null);
          } else if (asyncStep == 9) {
            asyncStep = 10;
            log("async: If-None-Match override (none match): status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Range", eTag);
            req.setRequestHeader("Range", "bytes=52-59");
            req.send(null);
          } else if (asyncStep == 10) {
            asyncStep = 11;
            log("async: If-Range override: status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/reply.xml", true);
            req.setRequestHeader("If-Range", "xyzzy");
            req.setRequestHeader("Range", "bytes=52-59");
            req.send(null);
          } else if (asyncStep == 11) {
            asyncStep = 12;
            log("async: If-Range override (none match): status = " + req.status);

            req = new XMLHttpRequest();
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/304.pl", true);
            req.setRequestHeader("If-Modified-Since", "Tue, 01 Jan 1980 01:00:00 GMT");
            req.send(null);
          } else if (asyncStep == 12) {
            log("async: If-Modified-Since override (uncached response): status = " + req.status);
            if (window.testRunner)
              testRunner.notifyDone();
          }
        }
    }
</script>
</body>
</html>
