michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpserver.identity.primaryPort; michael@0: }); michael@0: michael@0: var httpserver = new HttpServer(); michael@0: var testpath = "/simple"; michael@0: var httpbody = "0123456789"; michael@0: michael@0: function syncXHR() michael@0: { michael@0: var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] michael@0: .createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("GET", URL + testpath, false); michael@0: xhr.send(null); michael@0: } michael@0: michael@0: const MAX_TESTS = 2; michael@0: michael@0: var listener = { michael@0: _done_onStart: false, michael@0: _done_onData: false, michael@0: _test: 0, michael@0: michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIStreamListener) || michael@0: iid.equals(Components.interfaces.nsIRequestObserver) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: onStartRequest: function(request, ctx) { michael@0: switch(this._test) { michael@0: case 0: michael@0: request.suspend(); michael@0: syncXHR(); michael@0: request.resume(); michael@0: break; michael@0: case 1: michael@0: request.suspend(); michael@0: syncXHR(); michael@0: do_execute_soon(function() request.resume()); michael@0: break; michael@0: case 2: michael@0: do_execute_soon(function() request.suspend()); michael@0: do_execute_soon(function() request.resume()); michael@0: syncXHR(); michael@0: break; michael@0: } michael@0: michael@0: this._done_onStart = true; michael@0: }, michael@0: michael@0: onDataAvailable: function(request, context, stream, offset, count) { michael@0: do_check_true(this._done_onStart); michael@0: read_stream(stream, count); michael@0: this._done_onData = true; michael@0: }, michael@0: michael@0: onStopRequest: function(request, ctx, status) { michael@0: do_check_true(this._done_onData); michael@0: this._reset(); michael@0: if (this._test <= MAX_TESTS) michael@0: next_test(); michael@0: else michael@0: httpserver.stop(do_test_finished); michael@0: }, michael@0: michael@0: _reset: function() { michael@0: this._done_onStart = false; michael@0: this._done_onData = false; michael@0: this._test++; michael@0: } michael@0: }; michael@0: michael@0: function makeChan(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); michael@0: return chan; michael@0: } michael@0: michael@0: function next_test() michael@0: { michael@0: var chan = makeChan(URL + testpath); michael@0: chan.QueryInterface(Ci.nsIRequest); michael@0: chan.asyncOpen(listener, null); 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: next_test(); michael@0: 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: }