michael@0: // This file ensures that suspending a channel directly after opening it michael@0: // suspends future notifications correctly. michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpserv.identity.primaryPort; michael@0: }); michael@0: michael@0: const MIN_TIME_DIFFERENCE = 3000; michael@0: const RESUME_DELAY = 5000; michael@0: michael@0: var listener = { michael@0: _lastEvent: 0, michael@0: _gotData: false, 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: this._lastEvent = Date.now(); michael@0: request.QueryInterface(Ci.nsIRequest); michael@0: michael@0: // Insert a delay between this and the next callback to ensure message buffering michael@0: // works correctly michael@0: request.suspend(); michael@0: request.suspend(); michael@0: do_timeout(RESUME_DELAY, function() request.resume()); michael@0: do_timeout(RESUME_DELAY + 1000, function() request.resume()); michael@0: }, michael@0: michael@0: onDataAvailable: function(request, context, stream, offset, count) { michael@0: do_check_true(Date.now() - this._lastEvent >= MIN_TIME_DIFFERENCE); michael@0: read_stream(stream, count); michael@0: michael@0: // Ensure that suspending and resuming inside a callback works correctly michael@0: request.suspend(); michael@0: request.suspend(); michael@0: request.resume(); michael@0: request.resume(); michael@0: michael@0: this._gotData = true; michael@0: }, michael@0: michael@0: onStopRequest: function(request, ctx, status) { michael@0: do_check_true(this._gotData); michael@0: httpserv.stop(do_test_finished); 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: var httpserv = null; michael@0: michael@0: function run_test() { michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerPathHandler("/woo", data); michael@0: httpserv.start(-1); michael@0: michael@0: var chan = makeChan(URL + "/woo"); michael@0: chan.QueryInterface(Ci.nsIRequest); michael@0: chan.asyncOpen(listener, null); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function data(metadata, response) { michael@0: let httpbody = "0123456789"; michael@0: response.setHeader("Content-Type", "text/plain", false); michael@0: response.bodyOutputStream.write(httpbody, httpbody.length); michael@0: }