diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/tests/mochitest/beacon/test_beaconContentPolicy.html | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | dom/tests/mochitest/beacon/test_beaconContentPolicy.html | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/dom/tests/mochitest/beacon/test_beaconContentPolicy.html b/dom/tests/mochitest/beacon/test_beaconContentPolicy.html new file mode 100644 index 0000000000..0e1bce574e --- /dev/null +++ b/dom/tests/mochitest/beacon/test_beaconContentPolicy.html @@ -0,0 +1,103 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=936340 +--> +<head> + <title>Test that sendBeacon obeys content policy directives</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a> +<p id="display"></p> +<div id="content" style="display: none"> + +</div> +<pre id="test"> +<script class="testbody" type="text/javascript"> + +var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs"; + +const Cc = SpecialPowers.Cc; +const Ci = SpecialPowers.Ci; + +// not enabled by default yet. +SimpleTest.waitForExplicitFinish(); + +var policy; + +SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest); + +function setupPolicy() { + var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}"); + var policyName = "@mozilla.org/testpolicy;1"; + var policy = { + // nsISupports implementation + QueryInterface(iid) { + iid = SpecialPowers.wrap(iid); + if (iid.equals(Ci.nsISupports) || + iid.equals(Ci.nsIFactory) || + iid.equals(Ci.nsIContentPolicy)) + return this; + throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; + }, + + // nsIFactory implementation + createInstance(iid) { + return this.QueryInterface(iid); + }, + + // nsIContentPolicy implementation + shouldLoad(contentLocation, loadInfo, mimeTypeGuess) { + // Remember last content type seen for the test url + let contentType = loadInfo.externalContentPolicyType; + + if (SpecialPowers.wrap(contentLocation).spec == beaconUrl) { + is(contentType, Ci.nsIContentPolicy.TYPE_BEACON, "Beacon content type should match expected. is: " + contentType + " should be: " + Ci.nsIContentPolicy.TYPE_BEACON); + teardownPolicy(); + SimpleTest.finish(); + } + + return Ci.nsIContentPolicy.ACCEPT; + }, + + shouldProcess(contentLocation, loadInfo, mimeTypeGuess) { + return Ci.nsIContentPolicy.ACCEPT; + } + } + policy = SpecialPowers.wrapCallbackObject(policy); + + // Register content policy + var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); + componentManager.registerFactory(policyID, "Test content policy", policyName, policy); + + var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); + categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true); + + return { 'policy': policy, 'policyID': policyID, 'policyName': policyName }; +} + +function teardownPolicy() { + setTimeout(function() { + // policy will not be removed from the category correctly + var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); + componentManager.unregisterFactory(policy.policyID, policy.policy); + var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); + categoryManager.deleteCategoryEntry("content-policy", policy.policyName, false); + }, 0); +} + +function beginTest() { + policy = setupPolicy(); + // Make sure to hit the event loop here in order to ensure that nsContentPolicy + // has been notified of the newly registered policy. + SimpleTest.executeSoon(function() { + navigator.sendBeacon(beaconUrl, "bacon would have been a better name than beacon"); + }); +} + +</script> +</pre> +</body> +</html> |