|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 var httpserver = null; |
|
4 var simplePath = "/simple"; |
|
5 var normalPath = "/normal"; |
|
6 var httpbody = "<html></html>"; |
|
7 |
|
8 XPCOMUtils.defineLazyGetter(this, "uri1", function() { |
|
9 return "http://localhost:" + httpserver.identity.primaryPort + simplePath; |
|
10 }); |
|
11 |
|
12 XPCOMUtils.defineLazyGetter(this, "uri2", function() { |
|
13 return "http://localhost:" + httpserver.identity.primaryPort + normalPath; |
|
14 }); |
|
15 |
|
16 function make_channel(url) { |
|
17 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
18 getService(Ci.nsIIOService); |
|
19 return ios.newChannel(url, "", null); |
|
20 } |
|
21 |
|
22 var listener_proto = { |
|
23 QueryInterface: function(iid) { |
|
24 if (iid.equals(Components.interfaces.nsIStreamListener) || |
|
25 iid.equals(Components.interfaces.nsIRequestObserver) || |
|
26 iid.equals(Components.interfaces.nsISupports)) |
|
27 return this; |
|
28 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
29 }, |
|
30 |
|
31 onStartRequest: function(request, context) { |
|
32 do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, |
|
33 this.contentType); |
|
34 request.cancel(Cr.NS_BINDING_ABORTED); |
|
35 }, |
|
36 |
|
37 onDataAvailable: function(request, context, stream, offset, count) { |
|
38 do_throw("Unexpected onDataAvailable"); |
|
39 }, |
|
40 |
|
41 onStopRequest: function(request, context, status) { |
|
42 do_check_eq(status, Cr.NS_BINDING_ABORTED); |
|
43 this.termination_func(); |
|
44 } |
|
45 }; |
|
46 |
|
47 function listener(contentType, termination_func) { |
|
48 this.contentType = contentType; |
|
49 this.termination_func = termination_func; |
|
50 } |
|
51 listener.prototype = listener_proto; |
|
52 |
|
53 function run_test() |
|
54 { |
|
55 httpserver = new HttpServer(); |
|
56 httpserver.registerPathHandler(simplePath, simpleHandler); |
|
57 httpserver.registerPathHandler(normalPath, normalHandler); |
|
58 httpserver.start(-1); |
|
59 |
|
60 var channel = make_channel(uri1); |
|
61 channel.asyncOpen(new listener("text/plain", function() { |
|
62 run_test2(); |
|
63 }), null); |
|
64 |
|
65 do_test_pending(); |
|
66 } |
|
67 |
|
68 function run_test2() |
|
69 { |
|
70 var channel = make_channel(uri2); |
|
71 channel.asyncOpen(new listener("text/html", function() { |
|
72 httpserver.stop(do_test_finished); |
|
73 }), null); |
|
74 } |
|
75 |
|
76 function simpleHandler(metadata, response) |
|
77 { |
|
78 response.seizePower(); |
|
79 response.bodyOutputStream.write(httpbody, httpbody.length); |
|
80 response.finish(); |
|
81 } |
|
82 |
|
83 function normalHandler(metadata, response) |
|
84 { |
|
85 response.bodyOutputStream.write(httpbody, httpbody.length); |
|
86 response.finish(); |
|
87 } |