|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=936340 |
|
5 --> |
|
6 <head> |
|
7 <title>Test that sendBeacon obeys content policy directives</title> |
|
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body> |
|
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a> |
|
13 <p id="display"></p> |
|
14 <div id="content" style="display: none"> |
|
15 |
|
16 </div> |
|
17 <pre id="test"> |
|
18 <script class="testbody" type="text/javascript"> |
|
19 |
|
20 var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs"; |
|
21 |
|
22 const Cc = SpecialPowers.Cc; |
|
23 const Ci = SpecialPowers.Ci; |
|
24 |
|
25 // not enabled by default yet. |
|
26 SimpleTest.waitForExplicitFinish(); |
|
27 |
|
28 var policy; |
|
29 |
|
30 SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest); |
|
31 |
|
32 function setupPolicy() { |
|
33 var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}"); |
|
34 var policyName = "@mozilla.org/testpolicy;1"; |
|
35 var policy = { |
|
36 // nsISupports implementation |
|
37 QueryInterface: function(iid) { |
|
38 iid = SpecialPowers.wrap(iid); |
|
39 if (iid.equals(Ci.nsISupports) || |
|
40 iid.equals(Ci.nsIFactory) || |
|
41 iid.equals(Ci.nsIContentPolicy)) |
|
42 return this; |
|
43 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; |
|
44 }, |
|
45 |
|
46 // nsIFactory implementation |
|
47 createInstance: function(outer, iid) { |
|
48 return this.QueryInterface(iid); |
|
49 }, |
|
50 |
|
51 // nsIContentPolicy implementation |
|
52 shouldLoad: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) { |
|
53 // Remember last content type seen for the test url |
|
54 |
|
55 if (SpecialPowers.wrap(contentLocation).spec == beaconUrl) { |
|
56 is(contentType, Ci.nsIContentPolicy.TYPE_BEACON, "Beacon content type should match expected. is: " + contentType + " should be: " + Ci.nsIContentPolicy.TYPE_BEACON); |
|
57 teardownPolicy(); |
|
58 SimpleTest.finish(); |
|
59 } |
|
60 |
|
61 return Ci.nsIContentPolicy.ACCEPT; |
|
62 }, |
|
63 |
|
64 shouldProcess: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) { |
|
65 return Ci.nsIContentPolicy.ACCEPT; |
|
66 } |
|
67 } |
|
68 policy = SpecialPowers.wrapCallbackObject(policy); |
|
69 |
|
70 // Register content policy |
|
71 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
72 componentManager.registerFactory(policyID, "Test content policy", policyName, policy); |
|
73 |
|
74 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
|
75 categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true); |
|
76 |
|
77 return { 'policy': policy, 'policyID': policyID, 'policyName': policyName }; |
|
78 } |
|
79 |
|
80 function teardownPolicy() { |
|
81 setTimeout(function() { |
|
82 // policy will not be removed from the category correctly |
|
83 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
84 componentManager.unregisterFactory(policy.policyID, policy.policy); |
|
85 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
|
86 categoryManager.deleteCategoryEntry("content-policy", policy.policyName, false); |
|
87 }, 0); |
|
88 } |
|
89 |
|
90 function beginTest() { |
|
91 policy = setupPolicy(); |
|
92 // Make sure to hit the event loop here in order to ensure that nsContentPolicy |
|
93 // has been notified of the newly registered policy. |
|
94 SimpleTest.executeSoon(function() { |
|
95 navigator.sendBeacon(beaconUrl, "bacon would have been a better name than beacon"); |
|
96 }); |
|
97 } |
|
98 |
|
99 </script> |
|
100 </pre> |
|
101 </body> |
|
102 </html> |