|
1 // This file tests nsIContentSniffer, introduced in bug 324985 |
|
2 |
|
3 Cu.import("resource://testing-common/httpd.js"); |
|
4 |
|
5 const unknownType = "application/x-unknown-content-type"; |
|
6 const sniffedType = "application/x-sniffed"; |
|
7 |
|
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"; |
|
11 |
|
12 var sniffing_enabled = true; |
|
13 |
|
14 /** |
|
15 * This object is both a factory and an nsIContentSniffer implementation (so, it |
|
16 * is de-facto a service) |
|
17 */ |
|
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; |
|
25 }, |
|
26 createInstance: function sniffer_ci(outer, iid) { |
|
27 if (outer) |
|
28 throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
29 return this.QueryInterface(iid); |
|
30 }, |
|
31 lockFactory: function sniffer_lockf(lock) { |
|
32 throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
|
33 }, |
|
34 |
|
35 getMIMETypeFromContent: function (request, data, length) { |
|
36 return sniffedType; |
|
37 } |
|
38 }; |
|
39 |
|
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); |
|
52 } |
|
53 } catch (e) { |
|
54 do_throw("Unexpected exception: " + e); |
|
55 } |
|
56 |
|
57 throw Components.results.NS_ERROR_ABORT; |
|
58 }, |
|
59 |
|
60 onDataAvailable: function test_ODA() { |
|
61 throw Components.results.NS_ERROR_UNEXPECTED; |
|
62 }, |
|
63 |
|
64 onStopRequest: function test_onStopR(request, ctx, status) { |
|
65 run_test_iteration(this._iteration); |
|
66 do_test_finished(); |
|
67 }, |
|
68 |
|
69 _iteration: 1 |
|
70 }; |
|
71 |
|
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; |
|
78 |
|
79 return chan; |
|
80 } |
|
81 |
|
82 var httpserv = null; |
|
83 var urls = null; |
|
84 |
|
85 function run_test() { |
|
86 httpserv = new HttpServer(); |
|
87 httpserv.start(-1); |
|
88 |
|
89 urls = [ |
|
90 // NOTE: First URL here runs without our content sniffer |
|
91 "data:" + unknownType + ", Some text", |
|
92 "data:" + unknownType + ", Text", // Make sure sniffing works even if we |
|
93 // used the unknown content sniffer too |
|
94 "data:text/plain, Some more text", |
|
95 "http://localhost:" + httpserv.identity.primaryPort |
|
96 ]; |
|
97 |
|
98 Components.manager.nsIComponentRegistrar.registerFactory(snifferCID, |
|
99 "Unit test content sniffer", snifferContract, sniffer); |
|
100 |
|
101 run_test_iteration(1); |
|
102 } |
|
103 |
|
104 function run_test_iteration(index) { |
|
105 if (index > urls.length) { |
|
106 if (sniffing_enabled) { |
|
107 sniffing_enabled = false; |
|
108 index = listener._iteration = 1; |
|
109 } else { |
|
110 do_test_pending(); |
|
111 httpserv.stop(do_test_finished); |
|
112 return; // we're done |
|
113 } |
|
114 } |
|
115 |
|
116 if (sniffing_enabled && index == 2) { |
|
117 // Register our sniffer only here |
|
118 // This also makes sure that dynamic registration is working |
|
119 var catMan = Components.classes["@mozilla.org/categorymanager;1"] |
|
120 .getService(Components.interfaces.nsICategoryManager); |
|
121 catMan.nsICategoryManager.addCategoryEntry(categoryName, "unit test", |
|
122 snifferContract, false, true); |
|
123 } |
|
124 |
|
125 var chan = makeChan(urls[index - 1]); |
|
126 |
|
127 listener._iteration++; |
|
128 chan.asyncOpen(listener, null); |
|
129 |
|
130 do_test_pending(); |
|
131 } |
|
132 |