netwerk/test/unit/test_force_sniffing.js

changeset 1
ca08bd8f51b2
equal deleted inserted replaced
-1:000000000000 0:724b7169b081
1 // This file tests the flag LOAD_TREAT_APPLICATION_OCTET_STREAM_AS_UNKNOWN.
2
3 Cu.import("resource://testing-common/httpd.js");
4
5 const octetStreamType = "application/octet-stream";
6 const sniffedType = "application/x-sniffed";
7
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";
11
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 };
32
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 },
39
40 onDataAvailable: function test_ODA() {
41 throw Components.results.NS_ERROR_UNEXPECTED;
42 },
43
44 onStopRequest: function test_onStopR(request, ctx, status) {
45 do_test_finished();
46 }
47 };
48
49 function handler(metadata, response) {
50 response.setHeader("Content-Type", octetStreamType);
51 }
52
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;
61
62 return chan;
63 }
64
65 XPCOMUtils.defineLazyGetter(this, "url", function() {
66 return "http://localhost:" + httpserv.identity.primaryPort + "/test";
67 });
68
69 var httpserv = null;
70
71 function run_test() {
72 httpserv = new HttpServer();
73 httpserv.registerPathHandler("/test", handler);
74 httpserv.start(-1);
75
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);
79
80 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
81 .getService(Components.interfaces.nsICategoryManager);
82 catMan.nsICategoryManager.addCategoryEntry(categoryName, snifferContract,
83 snifferContract, false, true);
84
85 var chan = makeChan(url);
86 chan.asyncOpen(listener, null);
87
88 do_test_pending();
89 }
90

mercurial