2006-02-22 17:07:21 +03:00
|
|
|
// This file tests channel event sinks (bug 315598 et al)
|
|
|
|
|
2020-04-07 19:00:03 +03:00
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
|
2006-12-15 05:13:54 +03:00
|
|
|
|
2013-07-19 21:19:28 +04:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "URL", function() {
|
|
|
|
return "http://localhost:" + httpserv.identity.primaryPort;
|
|
|
|
});
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
const sinkCID = Components.ID("{14aa4b81-e266-45cb-88f8-89595dece114}");
|
|
|
|
const sinkContract = "@mozilla.org/network/unittest/channeleventsink;1";
|
|
|
|
|
|
|
|
const categoryName = "net-channel-event-sinks";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This object is both a factory and an nsIChannelEventSink implementation (so, it
|
|
|
|
* is de-facto a service). It's also an interface requestor that gives out
|
|
|
|
* itself when asked for nsIChannelEventSink.
|
|
|
|
*/
|
|
|
|
var eventsink = {
|
2019-06-21 23:13:40 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIChannelEventSink"]),
|
2006-02-22 17:07:21 +03:00
|
|
|
createInstance: function eventsink_ci(outer, iid) {
|
|
|
|
if (outer) {
|
2018-02-28 20:51:33 +03:00
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
2019-07-05 11:55:23 +03:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
return this.QueryInterface(iid);
|
|
|
|
},
|
|
|
|
lockFactory: function eventsink_lockf(lock) {
|
2018-02-28 20:51:33 +03:00
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
2006-02-22 17:07:21 +03:00
|
|
|
},
|
|
|
|
|
2010-08-05 06:15:55 +04:00
|
|
|
asyncOnChannelRedirect: function eventsink_onredir(
|
|
|
|
oldChan,
|
|
|
|
newChan,
|
|
|
|
flags,
|
|
|
|
callback
|
|
|
|
) {
|
2006-02-22 17:07:21 +03:00
|
|
|
// veto
|
|
|
|
this.called = true;
|
2018-02-28 20:51:33 +03:00
|
|
|
throw Cr.NS_BINDING_ABORTED;
|
2006-02-22 17:07:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInterface: function eventsink_gi(iid) {
|
2018-02-28 20:51:33 +03:00
|
|
|
if (iid.equals(Ci.nsIChannelEventSink)) {
|
2006-02-22 17:07:21 +03:00
|
|
|
return this;
|
2019-07-05 11:55:23 +03:00
|
|
|
}
|
2018-02-28 20:51:33 +03:00
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
2006-02-22 17:07:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
called: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
var listener = {
|
|
|
|
expectSinkCall: true,
|
|
|
|
|
2019-02-28 02:41:54 +03:00
|
|
|
onStartRequest: function test_onStartR(request) {
|
2006-02-22 17:07:21 +03:00
|
|
|
try {
|
|
|
|
// Commenting out this check pending resolution of bug 255119
|
|
|
|
//if (Components.isSuccessCode(request.status))
|
|
|
|
// do_throw("Channel should have a failure code!");
|
|
|
|
|
|
|
|
// The current URI must be the original URI, as all redirects have been
|
|
|
|
// cancelled
|
2018-02-28 20:51:33 +03:00
|
|
|
if (
|
|
|
|
!(request instanceof Ci.nsIChannel) ||
|
2006-02-22 17:07:21 +03:00
|
|
|
!request.URI.equals(request.originalURI)
|
2019-07-05 11:55:23 +03:00
|
|
|
) {
|
2006-02-22 17:07:21 +03:00
|
|
|
do_throw(
|
|
|
|
"Wrong URI: Is <" +
|
|
|
|
request.URI.spec +
|
|
|
|
">, should be <" +
|
|
|
|
request.originalURI.spec +
|
|
|
|
">"
|
|
|
|
);
|
2019-07-05 11:55:23 +03:00
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
|
2018-02-28 20:51:33 +03:00
|
|
|
if (request instanceof Ci.nsIHttpChannel) {
|
2006-02-22 17:07:21 +03:00
|
|
|
// As we expect a blocked redirect, verify that we have a 3xx status
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(Math.floor(request.responseStatus / 100), 3);
|
|
|
|
Assert.equal(request.requestSucceeded, false);
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(eventsink.called, this.expectSinkCall);
|
2006-02-22 17:07:21 +03:00
|
|
|
} catch (e) {
|
|
|
|
do_throw("Unexpected exception: " + e);
|
|
|
|
}
|
|
|
|
|
2018-02-28 20:51:33 +03:00
|
|
|
throw Cr.NS_ERROR_ABORT;
|
2006-02-22 17:07:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
onDataAvailable: function test_ODA() {
|
|
|
|
do_throw("Should not get any data!");
|
|
|
|
},
|
|
|
|
|
2019-02-28 02:41:54 +03:00
|
|
|
onStopRequest: function test_onStopR(request, status) {
|
2009-04-16 00:19:35 +04:00
|
|
|
if (this._iteration <= 2) {
|
2006-02-22 17:07:21 +03:00
|
|
|
run_test_continued();
|
2009-04-16 00:19:35 +04:00
|
|
|
} else {
|
|
|
|
do_test_pending();
|
|
|
|
httpserv.stop(do_test_finished);
|
|
|
|
}
|
2006-02-22 17:07:21 +03:00
|
|
|
do_test_finished();
|
|
|
|
},
|
|
|
|
|
|
|
|
_iteration: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
function makeChan(url) {
|
2015-12-22 00:06:46 +03:00
|
|
|
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
|
2006-08-02 06:02:37 +04:00
|
|
|
var httpserv = null;
|
|
|
|
|
2006-02-22 17:07:21 +03:00
|
|
|
function run_test() {
|
2012-08-14 18:06:04 +04:00
|
|
|
httpserv = new HttpServer();
|
2006-12-15 05:13:54 +03:00
|
|
|
httpserv.registerPathHandler("/redirect", redirect);
|
|
|
|
httpserv.registerPathHandler("/redirectfile", redirectfile);
|
2013-07-19 21:19:28 +04:00
|
|
|
httpserv.start(-1);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
Components.manager.nsIComponentRegistrar.registerFactory(
|
|
|
|
sinkCID,
|
|
|
|
"Unit test Event sink",
|
|
|
|
sinkContract,
|
|
|
|
eventsink
|
|
|
|
);
|
|
|
|
|
|
|
|
// Step 1: Set the callbacks on the listener itself
|
2013-07-19 21:19:28 +04:00
|
|
|
var chan = makeChan(URL + "/redirect");
|
2006-02-22 17:07:21 +03:00
|
|
|
chan.notificationCallbacks = eventsink;
|
|
|
|
|
2019-02-12 19:08:25 +03:00
|
|
|
chan.asyncOpen(listener);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_test_continued() {
|
|
|
|
eventsink.called = false;
|
|
|
|
|
2018-02-28 20:51:33 +03:00
|
|
|
var catMan = Cc["@mozilla.org/categorymanager;1"].getService(
|
|
|
|
Ci.nsICategoryManager
|
|
|
|
);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
var chan;
|
|
|
|
if (listener._iteration == 1) {
|
|
|
|
// Step 2: Category entry
|
|
|
|
catMan.nsICategoryManager.addCategoryEntry(
|
|
|
|
categoryName,
|
|
|
|
"unit test",
|
|
|
|
sinkContract,
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
2013-07-19 21:19:28 +04:00
|
|
|
chan = makeChan(URL + "/redirect");
|
2006-02-22 17:07:21 +03:00
|
|
|
} else {
|
|
|
|
// Step 3: Global contract id
|
|
|
|
catMan.nsICategoryManager.deleteCategoryEntry(
|
|
|
|
categoryName,
|
|
|
|
"unit test",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
listener.expectSinkCall = false;
|
2013-07-19 21:19:28 +04:00
|
|
|
chan = makeChan(URL + "/redirectfile");
|
2006-02-22 17:07:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
listener._iteration++;
|
2019-02-12 19:08:25 +03:00
|
|
|
chan.asyncOpen(listener);
|
2006-02-22 17:07:21 +03:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
}
|
|
|
|
|
2006-12-15 05:13:54 +03:00
|
|
|
// PATHS
|
|
|
|
|
|
|
|
// /redirect
|
|
|
|
function redirect(metadata, response) {
|
|
|
|
response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
|
|
|
|
response.setHeader(
|
|
|
|
"Location",
|
|
|
|
"http://localhost:" + metadata.port + "/",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
var body = "Moved\n";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /redirectfile
|
|
|
|
function redirectfile(metadata, response) {
|
|
|
|
response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
|
|
|
|
response.setHeader("Content-Type", "text/plain", false);
|
|
|
|
response.setHeader("Location", "file:///etc/", false);
|
|
|
|
|
|
|
|
var body = "Attempted to move to a file URI, but failed.\n";
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
}
|