content/base/test/test_XHR.html
author L. David Baron <dbaron@dbaron.org>
Wed, 06 May 2009 13:46:04 -0700
changeset 28048 8f7b73d88f6bbedec99c6c8ef48b243ab922d05c
parent 13557 2050cf0b5d3338f194557f9070e3e50580496811
child 50604 b0a91fbddbff33e29e0c2e9448b64239433dcbce
permissions -rw-r--r--
Switch HTML mochitests from using MochiKit.js to packed.js. (Bug 490955) r=sayrer

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for XMLHttpRequest</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

var path = "/tests/content/base/test/";

var passFiles = [['file_XHR_pass1.xml', 'GET'],
                 ['file_XHR_pass2.txt', 'GET'],
                 ['file_XHR_pass3.txt', 'GET'],
                 ];

var failFiles = [['//example.com' + path + 'file_XHR_pass1.xml', 'GET'],
                 ['ftp://localhost' + path + 'file_XHR_pass1.xml', 'GET'],
                 ['file_XHR_fail1.txt', 'GET'],
                 ];

for (i = 0; i < passFiles.length; ++i) {
  xhr = new XMLHttpRequest();
  xhr.open(passFiles[i][1], passFiles[i][0], false); 
  xhr.send(null);
  is(xhr.status, 200, "wrong status");
  if (xhr.responseXML) {
    is((new XMLSerializer()).serializeToString(xhr.responseXML.documentElement),
       "<res>hello</res>",
       "wrong response");
  }
  else {
    is(xhr.responseText, "hello pass\n", "wrong response");
  }
}

for (i = 0; i < failFiles.length; ++i) {
  xhr = new XMLHttpRequest();
  didthrow = false;
  try {
    xhr.open(failFiles[i][1], failFiles[i][0], false); 
    xhr.send(null);
  }
  catch (e) {
    didthrow = true;
  }
  if (!didthrow) {
    is(xhr.status, 301, "wrong status");
    is(xhr.responseText, "redirect file\n", "wrong response");
  }
  else {
    ok(1, "should have thrown or given incorrect result");
  }
}
</script>
</pre>
</body>
</html>