michael@0: // This file tests nsIContentSniffer, introduced in bug 324985 michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: const unknownType = "application/x-unknown-content-type"; michael@0: const sniffedType = "application/x-sniffed"; michael@0: michael@0: const snifferCID = Components.ID("{4c93d2db-8a56-48d7-b261-9cf2a8d998eb}"); michael@0: const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1"; michael@0: const categoryName = "net-content-sniffers"; michael@0: michael@0: var sniffing_enabled = true; michael@0: michael@0: /** michael@0: * This object is both a factory and an nsIContentSniffer implementation (so, it michael@0: * is de-facto a service) michael@0: */ michael@0: var sniffer = { michael@0: QueryInterface: function sniffer_qi(iid) { michael@0: if (iid.equals(Components.interfaces.nsISupports) || michael@0: iid.equals(Components.interfaces.nsIFactory) || michael@0: iid.equals(Components.interfaces.nsIContentSniffer)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: createInstance: function sniffer_ci(outer, iid) { michael@0: if (outer) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return this.QueryInterface(iid); michael@0: }, michael@0: lockFactory: function sniffer_lockf(lock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: michael@0: getMIMETypeFromContent: function (request, data, length) { michael@0: return sniffedType; michael@0: } michael@0: }; michael@0: michael@0: var listener = { michael@0: onStartRequest: function test_onStartR(request, ctx) { michael@0: try { michael@0: var chan = request.QueryInterface(Components.interfaces.nsIChannel); michael@0: if (chan.contentType == unknownType) michael@0: do_throw("Type should not be unknown!"); michael@0: if (sniffing_enabled && this._iteration > 2 && michael@0: chan.contentType != sniffedType) { michael@0: do_throw("Expecting <" + sniffedType +"> but got <" + michael@0: chan.contentType + "> for " + chan.URI.spec); michael@0: } else if (!sniffing_enabled && chan.contentType == sniffedType) { michael@0: do_throw("Sniffing not enabled but sniffer called for " + chan.URI.spec); michael@0: } michael@0: } catch (e) { michael@0: do_throw("Unexpected exception: " + e); michael@0: } michael@0: michael@0: throw Components.results.NS_ERROR_ABORT; michael@0: }, michael@0: michael@0: onDataAvailable: function test_ODA() { michael@0: throw Components.results.NS_ERROR_UNEXPECTED; michael@0: }, michael@0: michael@0: onStopRequest: function test_onStopR(request, ctx, status) { michael@0: run_test_iteration(this._iteration); michael@0: do_test_finished(); michael@0: }, michael@0: michael@0: _iteration: 1 michael@0: }; michael@0: michael@0: function makeChan(url) { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null); michael@0: if (sniffing_enabled) michael@0: chan.loadFlags |= Components.interfaces.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS; michael@0: michael@0: return chan; michael@0: } michael@0: michael@0: var httpserv = null; michael@0: var urls = null; michael@0: michael@0: function run_test() { michael@0: httpserv = new HttpServer(); michael@0: httpserv.start(-1); michael@0: michael@0: urls = [ michael@0: // NOTE: First URL here runs without our content sniffer michael@0: "data:" + unknownType + ", Some text", michael@0: "data:" + unknownType + ", Text", // Make sure sniffing works even if we michael@0: // used the unknown content sniffer too michael@0: "data:text/plain, Some more text", michael@0: "http://localhost:" + httpserv.identity.primaryPort michael@0: ]; michael@0: michael@0: Components.manager.nsIComponentRegistrar.registerFactory(snifferCID, michael@0: "Unit test content sniffer", snifferContract, sniffer); michael@0: michael@0: run_test_iteration(1); michael@0: } michael@0: michael@0: function run_test_iteration(index) { michael@0: if (index > urls.length) { michael@0: if (sniffing_enabled) { michael@0: sniffing_enabled = false; michael@0: index = listener._iteration = 1; michael@0: } else { michael@0: do_test_pending(); michael@0: httpserv.stop(do_test_finished); michael@0: return; // we're done michael@0: } michael@0: } michael@0: michael@0: if (sniffing_enabled && index == 2) { michael@0: // Register our sniffer only here michael@0: // This also makes sure that dynamic registration is working michael@0: var catMan = Components.classes["@mozilla.org/categorymanager;1"] michael@0: .getService(Components.interfaces.nsICategoryManager); michael@0: catMan.nsICategoryManager.addCategoryEntry(categoryName, "unit test", michael@0: snifferContract, false, true); michael@0: } michael@0: michael@0: var chan = makeChan(urls[index - 1]); michael@0: michael@0: listener._iteration++; michael@0: chan.asyncOpen(listener, null); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: