1 // This file tests channel event sinks (bug 315598 et al)
5 const { HttpServer
} = ChromeUtils
.importESModule(
6 "resource://testing-common/httpd.sys.mjs"
9 ChromeUtils
.defineLazyGetter(this, "URL", function () {
10 return "http://localhost:" + httpserv
.identity
.primaryPort
;
13 const sinkCID
= Components
.ID("{14aa4b81-e266-45cb-88f8-89595dece114}");
14 const sinkContract
= "@mozilla.org/network/unittest/channeleventsink;1";
16 const categoryName
= "net-channel-event-sinks";
19 * This object is both a factory and an nsIChannelEventSink implementation (so, it
20 * is de-facto a service). It's also an interface requestor that gives out
21 * itself when asked for nsIChannelEventSink.
24 QueryInterface
: ChromeUtils
.generateQI(["nsIFactory", "nsIChannelEventSink"]),
25 createInstance
: function eventsink_ci(iid
) {
26 return this.QueryInterface(iid
);
29 asyncOnChannelRedirect
: function eventsink_onredir() {
32 throw Components
.Exception("", Cr
.NS_BINDING_ABORTED
);
35 getInterface
: function eventsink_gi(iid
) {
36 if (iid
.equals(Ci
.nsIChannelEventSink
)) {
39 throw Components
.Exception("", Cr
.NS_ERROR_NO_INTERFACE
);
48 onStartRequest
: function test_onStartR(request
) {
50 // Commenting out this check pending resolution of bug 255119
51 //if (Components.isSuccessCode(request.status))
52 // do_throw("Channel should have a failure code!");
54 // The current URI must be the original URI, as all redirects have been
57 !(request
instanceof Ci
.nsIChannel
) ||
58 !request
.URI
.equals(request
.originalURI
)
64 request
.originalURI
.spec
+
69 if (request
instanceof Ci
.nsIHttpChannel
) {
70 // As we expect a blocked redirect, verify that we have a 3xx status
71 Assert
.equal(Math
.floor(request
.responseStatus
/ 100), 3);
72 Assert
.equal(request
.requestSucceeded
, false);
75 Assert
.equal(eventsink
.called
, this.expectSinkCall
);
77 do_throw("Unexpected exception: " + e
);
80 throw Components
.Exception("", Cr
.NS_ERROR_ABORT
);
83 onDataAvailable
: function test_ODA() {
84 do_throw("Should not get any data!");
87 onStopRequest
: function test_onStopR() {
88 if (this._iteration
<= 2) {
92 httpserv
.stop(do_test_finished
);
100 function makeChan(url
) {
101 return NetUtil
.newChannel({ uri
: url
, loadUsingSystemPrincipal
: true });
106 function run_test() {
107 httpserv
= new HttpServer();
108 httpserv
.registerPathHandler("/redirect", redirect
);
109 httpserv
.registerPathHandler("/redirectfile", redirectfile
);
112 Components
.manager
.nsIComponentRegistrar
.registerFactory(
114 "Unit test Event sink",
119 // Step 1: Set the callbacks on the listener itself
120 var chan
= makeChan(URL
+ "/redirect");
121 chan
.notificationCallbacks
= eventsink
;
123 chan
.asyncOpen(listener
);
128 function run_test_continued() {
129 eventsink
.called
= false;
132 if (listener
._iteration
== 1) {
133 // Step 2: Category entry
134 Services
.catMan
.addCategoryEntry(
141 chan
= makeChan(URL
+ "/redirect");
143 // Step 3: Global contract id
144 Services
.catMan
.deleteCategoryEntry(categoryName
, "unit test", false);
145 listener
.expectSinkCall
= false;
146 chan
= makeChan(URL
+ "/redirectfile");
149 listener
._iteration
++;
150 chan
.asyncOpen(listener
);
158 function redirect(metadata
, response
) {
159 response
.setStatusLine(metadata
.httpVersion
, 301, "Moved Permanently");
162 "http://localhost:" + metadata
.port
+ "/",
166 var body
= "Moved\n";
167 response
.bodyOutputStream
.write(body
, body
.length
);
171 function redirectfile(metadata
, response
) {
172 response
.setStatusLine(metadata
.httpVersion
, 301, "Moved Permanently");
173 response
.setHeader("Content-Type", "text/plain", false);
174 response
.setHeader("Location", "file:///etc/", false);
176 var body
= "Attempted to move to a file URI, but failed.\n";
177 response
.bodyOutputStream
.write(body
, body
.length
);