Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_event_sink.js
blobd89c905cd2b1e7902a6c1d75572eb862abaf8c4a
1 // This file tests channel event sinks (bug 315598 et al)
3 "use strict";
5 const { HttpServer } = ChromeUtils.importESModule(
6 "resource://testing-common/httpd.sys.mjs"
7 );
9 ChromeUtils.defineLazyGetter(this, "URL", function () {
10 return "http://localhost:" + httpserv.identity.primaryPort;
11 });
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";
18 /**
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.
23 var eventsink = {
24 QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIChannelEventSink"]),
25 createInstance: function eventsink_ci(iid) {
26 return this.QueryInterface(iid);
29 asyncOnChannelRedirect: function eventsink_onredir() {
30 // veto
31 this.called = true;
32 throw Components.Exception("", Cr.NS_BINDING_ABORTED);
35 getInterface: function eventsink_gi(iid) {
36 if (iid.equals(Ci.nsIChannelEventSink)) {
37 return this;
39 throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
42 called: false,
45 var listener = {
46 expectSinkCall: true,
48 onStartRequest: function test_onStartR(request) {
49 try {
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
55 // cancelled
56 if (
57 !(request instanceof Ci.nsIChannel) ||
58 !request.URI.equals(request.originalURI)
59 ) {
60 do_throw(
61 "Wrong URI: Is <" +
62 request.URI.spec +
63 ">, should be <" +
64 request.originalURI.spec +
65 ">"
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);
76 } catch (e) {
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) {
89 run_test_continued();
90 } else {
91 do_test_pending();
92 httpserv.stop(do_test_finished);
94 do_test_finished();
97 _iteration: 1,
100 function makeChan(url) {
101 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
104 var httpserv = null;
106 function run_test() {
107 httpserv = new HttpServer();
108 httpserv.registerPathHandler("/redirect", redirect);
109 httpserv.registerPathHandler("/redirectfile", redirectfile);
110 httpserv.start(-1);
112 Components.manager.nsIComponentRegistrar.registerFactory(
113 sinkCID,
114 "Unit test Event sink",
115 sinkContract,
116 eventsink
119 // Step 1: Set the callbacks on the listener itself
120 var chan = makeChan(URL + "/redirect");
121 chan.notificationCallbacks = eventsink;
123 chan.asyncOpen(listener);
125 do_test_pending();
128 function run_test_continued() {
129 eventsink.called = false;
131 var chan;
132 if (listener._iteration == 1) {
133 // Step 2: Category entry
134 Services.catMan.addCategoryEntry(
135 categoryName,
136 "unit test",
137 sinkContract,
138 false,
139 true
141 chan = makeChan(URL + "/redirect");
142 } else {
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);
152 do_test_pending();
155 // PATHS
157 // /redirect
158 function redirect(metadata, response) {
159 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
160 response.setHeader(
161 "Location",
162 "http://localhost:" + metadata.port + "/",
163 false
166 var body = "Moved\n";
167 response.bodyOutputStream.write(body, body.length);
170 // /redirectfile
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);