netwerk/test/unit/test_force_sniffing.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial