|
1 // This file ensures that suspending a channel directly after opening it |
|
2 // suspends future notifications correctly. |
|
3 |
|
4 Cu.import("resource://testing-common/httpd.js"); |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "URL", function() { |
|
7 return "http://localhost:" + httpserv.identity.primaryPort; |
|
8 }); |
|
9 |
|
10 const MIN_TIME_DIFFERENCE = 3000; |
|
11 const RESUME_DELAY = 5000; |
|
12 |
|
13 var listener = { |
|
14 _lastEvent: 0, |
|
15 _gotData: false, |
|
16 |
|
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 }, |
|
24 |
|
25 onStartRequest: function(request, ctx) { |
|
26 this._lastEvent = Date.now(); |
|
27 request.QueryInterface(Ci.nsIRequest); |
|
28 |
|
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 }, |
|
36 |
|
37 onDataAvailable: function(request, context, stream, offset, count) { |
|
38 do_check_true(Date.now() - this._lastEvent >= MIN_TIME_DIFFERENCE); |
|
39 read_stream(stream, count); |
|
40 |
|
41 // Ensure that suspending and resuming inside a callback works correctly |
|
42 request.suspend(); |
|
43 request.suspend(); |
|
44 request.resume(); |
|
45 request.resume(); |
|
46 |
|
47 this._gotData = true; |
|
48 }, |
|
49 |
|
50 onStopRequest: function(request, ctx, status) { |
|
51 do_check_true(this._gotData); |
|
52 httpserv.stop(do_test_finished); |
|
53 } |
|
54 }; |
|
55 |
|
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 } |
|
61 |
|
62 var httpserv = null; |
|
63 |
|
64 function run_test() { |
|
65 httpserv = new HttpServer(); |
|
66 httpserv.registerPathHandler("/woo", data); |
|
67 httpserv.start(-1); |
|
68 |
|
69 var chan = makeChan(URL + "/woo"); |
|
70 chan.QueryInterface(Ci.nsIRequest); |
|
71 chan.asyncOpen(listener, null); |
|
72 |
|
73 do_test_pending(); |
|
74 } |
|
75 |
|
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 } |