chromium/third_party/blink/web_tests/http/tests/xmlhttprequest/zero-length-response-sync.html

<html>
<head>
<title>Test XMLHttpRequest zero-length response handling (sync)</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<body>
<p>Test for <a href="http://bugs.webkit.org/show_bug.cgi?id=5924">bug 5924</a>
- zero-length responses to XMLHTTPRequest mishandled.</p>
<script>

    if (window.testRunner)
        testRunner.dumpAsText();

    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);
    }
    
    function get(url, async)
    {
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        } else {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (ex) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        
        log("after creation: " + stateName(req.readyState));
        
        try {
            log("ResponseText: " + prettyPrintText(req.responseText));
        } catch (ex) {
            log("Exception getting responseText: " + ex.message);
        }

        if (async)
            req.onreadystatechange = processStateChange;
        
        log("after setting onreadystatechange: " + stateName(req.readyState));

        req.open('GET', url, async);
        log("after open(): " + stateName(req.readyState));
        try {
            req.overrideMimeType('text/xml');
            log("after overrideMimeType(): " + stateName(req.readyState));
        } catch (ex) {
            log("overrideMimeType(): " + ex);
        }

        req.send(null);
        log("after send(): " + stateName(req.readyState));

        if (!async && req.status != 200)
            throw ("HTTP request failed, status: " + req.status);
        
        return req;
    }
    
    function stateName(val) {
      if (req.readyState == 0)
          return "Uninitialized";
      else if (req.readyState == 1)
          return "Open";
      else if (req.readyState == 2)
          return "Sent";
      else if (req.readyState == 3)
          return "Receiving";
      else if (req.readyState == 4)
          return "Loaded";
      else
        return "???";
    }
    
    function prettyPrintText(text) {
        if (text == null)
            return text;
        else
            return '"' + text + '"';
    }

    function dumpResponse() {
       try { log ("  Status: " + req.status); } catch (ex) { log("  Exception getting status: " + ex.message); }
       try { log ("  StatusText: " + prettyPrintText(req.statusText)); } catch (ex) { log("  Exception getting StatusText: " + ex.message); }
       try { log ("  ResponseText: " + prettyPrintText(req.responseText)); } catch (ex) { log("  Exception getting ResponseText: " + ex.message); }
       try { log ("  ResponseXML: " + req.responseXML); } catch (ex) { log("  Exception getting ResponseXML: " + ex.message); }
       try { log ("  ResponseXML serialized: " + (req.responseXML ? prettyPrintText(window.XMLSerializer ? (new XMLSerializer()).serializeToString(req.responseXML) : req.responseXML.xml) : "n/a")); } catch (ex) { log("  Exception serializing ResponseXML: " + ex.message); }
    }

    function processStateChange() {
      log("onreadystatechange: " + stateName(req.readyState));
      dumpResponse();
    }
    
    get('resources/zero-length.xml', false);
    dumpResponse();
</script>
</body>
</html>