netwerk/test/unit/test_content_sniffer.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 // This file tests nsIContentSniffer, introduced in bug 324985
     3 Cu.import("resource://testing-common/httpd.js");
     5 const unknownType = "application/x-unknown-content-type";
     6 const sniffedType = "application/x-sniffed";
     8 const snifferCID = Components.ID("{4c93d2db-8a56-48d7-b261-9cf2a8d998eb}");
     9 const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1";
    10 const categoryName = "net-content-sniffers";
    12 var sniffing_enabled = true;
    14 /**
    15  * This object is both a factory and an nsIContentSniffer implementation (so, it
    16  * is de-facto a service)
    17  */
    18 var sniffer = {
    19   QueryInterface: function sniffer_qi(iid) {
    20     if (iid.equals(Components.interfaces.nsISupports) ||
    21         iid.equals(Components.interfaces.nsIFactory) ||
    22         iid.equals(Components.interfaces.nsIContentSniffer))
    23       return this;
    24     throw Components.results.NS_ERROR_NO_INTERFACE;
    25   },
    26   createInstance: function sniffer_ci(outer, iid) {
    27     if (outer)
    28       throw Components.results.NS_ERROR_NO_AGGREGATION;
    29     return this.QueryInterface(iid);
    30   },
    31   lockFactory: function sniffer_lockf(lock) {
    32     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    33   },
    35   getMIMETypeFromContent: function (request, data, length) {
    36     return sniffedType;
    37   }
    38 };
    40 var listener = {
    41   onStartRequest: function test_onStartR(request, ctx) {
    42     try {
    43       var chan = request.QueryInterface(Components.interfaces.nsIChannel);
    44       if (chan.contentType == unknownType)
    45         do_throw("Type should not be unknown!");
    46       if (sniffing_enabled && this._iteration > 2 &&
    47           chan.contentType != sniffedType) {
    48         do_throw("Expecting <" + sniffedType +"> but got <" +
    49                  chan.contentType + "> for " + chan.URI.spec);
    50       } else if (!sniffing_enabled && chan.contentType == sniffedType) {
    51         do_throw("Sniffing not enabled but sniffer called for " + chan.URI.spec);
    52       }
    53     } catch (e) {
    54       do_throw("Unexpected exception: " + e);
    55     }
    57     throw Components.results.NS_ERROR_ABORT;
    58   },
    60   onDataAvailable: function test_ODA() {
    61     throw Components.results.NS_ERROR_UNEXPECTED;
    62   },
    64   onStopRequest: function test_onStopR(request, ctx, status) {
    65     run_test_iteration(this._iteration);
    66     do_test_finished();
    67   },
    69   _iteration: 1
    70 };
    72 function makeChan(url) {
    73   var ios = Components.classes["@mozilla.org/network/io-service;1"]
    74                       .getService(Components.interfaces.nsIIOService);
    75   var chan = ios.newChannel(url, null, null);
    76   if (sniffing_enabled)
    77     chan.loadFlags |= Components.interfaces.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
    79   return chan;
    80 }
    82 var httpserv = null;
    83 var urls = null;
    85 function run_test() {
    86   httpserv = new HttpServer();
    87   httpserv.start(-1);
    89   urls = [
    90   // NOTE: First URL here runs without our content sniffer
    91   "data:" + unknownType + ", Some text",
    92   "data:" + unknownType + ", Text", // Make sure sniffing works even if we
    93                                     // used the unknown content sniffer too
    94   "data:text/plain, Some more text",
    95     "http://localhost:" + httpserv.identity.primaryPort
    96 ];
    98   Components.manager.nsIComponentRegistrar.registerFactory(snifferCID,
    99     "Unit test content sniffer", snifferContract, sniffer);
   101   run_test_iteration(1);
   102 }
   104 function run_test_iteration(index) {
   105   if (index > urls.length) {
   106     if (sniffing_enabled) {
   107         sniffing_enabled = false;
   108         index = listener._iteration = 1;
   109     } else {
   110         do_test_pending();
   111         httpserv.stop(do_test_finished);
   112         return; // we're done
   113     }
   114   }
   116   if (sniffing_enabled && index == 2) {
   117     // Register our sniffer only here
   118     // This also makes sure that dynamic registration is working
   119     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
   120                            .getService(Components.interfaces.nsICategoryManager);
   121     catMan.nsICategoryManager.addCategoryEntry(categoryName, "unit test",
   122                                                snifferContract, false, true);
   123   }
   125   var chan = makeChan(urls[index - 1]);
   127   listener._iteration++;
   128   chan.asyncOpen(listener, null);
   130   do_test_pending();
   131 }

mercurial