netwerk/test/unit/test_content_sniffer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_content_sniffer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +// This file tests nsIContentSniffer, introduced in bug 324985
     1.5 +
     1.6 +Cu.import("resource://testing-common/httpd.js");
     1.7 +
     1.8 +const unknownType = "application/x-unknown-content-type";
     1.9 +const sniffedType = "application/x-sniffed";
    1.10 +
    1.11 +const snifferCID = Components.ID("{4c93d2db-8a56-48d7-b261-9cf2a8d998eb}");
    1.12 +const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1";
    1.13 +const categoryName = "net-content-sniffers";
    1.14 +
    1.15 +var sniffing_enabled = true;
    1.16 +
    1.17 +/**
    1.18 + * This object is both a factory and an nsIContentSniffer implementation (so, it
    1.19 + * is de-facto a service)
    1.20 + */
    1.21 +var sniffer = {
    1.22 +  QueryInterface: function sniffer_qi(iid) {
    1.23 +    if (iid.equals(Components.interfaces.nsISupports) ||
    1.24 +        iid.equals(Components.interfaces.nsIFactory) ||
    1.25 +        iid.equals(Components.interfaces.nsIContentSniffer))
    1.26 +      return this;
    1.27 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.28 +  },
    1.29 +  createInstance: function sniffer_ci(outer, iid) {
    1.30 +    if (outer)
    1.31 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
    1.32 +    return this.QueryInterface(iid);
    1.33 +  },
    1.34 +  lockFactory: function sniffer_lockf(lock) {
    1.35 +    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    1.36 +  },
    1.37 +
    1.38 +  getMIMETypeFromContent: function (request, data, length) {
    1.39 +    return sniffedType;
    1.40 +  }
    1.41 +};
    1.42 +
    1.43 +var listener = {
    1.44 +  onStartRequest: function test_onStartR(request, ctx) {
    1.45 +    try {
    1.46 +      var chan = request.QueryInterface(Components.interfaces.nsIChannel);
    1.47 +      if (chan.contentType == unknownType)
    1.48 +        do_throw("Type should not be unknown!");
    1.49 +      if (sniffing_enabled && this._iteration > 2 &&
    1.50 +          chan.contentType != sniffedType) {
    1.51 +        do_throw("Expecting <" + sniffedType +"> but got <" +
    1.52 +                 chan.contentType + "> for " + chan.URI.spec);
    1.53 +      } else if (!sniffing_enabled && chan.contentType == sniffedType) {
    1.54 +        do_throw("Sniffing not enabled but sniffer called for " + chan.URI.spec);
    1.55 +      }
    1.56 +    } catch (e) {
    1.57 +      do_throw("Unexpected exception: " + e);
    1.58 +    }
    1.59 +
    1.60 +    throw Components.results.NS_ERROR_ABORT;
    1.61 +  },
    1.62 +
    1.63 +  onDataAvailable: function test_ODA() {
    1.64 +    throw Components.results.NS_ERROR_UNEXPECTED;
    1.65 +  },
    1.66 +
    1.67 +  onStopRequest: function test_onStopR(request, ctx, status) {
    1.68 +    run_test_iteration(this._iteration);
    1.69 +    do_test_finished();
    1.70 +  },
    1.71 +
    1.72 +  _iteration: 1
    1.73 +};
    1.74 +
    1.75 +function makeChan(url) {
    1.76 +  var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.77 +                      .getService(Components.interfaces.nsIIOService);
    1.78 +  var chan = ios.newChannel(url, null, null);
    1.79 +  if (sniffing_enabled)
    1.80 +    chan.loadFlags |= Components.interfaces.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
    1.81 +
    1.82 +  return chan;
    1.83 +}
    1.84 +
    1.85 +var httpserv = null;
    1.86 +var urls = null;
    1.87 +
    1.88 +function run_test() {
    1.89 +  httpserv = new HttpServer();
    1.90 +  httpserv.start(-1);
    1.91 +
    1.92 +  urls = [
    1.93 +  // NOTE: First URL here runs without our content sniffer
    1.94 +  "data:" + unknownType + ", Some text",
    1.95 +  "data:" + unknownType + ", Text", // Make sure sniffing works even if we
    1.96 +                                    // used the unknown content sniffer too
    1.97 +  "data:text/plain, Some more text",
    1.98 +    "http://localhost:" + httpserv.identity.primaryPort
    1.99 +];
   1.100 +
   1.101 +  Components.manager.nsIComponentRegistrar.registerFactory(snifferCID,
   1.102 +    "Unit test content sniffer", snifferContract, sniffer);
   1.103 +
   1.104 +  run_test_iteration(1);
   1.105 +}
   1.106 +
   1.107 +function run_test_iteration(index) {
   1.108 +  if (index > urls.length) {
   1.109 +    if (sniffing_enabled) {
   1.110 +        sniffing_enabled = false;
   1.111 +        index = listener._iteration = 1;
   1.112 +    } else {
   1.113 +        do_test_pending();
   1.114 +        httpserv.stop(do_test_finished);
   1.115 +        return; // we're done
   1.116 +    }
   1.117 +  }
   1.118 +
   1.119 +  if (sniffing_enabled && index == 2) {
   1.120 +    // Register our sniffer only here
   1.121 +    // This also makes sure that dynamic registration is working
   1.122 +    var catMan = Components.classes["@mozilla.org/categorymanager;1"]
   1.123 +                           .getService(Components.interfaces.nsICategoryManager);
   1.124 +    catMan.nsICategoryManager.addCategoryEntry(categoryName, "unit test",
   1.125 +                                               snifferContract, false, true);
   1.126 +  }
   1.127 +
   1.128 +  var chan = makeChan(urls[index - 1]);
   1.129 +
   1.130 +  listener._iteration++;
   1.131 +  chan.asyncOpen(listener, null);
   1.132 +
   1.133 +  do_test_pending();
   1.134 +}
   1.135 +

mercurial