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