Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | // This file ensures that suspending a channel directly after opening it |
michael@0 | 2 | // suspends future notifications correctly. |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 5 | |
michael@0 | 6 | XPCOMUtils.defineLazyGetter(this, "URL", function() { |
michael@0 | 7 | return "http://localhost:" + httpserv.identity.primaryPort; |
michael@0 | 8 | }); |
michael@0 | 9 | |
michael@0 | 10 | const MIN_TIME_DIFFERENCE = 3000; |
michael@0 | 11 | const RESUME_DELAY = 5000; |
michael@0 | 12 | |
michael@0 | 13 | var listener = { |
michael@0 | 14 | _lastEvent: 0, |
michael@0 | 15 | _gotData: false, |
michael@0 | 16 | |
michael@0 | 17 | QueryInterface: function(iid) { |
michael@0 | 18 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 19 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 20 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 21 | return this; |
michael@0 | 22 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 23 | }, |
michael@0 | 24 | |
michael@0 | 25 | onStartRequest: function(request, ctx) { |
michael@0 | 26 | this._lastEvent = Date.now(); |
michael@0 | 27 | request.QueryInterface(Ci.nsIRequest); |
michael@0 | 28 | |
michael@0 | 29 | // Insert a delay between this and the next callback to ensure message buffering |
michael@0 | 30 | // works correctly |
michael@0 | 31 | request.suspend(); |
michael@0 | 32 | request.suspend(); |
michael@0 | 33 | do_timeout(RESUME_DELAY, function() request.resume()); |
michael@0 | 34 | do_timeout(RESUME_DELAY + 1000, function() request.resume()); |
michael@0 | 35 | }, |
michael@0 | 36 | |
michael@0 | 37 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 38 | do_check_true(Date.now() - this._lastEvent >= MIN_TIME_DIFFERENCE); |
michael@0 | 39 | read_stream(stream, count); |
michael@0 | 40 | |
michael@0 | 41 | // Ensure that suspending and resuming inside a callback works correctly |
michael@0 | 42 | request.suspend(); |
michael@0 | 43 | request.suspend(); |
michael@0 | 44 | request.resume(); |
michael@0 | 45 | request.resume(); |
michael@0 | 46 | |
michael@0 | 47 | this._gotData = true; |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | onStopRequest: function(request, ctx, status) { |
michael@0 | 51 | do_check_true(this._gotData); |
michael@0 | 52 | httpserv.stop(do_test_finished); |
michael@0 | 53 | } |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | function makeChan(url) { |
michael@0 | 57 | var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
michael@0 | 58 | var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 59 | return chan; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | var httpserv = null; |
michael@0 | 63 | |
michael@0 | 64 | function run_test() { |
michael@0 | 65 | httpserv = new HttpServer(); |
michael@0 | 66 | httpserv.registerPathHandler("/woo", data); |
michael@0 | 67 | httpserv.start(-1); |
michael@0 | 68 | |
michael@0 | 69 | var chan = makeChan(URL + "/woo"); |
michael@0 | 70 | chan.QueryInterface(Ci.nsIRequest); |
michael@0 | 71 | chan.asyncOpen(listener, null); |
michael@0 | 72 | |
michael@0 | 73 | do_test_pending(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function data(metadata, response) { |
michael@0 | 77 | let httpbody = "0123456789"; |
michael@0 | 78 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 79 | response.bodyOutputStream.write(httpbody, httpbody.length); |
michael@0 | 80 | } |