commit 3bcfc371da7089be3137d5c2a4910a5a0ca33487
parent 979eed83e5f099d7e9875fbfd870f80670d6f390
Author: Sean <sekim@mozilla.com>
Date: Tue, 16 Dec 2025 19:21:11 +0000
Bug 1915371 - Part 2: Add a mochitest for HTTP Authentication Cache XPIDL r=edgul,necko-reviewers,jesup
Differential Revision: https://phabricator.services.mozilla.com/D244063
Diffstat:
3 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/netwerk/test/mochitests/authenticate.sjs b/netwerk/test/mochitests/authenticate.sjs
@@ -0,0 +1,20 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function handleRequest(request, response) {
+ const expectedAuth = "Basic " + btoa("user:pass");
+
+ let actualAuth = request.hasHeader("Authorization")
+ ? request.getHeader("Authorization")
+ : null;
+
+ if (actualAuth === expectedAuth) {
+ response.setStatusLine("1.1", 200, "OK");
+ response.write("Authenticated");
+ } else {
+ response.setStatusLine("1.1", 401, "Unauthorized");
+ response.setHeader("WWW-Authenticate", 'Basic realm="TestRealm"');
+ response.write("Authentication required");
+ }
+}
diff --git a/netwerk/test/mochitests/mochitest.toml b/netwerk/test/mochitests/mochitest.toml
@@ -140,6 +140,9 @@ skip-if = [
["test_fetch_lnk.html"]
+["test_http_auth_cache.html"]
+support-files = ["authenticate.sjs"]
+
["test_idn_redirect.html"]
skip-if = [
"http2",
diff --git a/netwerk/test/mochitests/test_http_auth_cache.html b/netwerk/test/mochitests/test_http_auth_cache.html
@@ -0,0 +1,63 @@
+<!DOCTYPE HTML>
+<!-- Any copyright is dedicated to the Public Domain.
+ - http://creativecommons.org/publicdomain/zero/1.0/ -->
+<html>
+ <head>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ </head>
+<body>
+ <script type="application/javascript">
+
+ SimpleTest.waitForExplicitFinish();
+
+ async function populateHttpAuthCache() {
+ await fetch("authenticate.sjs");
+ }
+
+ async function clearHttpAuthCacheTest() {
+ let script = SpecialPowers.loadChromeScript(function () {
+ /* eslint-env mozilla/frame-script */
+ addMessageListener("start", function () {
+ try {
+ let httpAuthCache = Cc["@mozilla.org/network/http-auth-cache;1"]
+ .getService(Ci.nsIHttpAuthCache);
+
+ // for(let k in httpAuthCache) {
+ // console.log("Method:", k);
+ // }
+ let entries = httpAuthCache.getEntries();
+ sendAsyncMessage("before", entries.length);
+
+ for (let entry of entries) {
+ console.log(entry.realm);
+ httpAuthCache.clearEntry(entry);
+ }
+
+ let afterEntries = httpAuthCache.getEntries(1);
+ sendAsyncMessage("after", afterEntries.length);
+ } catch (e) {
+ console.error("Failed to get or clear the HTTP auth cache", e);
+ sendAsyncMessage("before", -1);
+ sendAsyncMessage("after", -1);
+ }
+ });
+ });
+
+ script.sendAsyncMessage("start");
+ await script.promiseOneMessage("before").then(val => {
+ ok(val > 0, `got ${val}, cache size before clearing should be above 0`);
+ });
+ await script.promiseOneMessage("after").then(val => {
+ is(val, 0, "cache should be empty after clearing");
+ });
+ }
+
+ add_task(async function () {
+ await populateHttpAuthCache();
+ await clearHttpAuthCacheTest();
+ SimpleTest.finish();
+ });
+
+</script>
+</body>
+</html>