1 // This file tests nsIContentSniffer, introduced in bug 324985
3 do_import_script("netwerk/test/httpserver/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;
15 * This object is both a factory and an nsIContentSniffer implementation (so, it
16 * is de-facto a service)
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
))
24 throw Components
.results
.NS_ERROR_NO_INTERFACE
;
26 createInstance
: function sniffer_ci(outer
, iid
) {
28 throw Components
.results
.NS_ERROR_NO_AGGREGATION
;
29 return this.QueryInterface(iid
);
31 lockFactory
: function sniffer_lockf(lock
) {
32 throw Components
.results
.NS_ERROR_NOT_IMPLEMENTED
;
35 getMIMETypeFromContent: function (request
, data
, length
) {
41 onStartRequest
: function test_onStartR(request
, ctx
) {
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
);
54 do_throw("Unexpected exception: " + e
);
57 throw Components
.results
.NS_ERROR_ABORT
;
60 onDataAvailable
: function test_ODA() {
61 throw Components
.results
.NS_ERROR_UNEXPECTED
;
64 onStopRequest
: function test_onStopR(request
, ctx
, status
) {
65 run_test_iteration(this._iteration
);
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);
77 chan
.loadFlags
|= Components
.interfaces
.nsIChannel
.LOAD_CALL_CONTENT_SNIFFERS
;
83 // NOTE: First URL here runs without our content sniffer
84 "data:" + unknownType
+ ", Some text",
85 "data:" + unknownType
+ ", Text", // Make sure sniffing works even if we
86 // used the unknown content sniffer too
87 "data:text/plain, Some more text",
88 "http://localhost:4444"
94 httpserv
= new nsHttpServer();
97 Components
.manager
.nsIComponentRegistrar
.registerFactory(snifferCID
,
98 "Unit test content sniffer", snifferContract
, sniffer
);
100 run_test_iteration(1);
103 function run_test_iteration(index
) {
104 if (index
> urls
.length
) {
105 if (sniffing_enabled
) {
106 sniffing_enabled
= false;
107 index
= listener
._iteration
= 1;
110 return; // we're done
114 if (sniffing_enabled
&& index
== 2) {
115 // Register our sniffer only here
116 // This also makes sure that dynamic registration is working
117 var catMan
= Components
.classes
["@mozilla.org/categorymanager;1"]
118 .getService(Components
.interfaces
.nsICategoryManager
);
119 catMan
.nsICategoryManager
.addCategoryEntry(categoryName
, "unit test",
120 snifferContract
, false, true);
123 var chan
= makeChan(urls
[index
- 1]);
125 listener
._iteration
++;
126 chan
.asyncOpen(listener
, null);