<html>
<meta charset="utf-8">
<body>
<p>Tests for XMLHttpRequest authentication.
<script>
    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }
    
    var console_messages = document.createElement("ol");
    document.body.appendChild(console_messages);
    
    function log(message)
    {
        var item = document.createElement("li");
        item.appendChild(document.createTextNode(message));
        console_messages.appendChild(item);
    }

    function sendAndLogResponse(uid, req)
    {
        try {
            req.send("");
            log(uid + ': ' + req.responseText);
        } catch (e) {
            log(uid + ": req.send raised exception: " + e);
            log(uid + ": req.readyState: " + req.readyState);
            log(uid + ": req.status: " + req.status);
        }
    }

    // async
    var asyncStep = 1;

    req = new XMLHttpRequest;
    req.onreadystatechange = processStateChange;
    req.open("GET", "resources/basic-auth/basic-auth.py?uid=1", true, "1", "%a%");
    req.send("");

    function processStateChange() {
    
      if (req.readyState == 4){
        if (req.status == 200){
          if (asyncStep == 1) {
            asyncStep = 2;
            log('async: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=2", true, "2", "%40");
            req.send("");
          } else if (asyncStep == 2) {
            asyncStep = 3;
            log('async2: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=3", true, "3", "@");
            req.send("");
          } else if (asyncStep == 3) {
            asyncStep = 4;
            log('async3: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=4", true, "4", ":");
            req.send("");
          } else if (asyncStep == 4) {
            asyncStep = 5;
            log('async4: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=5", true, "5", " ");
            req.send("");
          } else if (asyncStep == 5) {
            asyncStep = 6;
            log('async5: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=6", true, "6", "%61");
            req.send("");
          } else if (asyncStep == 6) {
            asyncStep = 7;
            log('async6: ' + req.responseText);
            req.onreadystatechange = processStateChange;
            req.open("GET", "resources/basic-auth/basic-auth.py?uid=7", true, "7", "/@\\ \"#><?`");
            req.send("");
          } else if (asyncStep == 7) {
            log('async7: ' + req.responseText);
            if (window.testRunner)
              testRunner.notifyDone();
          }
        } else {
            log("async" + asyncStep + " failed with unexpected status: " + req.status + ". Response: '" + req.responseText + "'.");
            if (window.testRunner)
              testRunner.notifyDone();
        }
      }
    }
</script>
</body>
</html>
