michael@0: // This file tests the flag LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN. michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: const octetStreamType = "application/octet-stream"; michael@0: const sniffedType = "application/x-sniffed"; michael@0: michael@0: const snifferCID = Components.ID("{954f3fdd-d717-4c02-9464-7c2da617d21d}"); michael@0: const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1"; michael@0: const categoryName = "content-sniffing-services"; 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: 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: // We should have sniffed the type of the file. michael@0: var chan = request.QueryInterface(Components.interfaces.nsIChannel); michael@0: do_check_eq(chan.contentType, sniffedType); 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: do_test_finished(); michael@0: } michael@0: }; michael@0: michael@0: function handler(metadata, response) { michael@0: response.setHeader("Content-Type", octetStreamType); 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: // Force sniffing if we have "application/octet-stream" as Content-Type michael@0: chan.loadFlags |= Components.interfaces michael@0: .nsIChannel michael@0: .LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN; michael@0: michael@0: return chan; michael@0: } michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "url", function() { michael@0: return "http://localhost:" + httpserv.identity.primaryPort + "/test"; michael@0: }); michael@0: michael@0: var httpserv = null; michael@0: michael@0: function run_test() { michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerPathHandler("/test", handler); michael@0: httpserv.start(-1); michael@0: michael@0: // Register our fake sniffer that always returns the content-type we want. michael@0: Components.manager.nsIComponentRegistrar.registerFactory(snifferCID, michael@0: "Unit test content sniffer", snifferContract, sniffer); michael@0: michael@0: var catMan = Components.classes["@mozilla.org/categorymanager;1"] michael@0: .getService(Components.interfaces.nsICategoryManager); michael@0: catMan.nsICategoryManager.addCategoryEntry(categoryName, snifferContract, michael@0: snifferContract, false, true); michael@0: michael@0: var chan = makeChan(url); michael@0: chan.asyncOpen(listener, null); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: