test_http_auth_cache.html (2400B)
1 <!DOCTYPE HTML> 2 <!-- Any copyright is dedicated to the Public Domain. 3 - http://creativecommons.org/publicdomain/zero/1.0/ --> 4 <html> 5 <head> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <script type="text/javascript" src="../../../toolkit/components/prompts/test/prompt_common.js"></script> 8 </head> 9 <body> 10 <script type="application/javascript"> 11 12 async function populateHttpAuthCache() { 13 await fetch("authenticate.sjs"); 14 } 15 16 async function clearHttpAuthCacheTest() { 17 let script = SpecialPowers.loadChromeScript(function () { 18 /* eslint-env mozilla/chrome-script */ 19 const {addMessageListener, sendAsyncMessage} = this; 20 addMessageListener("start", function () { 21 try { 22 let httpAuthCache = Cc["@mozilla.org/network/http-auth-cache;1"] 23 .getService(Ci.nsIHttpAuthCache); 24 25 let entries = httpAuthCache.getEntries(); 26 sendAsyncMessage("before", entries.length); 27 28 for (let entry of entries) { 29 console.log(entry.realm); 30 httpAuthCache.clearEntry(entry); 31 } 32 33 let afterEntries = httpAuthCache.getEntries(); 34 sendAsyncMessage("after", afterEntries.length); 35 } catch (e) { 36 console.error("Failed to get or clear the HTTP auth cache", e); 37 sendAsyncMessage("before", -1); 38 sendAsyncMessage("after", -1); 39 } 40 }); 41 }); 42 43 script.sendAsyncMessage("start"); 44 await script.promiseOneMessage("before").then(val => { 45 ok(val > 0, `got ${val}, cache size before clearing should be above 0`); 46 }); 47 await script.promiseOneMessage("after").then(val => { 48 is(val, 0, "cache should be empty after clearing"); 49 }); 50 } 51 52 add_task(async function () { 53 const state = { 54 msg: "This site is asking you to sign in.", 55 title: "Authentication Required", 56 textValue: "", 57 passValue: "", 58 iconClass: "authentication-icon question-icon", 59 titleHidden: true, 60 textHidden: false, 61 passHidden: false, 62 checkHidden: true, 63 checkMsg: "", 64 checked: false, 65 focused: "textField", 66 defButton: "button0", 67 }; 68 const action = { 69 buttonClick: "ok", 70 textField: "user", 71 passField: "pass", 72 }; 73 let promptDone = handlePrompt(state, action); 74 75 await populateHttpAuthCache(); 76 await promptDone; 77 await clearHttpAuthCacheTest(); 78 }); 79 80 </script> 81 </body> 82 </html>