dom/tests/mochitest/beacon/test_beaconContentPolicy.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/beacon/test_beaconContentPolicy.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=936340
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test that sendBeacon obeys content policy directives</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript">
    1.22 +
    1.23 +var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs";
    1.24 +
    1.25 +const Cc = SpecialPowers.Cc;
    1.26 +const Ci = SpecialPowers.Ci;
    1.27 +
    1.28 +// not enabled by default yet.
    1.29 +SimpleTest.waitForExplicitFinish();
    1.30 +
    1.31 +var policy;
    1.32 +
    1.33 +SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest);
    1.34 +
    1.35 +function setupPolicy() {
    1.36 +  var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
    1.37 +  var policyName = "@mozilla.org/testpolicy;1";
    1.38 +  var policy = {
    1.39 +    // nsISupports implementation
    1.40 +    QueryInterface: function(iid) {
    1.41 +      iid = SpecialPowers.wrap(iid);
    1.42 +      if (iid.equals(Ci.nsISupports) ||
    1.43 +        iid.equals(Ci.nsIFactory) ||
    1.44 +        iid.equals(Ci.nsIContentPolicy))
    1.45 +        return this;
    1.46 +      throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    1.47 +    },
    1.48 +    
    1.49 +    // nsIFactory implementation
    1.50 +    createInstance: function(outer, iid) {
    1.51 +      return this.QueryInterface(iid);
    1.52 +    },
    1.53 +
    1.54 +    // nsIContentPolicy implementation
    1.55 +    shouldLoad: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
    1.56 +      // Remember last content type seen for the test url
    1.57 +
    1.58 +      if (SpecialPowers.wrap(contentLocation).spec == beaconUrl) {
    1.59 +        is(contentType,  Ci.nsIContentPolicy.TYPE_BEACON, "Beacon content type should match expected.  is: " + contentType + " should be: " + Ci.nsIContentPolicy.TYPE_BEACON);
    1.60 +        teardownPolicy();
    1.61 +        SimpleTest.finish();
    1.62 +      }
    1.63 +
    1.64 +      return Ci.nsIContentPolicy.ACCEPT;
    1.65 +    },
    1.66 +
    1.67 +    shouldProcess: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
    1.68 +      return Ci.nsIContentPolicy.ACCEPT;
    1.69 +    }
    1.70 +  }
    1.71 +  policy = SpecialPowers.wrapCallbackObject(policy);
    1.72 +
    1.73 +  // Register content policy
    1.74 +  var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar);
    1.75 +  componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
    1.76 +
    1.77 +  var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    1.78 +  categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);
    1.79 +
    1.80 +  return { 'policy': policy, 'policyID': policyID, 'policyName': policyName };
    1.81 +}
    1.82 +
    1.83 +function teardownPolicy() {
    1.84 +  setTimeout(function() {
    1.85 +    // policy will not be removed from the category correctly
    1.86 +    var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar);
    1.87 +    componentManager.unregisterFactory(policy.policyID, policy.policy);
    1.88 +    var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    1.89 +    categoryManager.deleteCategoryEntry("content-policy", policy.policyName, false);
    1.90 +  }, 0);
    1.91 +}
    1.92 +
    1.93 +function beginTest() {
    1.94 +  policy = setupPolicy();
    1.95 +  // Make sure to hit the event loop here in order to ensure that nsContentPolicy
    1.96 +  // has been notified of the newly registered policy.
    1.97 +  SimpleTest.executeSoon(function() {
    1.98 +    navigator.sendBeacon(beaconUrl, "bacon would have been a better name than beacon");
    1.99 +  });
   1.100 +}
   1.101 +
   1.102 +</script>
   1.103 +</pre>
   1.104 +</body>
   1.105 +</html>

mercurial