1 // This file tests nsIContentSniffer, introduced in bug 324985
5 const { HttpServer } = ChromeUtils.importESModule(
6 "resource://testing-common/httpd.sys.mjs"
9 const unknownType = "application/x-unknown-content-type";
10 const sniffedType = "application/x-sniffed";
12 const snifferCID = Components.ID("{4c93d2db-8a56-48d7-b261-9cf2a8d998eb}");
13 const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1";
14 const categoryName = "net-content-sniffers";
16 var sniffing_enabled = true;
18 var isNosniff = false;
21 * This object is both a factory and an nsIContentSniffer implementation (so, it
22 * is de-facto a service)
25 QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIContentSniffer"]),
26 createInstance: function sniffer_ci(iid) {
27 return this.QueryInterface(iid);
30 getMIMETypeFromContent(request, data, length) {
36 onStartRequest: function test_onStartR(request) {
38 var chan = request.QueryInterface(Ci.nsIChannel);
39 if (chan.contentType == unknownType) {
40 do_throw("Type should not be unknown!");
43 if (chan.contentType == sniffedType) {
44 do_throw("Sniffer called for X-Content-Type-Options:nosniff");
48 this._iteration > 2 &&
49 chan.contentType != sniffedType
59 } else if (!sniffing_enabled && chan.contentType == sniffedType) {
61 "Sniffing not enabled but sniffer called for " + chan.URI.spec
65 do_throw("Unexpected exception: " + e);
68 throw Components.Exception("", Cr.NS_ERROR_ABORT);
71 onDataAvailable: function test_ODA() {
72 throw Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
75 onStopRequest: function test_onStopR(request, status) {
76 run_test_iteration(this._iteration);
83 function makeChan(url) {
84 var chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
85 if (sniffing_enabled) {
86 chan.loadFlags |= Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
96 httpserv = new HttpServer();
97 httpserv.registerPathHandler("/nosniff", nosniffHandler);
101 // NOTE: First URL here runs without our content sniffer
102 "data:" + unknownType + ", Some text",
103 "data:" + unknownType + ", Text", // Make sure sniffing works even if we
104 // used the unknown content sniffer too
105 "data:text/plain, Some more text",
106 "http://localhost:" + httpserv.identity.primaryPort,
107 "http://localhost:" + httpserv.identity.primaryPort + "/nosniff",
110 Components.manager.nsIComponentRegistrar.registerFactory(
112 "Unit test content sniffer",
117 run_test_iteration(1);
120 function nosniffHandler(request, response) {
121 response.setHeader("X-Content-Type-Options", "nosniff");
124 function run_test_iteration(index) {
125 if (index > urls.length) {
126 if (sniffing_enabled) {
127 sniffing_enabled = false;
128 index = listener._iteration = 1;
131 httpserv.stop(do_test_finished);
132 return; // we're done
136 if (sniffing_enabled && index == 2) {
137 // Register our sniffer only here
138 // This also makes sure that dynamic registration is working
139 var catMan = Services.catMan;
140 catMan.nsICategoryManager.addCategoryEntry(
147 } else if (sniffing_enabled && index == 5) {
151 var chan = makeChan(urls[index - 1]);
153 listener._iteration++;
154 chan.asyncOpen(listener);