browser_scriptCache_redirect.js (3000B)
1 const TEST_URL = "https://example.com/browser/dom/tests/browser/dummy.html"; 2 const SCRIPT_NAME = "redirect_server.sjs"; 3 const TEST_SCRIPT_URL = 4 "https://example.com/browser/dom/tests/browser/" + SCRIPT_NAME; 5 6 function getCounter(tab, query) { 7 const browser = tab.linkedBrowser; 8 const scriptPath = SCRIPT_NAME + query; 9 return SpecialPowers.spawn(browser, [scriptPath], async scriptPath => { 10 const { promise, resolve } = Promise.withResolvers(); 11 12 const script = content.document.createElement("script"); 13 script.src = scriptPath; 14 script.addEventListener("load", resolve); 15 content.document.body.appendChild(script); 16 17 await promise; 18 19 return parseInt(content.document.body.getAttribute("counter")); 20 }); 21 } 22 23 async function reloadAndGetCounter(tab, query) { 24 await BrowserTestUtils.reloadTab(tab); 25 26 return getCounter(tab, query); 27 } 28 29 add_task(async function test_redirectCache() { 30 await SpecialPowers.pushPrefEnv({ 31 set: [["dom.script_loader.experimental.navigation_cache", true]], 32 }); 33 registerCleanupFunction(() => SpecialPowers.popPrefEnv()); 34 35 const tests = [ 36 { 37 query: "?redirect=cacheable&script=cacheable", 38 cachedCounter: true, 39 log: [",redirect=cacheable&script=cacheable", ",script=cacheable"].join( 40 "" 41 ), 42 }, 43 { 44 query: "?redirect=cacheable&script=not-cacheable", 45 cachedCounter: false, 46 log: [ 47 ",redirect=cacheable&script=not-cacheable", 48 ",script=not-cacheable", 49 ",script=not-cacheable", 50 ].join(""), 51 }, 52 { 53 query: "?redirect=not-cacheable&script=cacheable", 54 cachedCounter: true, 55 log: [ 56 ",redirect=not-cacheable&script=cacheable", 57 ",script=cacheable", 58 ",redirect=not-cacheable&script=cacheable", 59 ].join(""), 60 }, 61 { 62 query: "?redirect=not-cacheable&script=not-cacheable", 63 cachedCounter: false, 64 log: [ 65 ",redirect=not-cacheable&script=not-cacheable", 66 ",script=not-cacheable", 67 ",redirect=not-cacheable&script=not-cacheable", 68 ",script=not-cacheable", 69 ].join(""), 70 }, 71 ]; 72 73 for (const { query, cachedCounter, log } of tests) { 74 ChromeUtils.clearResourceCache(); 75 Services.cache2.clear(); 76 77 const resetResponse = await fetch(TEST_SCRIPT_URL + "?reset"); 78 is(await resetResponse.text(), "reset", "Server state should be reset"); 79 80 const tab = await BrowserTestUtils.openNewForegroundTab({ 81 gBrowser, 82 url: TEST_URL, 83 }); 84 85 is( 86 await getCounter(tab, query), 87 0, 88 "counter should be 0 for the first load." 89 ); 90 91 const counter = await reloadAndGetCounter(tab, query); 92 if (cachedCounter) { 93 is(counter, 0, "cache should be used for " + query); 94 } else { 95 is(counter, 1, "cache should not be used for " + query); 96 } 97 98 const logResponse = await fetch(TEST_SCRIPT_URL + "?log"); 99 is(await logResponse.text(), log, "Log should match"); 100 101 BrowserTestUtils.removeTab(tab); 102 } 103 });