Remove UTF8 BOM marker from last commit.
[wine-gecko.git] / netwerk / test / unit / test_content_sniffer.js
blob013de2bd9d2b01240ca920dfbda4453ea2b6632d
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;
14 /**
15 * This object is both a factory and an nsIContentSniffer implementation (so, it
16 * is de-facto a service)
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;
26 createInstance: function sniffer_ci(outer, iid) {
27 if (outer)
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) {
36 return sniffedType;
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);
53 } catch (e) {
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);
66 do_test_finished();
69 _iteration: 1
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;
82 var urls = [
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"
91 var httpserv = null;
93 function run_test() {
94 httpserv = new nsHttpServer();
95 httpserv.start(4444);
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;
108 } else {
109 httpserv.stop();
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);
128 do_test_pending();