netwerk/test/unit/test_force_sniffing.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // This file tests the flag LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN.
michael@0 2
michael@0 3 Cu.import("resource://testing-common/httpd.js");
michael@0 4
michael@0 5 const octetStreamType = "application/octet-stream";
michael@0 6 const sniffedType = "application/x-sniffed";
michael@0 7
michael@0 8 const snifferCID = Components.ID("{954f3fdd-d717-4c02-9464-7c2da617d21d}");
michael@0 9 const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1";
michael@0 10 const categoryName = "content-sniffing-services";
michael@0 11
michael@0 12 var sniffer = {
michael@0 13 QueryInterface: function sniffer_qi(iid) {
michael@0 14 if (iid.equals(Components.interfaces.nsISupports) ||
michael@0 15 iid.equals(Components.interfaces.nsIFactory) ||
michael@0 16 iid.equals(Components.interfaces.nsIContentSniffer))
michael@0 17 return this;
michael@0 18 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 19 },
michael@0 20 createInstance: function sniffer_ci(outer, iid) {
michael@0 21 if (outer)
michael@0 22 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 23 return this.QueryInterface(iid);
michael@0 24 },
michael@0 25 lockFactory: function sniffer_lockf(lock) {
michael@0 26 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
michael@0 27 },
michael@0 28 getMIMETypeFromContent: function (request, data, length) {
michael@0 29 return sniffedType;
michael@0 30 }
michael@0 31 };
michael@0 32
michael@0 33 var listener = {
michael@0 34 onStartRequest: function test_onStartR(request, ctx) {
michael@0 35 // We should have sniffed the type of the file.
michael@0 36 var chan = request.QueryInterface(Components.interfaces.nsIChannel);
michael@0 37 do_check_eq(chan.contentType, sniffedType);
michael@0 38 },
michael@0 39
michael@0 40 onDataAvailable: function test_ODA() {
michael@0 41 throw Components.results.NS_ERROR_UNEXPECTED;
michael@0 42 },
michael@0 43
michael@0 44 onStopRequest: function test_onStopR(request, ctx, status) {
michael@0 45 do_test_finished();
michael@0 46 }
michael@0 47 };
michael@0 48
michael@0 49 function handler(metadata, response) {
michael@0 50 response.setHeader("Content-Type", octetStreamType);
michael@0 51 }
michael@0 52
michael@0 53 function makeChan(url) {
michael@0 54 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 55 .getService(Components.interfaces.nsIIOService);
michael@0 56 var chan = ios.newChannel(url, null, null);
michael@0 57 // Force sniffing if we have "application/octet-stream" as Content-Type
michael@0 58 chan.loadFlags |= Components.interfaces
michael@0 59 .nsIChannel
michael@0 60 .LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN;
michael@0 61
michael@0 62 return chan;
michael@0 63 }
michael@0 64
michael@0 65 XPCOMUtils.defineLazyGetter(this, "url", function() {
michael@0 66 return "http://localhost:" + httpserv.identity.primaryPort + "/test";
michael@0 67 });
michael@0 68
michael@0 69 var httpserv = null;
michael@0 70
michael@0 71 function run_test() {
michael@0 72 httpserv = new HttpServer();
michael@0 73 httpserv.registerPathHandler("/test", handler);
michael@0 74 httpserv.start(-1);
michael@0 75
michael@0 76 // Register our fake sniffer that always returns the content-type we want.
michael@0 77 Components.manager.nsIComponentRegistrar.registerFactory(snifferCID,
michael@0 78 "Unit test content sniffer", snifferContract, sniffer);
michael@0 79
michael@0 80 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
michael@0 81 .getService(Components.interfaces.nsICategoryManager);
michael@0 82 catMan.nsICategoryManager.addCategoryEntry(categoryName, snifferContract,
michael@0 83 snifferContract, false, true);
michael@0 84
michael@0 85 var chan = makeChan(url);
michael@0 86 chan.asyncOpen(listener, null);
michael@0 87
michael@0 88 do_test_pending();
michael@0 89 }
michael@0 90

mercurial