netwerk/test/unit/test_force_sniffing.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_force_sniffing.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +// This file tests the flag LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN.
     1.5 +
     1.6 +Cu.import("resource://testing-common/httpd.js");
     1.7 +
     1.8 +const octetStreamType = "application/octet-stream";
     1.9 +const sniffedType = "application/x-sniffed";
    1.10 +
    1.11 +const snifferCID = Components.ID("{954f3fdd-d717-4c02-9464-7c2da617d21d}");
    1.12 +const snifferContract = "@mozilla.org/network/unittest/contentsniffer;1";
    1.13 +const categoryName = "content-sniffing-services";
    1.14 +
    1.15 +var sniffer = {
    1.16 +  QueryInterface: function sniffer_qi(iid) {
    1.17 +    if (iid.equals(Components.interfaces.nsISupports) ||
    1.18 +        iid.equals(Components.interfaces.nsIFactory) ||
    1.19 +        iid.equals(Components.interfaces.nsIContentSniffer))
    1.20 +      return this;
    1.21 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.22 +  },
    1.23 +  createInstance: function sniffer_ci(outer, iid) {
    1.24 +    if (outer)
    1.25 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
    1.26 +    return this.QueryInterface(iid);
    1.27 +  },
    1.28 +  lockFactory: function sniffer_lockf(lock) {
    1.29 +    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    1.30 +  },
    1.31 +  getMIMETypeFromContent: function (request, data, length) {
    1.32 +    return sniffedType;
    1.33 +  }
    1.34 +};
    1.35 +
    1.36 +var listener = {
    1.37 +  onStartRequest: function test_onStartR(request, ctx) {
    1.38 +    // We should have sniffed the type of the file.
    1.39 +    var chan = request.QueryInterface(Components.interfaces.nsIChannel);
    1.40 +    do_check_eq(chan.contentType, sniffedType);
    1.41 +  },
    1.42 +
    1.43 +  onDataAvailable: function test_ODA() {
    1.44 +    throw Components.results.NS_ERROR_UNEXPECTED;
    1.45 +  },
    1.46 +
    1.47 +  onStopRequest: function test_onStopR(request, ctx, status) {
    1.48 +    do_test_finished();
    1.49 +  }
    1.50 +};
    1.51 +
    1.52 +function handler(metadata, response) {
    1.53 +  response.setHeader("Content-Type", octetStreamType);
    1.54 +}
    1.55 +
    1.56 +function makeChan(url) {
    1.57 +  var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.58 +                      .getService(Components.interfaces.nsIIOService);
    1.59 +  var chan = ios.newChannel(url, null, null);
    1.60 +  // Force sniffing if we have "application/octet-stream" as Content-Type
    1.61 +  chan.loadFlags |= Components.interfaces
    1.62 +                              .nsIChannel
    1.63 +                              .LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN;
    1.64 +
    1.65 +  return chan;
    1.66 +}
    1.67 +
    1.68 +XPCOMUtils.defineLazyGetter(this, "url", function() {
    1.69 +  return "http://localhost:" + httpserv.identity.primaryPort + "/test";
    1.70 +});
    1.71 +
    1.72 +var httpserv = null;
    1.73 +
    1.74 +function run_test() {
    1.75 +  httpserv = new HttpServer();
    1.76 +  httpserv.registerPathHandler("/test", handler);
    1.77 +  httpserv.start(-1);
    1.78 +
    1.79 +  // Register our fake sniffer that always returns the content-type we want.
    1.80 +  Components.manager.nsIComponentRegistrar.registerFactory(snifferCID,
    1.81 +                       "Unit test content sniffer", snifferContract, sniffer);
    1.82 +
    1.83 +  var catMan = Components.classes["@mozilla.org/categorymanager;1"]
    1.84 +                         .getService(Components.interfaces.nsICategoryManager);
    1.85 +  catMan.nsICategoryManager.addCategoryEntry(categoryName, snifferContract,
    1.86 +                                             snifferContract, false, true);
    1.87 +
    1.88 +  var chan = makeChan(url);
    1.89 +  chan.asyncOpen(listener, null);
    1.90 +
    1.91 +  do_test_pending();
    1.92 +}
    1.93 +

mercurial