2012-08-14 07:06:04 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
2010-04-21 02:02:05 -07:00
|
|
|
|
2012-08-14 07:06:04 -07:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
2010-04-21 02:02:05 -07:00
|
|
|
|
2012-08-14 07:06:04 -07:00
|
|
|
var httpserver = new HttpServer();
|
2010-04-21 02:02:05 -07:00
|
|
|
var testpath = "/simple";
|
|
|
|
var httpbody = "<?xml version='1.0' ?><root>0123456789</root>";
|
|
|
|
|
2010-08-05 21:59:37 -07:00
|
|
|
function createXHR(async)
|
|
|
|
{
|
|
|
|
var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
|
|
|
|
.createInstance(Ci.nsIXMLHttpRequest);
|
2013-07-19 10:19:28 -07:00
|
|
|
xhr.open("GET", "http://localhost:" +
|
|
|
|
httpserver.identity.primaryPort + testpath, async);
|
2010-08-05 21:59:37 -07:00
|
|
|
return xhr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkResults(xhr)
|
|
|
|
{
|
|
|
|
if (xhr.readyState != 4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
do_check_eq(xhr.status, 200);
|
|
|
|
do_check_eq(xhr.responseText, httpbody);
|
|
|
|
|
|
|
|
var root_node = xhr.responseXML.getElementsByTagName('root').item(0);
|
|
|
|
do_check_eq(root_node.firstChild.data, "0123456789");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-21 02:02:05 -07:00
|
|
|
function run_test()
|
|
|
|
{
|
|
|
|
httpserver.registerPathHandler(testpath, serverHandler);
|
2013-07-19 10:19:28 -07:00
|
|
|
httpserver.start(-1);
|
2010-04-21 02:02:05 -07:00
|
|
|
|
2010-08-05 21:59:37 -07:00
|
|
|
// Test sync XHR sending
|
|
|
|
var sync = createXHR(false);
|
|
|
|
sync.send(null);
|
|
|
|
checkResults(sync);
|
2010-04-21 02:02:05 -07:00
|
|
|
|
2010-08-05 21:59:37 -07:00
|
|
|
// Test async XHR sending
|
|
|
|
let async = createXHR(true);
|
2011-09-29 09:06:36 -07:00
|
|
|
async.addEventListener("readystatechange", function(event) {
|
2010-08-05 21:59:37 -07:00
|
|
|
if (checkResults(async))
|
2010-04-21 02:02:05 -07:00
|
|
|
httpserver.stop(do_test_finished);
|
2011-09-29 09:06:36 -07:00
|
|
|
}, false);
|
2010-08-05 21:59:37 -07:00
|
|
|
async.send(null);
|
2010-04-21 02:02:05 -07:00
|
|
|
do_test_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
function serverHandler(metadata, response)
|
|
|
|
{
|
|
|
|
response.setHeader("Content-Type", "text/xml", false);
|
|
|
|
response.bodyOutputStream.write(httpbody, httpbody.length);
|
|
|
|
}
|