michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserver = new HttpServer(); michael@0: var testpath = "/simple"; michael@0: var httpbody = "0123456789"; michael@0: michael@0: function createXHR(async) michael@0: { michael@0: var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] michael@0: .createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("GET", "http://localhost:" + michael@0: httpserver.identity.primaryPort + testpath, async); michael@0: return xhr; michael@0: } michael@0: michael@0: function checkResults(xhr) michael@0: { michael@0: if (xhr.readyState != 4) michael@0: return false; michael@0: michael@0: do_check_eq(xhr.status, 200); michael@0: do_check_eq(xhr.responseText, httpbody); michael@0: michael@0: var root_node = xhr.responseXML.getElementsByTagName('root').item(0); michael@0: do_check_eq(root_node.firstChild.data, "0123456789"); michael@0: return true; michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: httpserver.registerPathHandler(testpath, serverHandler); michael@0: httpserver.start(-1); michael@0: michael@0: // Test sync XHR sending michael@0: var sync = createXHR(false); michael@0: sync.send(null); michael@0: checkResults(sync); michael@0: michael@0: // Test async XHR sending michael@0: let async = createXHR(true); michael@0: async.addEventListener("readystatechange", function(event) { michael@0: if (checkResults(async)) michael@0: httpserver.stop(do_test_finished); michael@0: }, false); michael@0: async.send(null); michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function serverHandler(metadata, response) michael@0: { michael@0: response.setHeader("Content-Type", "text/xml", false); michael@0: response.bodyOutputStream.write(httpbody, httpbody.length); michael@0: }