gecko-dev/netwerk/test/unit/test_event_sink.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

196 строки
4.8 KiB
JavaScript
Исходник Обычный вид История

// This file tests channel event sinks (bug 315598 et al)
"use strict";
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 21:18:31 +03:00
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
XPCOMUtils.defineLazyGetter(this, "URL", function() {
return "http://localhost:" + httpserv.identity.primaryPort;
});
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 = {
QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIChannelEventSink"]),
createInstance: function eventsink_ci(outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return this.QueryInterface(iid);
},
lockFactory: function eventsink_lockf(lock) {
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
},
asyncOnChannelRedirect: function eventsink_onredir(
oldChan,
newChan,
flags,
callback
) {
// veto
this.called = true;
throw Cr.NS_BINDING_ABORTED;
},
getInterface: function eventsink_gi(iid) {
if (iid.equals(Ci.nsIChannelEventSink)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
},
called: false,
};
var listener = {
expectSinkCall: true,
onStartRequest: function test_onStartR(request) {
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
if (
!(request instanceof Ci.nsIChannel) ||
!request.URI.equals(request.originalURI)
) {
do_throw(
"Wrong URI: Is <" +
request.URI.spec +
">, should be <" +
request.originalURI.spec +
">"
);
}
if (request instanceof Ci.nsIHttpChannel) {
// As we expect a blocked redirect, verify that we have a 3xx status
Assert.equal(Math.floor(request.responseStatus / 100), 3);
Assert.equal(request.requestSucceeded, false);
}
Assert.equal(eventsink.called, this.expectSinkCall);
} catch (e) {
do_throw("Unexpected exception: " + e);
}
throw Cr.NS_ERROR_ABORT;
},
onDataAvailable: function test_ODA() {
do_throw("Should not get any data!");
},
onStopRequest: function test_onStopR(request, status) {
if (this._iteration <= 2) {
run_test_continued();
} else {
do_test_pending();
httpserv.stop(do_test_finished);
}
do_test_finished();
},
_iteration: 1,
};
function makeChan(url) {
return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}
var httpserv = null;
function run_test() {
httpserv = new HttpServer();
httpserv.registerPathHandler("/redirect", redirect);
httpserv.registerPathHandler("/redirectfile", redirectfile);
httpserv.start(-1);
Components.manager.nsIComponentRegistrar.registerFactory(
sinkCID,
"Unit test Event sink",
sinkContract,
eventsink
);
// Step 1: Set the callbacks on the listener itself
var chan = makeChan(URL + "/redirect");
chan.notificationCallbacks = eventsink;
chan.asyncOpen(listener);
do_test_pending();
}
function run_test_continued() {
eventsink.called = false;
var catMan = Cc["@mozilla.org/categorymanager;1"].getService(
Ci.nsICategoryManager
);
var chan;
if (listener._iteration == 1) {
// Step 2: Category entry
catMan.nsICategoryManager.addCategoryEntry(
categoryName,
"unit test",
sinkContract,
false,
true
);
chan = makeChan(URL + "/redirect");
} else {
// Step 3: Global contract id
catMan.nsICategoryManager.deleteCategoryEntry(
categoryName,
"unit test",
false
);
listener.expectSinkCall = false;
chan = makeChan(URL + "/redirectfile");
}
listener._iteration++;
chan.asyncOpen(listener);
do_test_pending();
}
// 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);
}