browser_favicon_nostore.js (5598B)
1 /* Any copyright is dedicated to the Public Domain. 2 * https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test that a favicon with Cache-Control: no-store is not stored in Places. 5 // Also tests that favicons added after pageshow are not stored, unless they 6 // are root icons. 7 8 const TEST_SITE = "http://example.net"; 9 const ICON_URL = 10 TEST_SITE + "/browser/browser/base/content/test/favicons/no-store.png"; 11 const PAGE_URL = 12 TEST_SITE + "/browser/browser/base/content/test/favicons/no-store.html"; 13 14 async function cleanup() { 15 Services.cache2.clear(); 16 await PlacesTestUtils.clearFavicons(); 17 await PlacesUtils.history.clear(); 18 } 19 20 add_task(async function browser_loader() { 21 await cleanup(); 22 let iconPromise = waitForFaviconMessage(true, ICON_URL); 23 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL); 24 registerCleanupFunction(async () => { 25 await cleanup(); 26 }); 27 28 let { iconURL } = await iconPromise; 29 is(iconURL, ICON_URL, "Should have seen the expected icon."); 30 31 // Ensure the favicon has not been stored. 32 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 33 await new Promise(resolve => setTimeout(resolve, 1000)); 34 let favicon = await PlacesUtils.favicons.getFaviconForPage( 35 Services.io.newURI(PAGE_URL) 36 ); 37 Assert.ok(!favicon); 38 BrowserTestUtils.removeTab(tab); 39 }); 40 41 async function later_addition(iconUrl) { 42 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL); 43 registerCleanupFunction(async () => { 44 await cleanup(); 45 BrowserTestUtils.removeTab(tab); 46 }); 47 48 let iconPromise = waitForFaviconMessage(true, iconUrl); 49 await ContentTask.spawn(gBrowser.selectedBrowser, iconUrl, href => { 50 let doc = content.document; 51 let head = doc.head; 52 let link = doc.createElement("link"); 53 link.rel = "icon"; 54 link.href = href; 55 link.type = "image/png"; 56 head.appendChild(link); 57 }); 58 let { iconURL } = await iconPromise; 59 is(iconURL, iconUrl, "Should have seen the expected icon."); 60 61 // Ensure the favicon has not been stored. 62 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 63 await new Promise(resolve => setTimeout(resolve, 1000)); 64 let favicon = await PlacesUtils.favicons.getFaviconForPage( 65 Services.io.newURI(PAGE_URL) 66 ); 67 Assert.ok(!favicon); 68 BrowserTestUtils.removeTab(tab); 69 } 70 71 add_task(async function test_later_addition() { 72 for (let iconUrl of [ 73 TEST_SITE + "/browser/browser/base/content/test/favicons/moz.png", 74 "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABH0lEQVRYw2P8////f4YBBEwMAwxGHcBCUMX/91DGOSj/BpT/DkpzQChGBSjfBErLQsVZhmoI/L8LpRdD6X1QietQGhYy7FB5aAgwmkLpBKi4BZTPMThDgBGjHIDF+f9mKD0fKvGBRKNdoF7sgPL1saaJwZgGDkJ9vpZMn8PAHqg5G9FyifBgD4H/W9HyOWrU/f+DIzHhkoeZxxgzZEIAVtJ9RxX+Q6DAxCmP3byhXxkxshAs5odqbcioAY3UC1CBLyTGOTqAmsfAOWRCwBvqxV0oIUB2OQAzDy3/D+a6wB7q8mCU2vD/nw94GziYIQOtDRn9oXz+IZMGBKGMbCjNh9Ii+v8HR4uIAUeLiEEbb9twELaIRlqrmHG0bzjiHQAA1LVfww8jwM4AAAAASUVORK5CYII=", 75 ]) { 76 await later_addition(iconUrl); 77 } 78 }); 79 80 add_task(async function root_icon_stored() { 81 XPCShellContentUtils.ensureInitialized(this); 82 let server = XPCShellContentUtils.createHttpServer({ 83 hosts: ["www.nostore.com"], 84 }); 85 server.registerFile( 86 "/favicon.ico", 87 new FileUtils.File( 88 PathUtils.join( 89 Services.dirsvc.get("CurWorkD", Ci.nsIFile).path, 90 "browser", 91 "browser", 92 "base", 93 "content", 94 "test", 95 "favicons", 96 "no-store.png" 97 ) 98 ) 99 ); 100 server.registerPathHandler("/page", (request, response) => { 101 response.write("<html>A page without icon</html>"); 102 }); 103 104 let noStorePromise = TestUtils.topicObserved("http-on-stop-request", s => { 105 let chan = s.QueryInterface(Ci.nsIHttpChannel); 106 return chan?.URI.spec == "http://www.nostore.com/favicon.ico"; 107 }).then(([chan]) => chan.isNoStoreResponse()); 108 109 await BrowserTestUtils.withNewTab( 110 { 111 gBrowser, 112 url: "http://www.nostore.com/page", 113 }, 114 async function () { 115 await TestUtils.waitForCondition(async () => { 116 let favicon = await PlacesUtils.favicons.getFaviconForPage( 117 Services.io.newURI("http://www.nostore.com/page") 118 ); 119 return favicon?.uri.spec == "http://www.nostore.com/favicon.ico"; 120 }, "wait for the favicon to be stored"); 121 Assert.ok(await noStorePromise, "Should have received no-store header"); 122 } 123 ); 124 }); 125 126 add_task(async function root_icon_after_pageshow_stored() { 127 XPCShellContentUtils.ensureInitialized(this); 128 let server = XPCShellContentUtils.createHttpServer({ 129 hosts: ["rootafterpageshow.com"], 130 }); 131 server.registerFile( 132 "/favicon.ico", 133 new FileUtils.File( 134 PathUtils.join( 135 Services.dirsvc.get("CurWorkD", Ci.nsIFile).path, 136 "browser", 137 "browser", 138 "base", 139 "content", 140 "test", 141 "favicons", 142 "no-store.png" 143 ) 144 ) 145 ); 146 server.registerPathHandler("/page", (request, response) => { 147 response.write(`<html><body> 148 <link rel="shortcut icon" href="/favicon.ico"> 149 A page with root icon in body. 150 </body></html>`); 151 }); 152 153 await BrowserTestUtils.withNewTab( 154 { 155 gBrowser, 156 url: "http://rootafterpageshow.com/page", 157 }, 158 async function () { 159 await TestUtils.waitForCondition(async () => { 160 let favicon = await PlacesUtils.favicons.getFaviconForPage( 161 Services.io.newURI("http://rootafterpageshow.com/page") 162 ); 163 return favicon?.uri.spec == "http://rootafterpageshow.com/favicon.ico"; 164 }, "wait for the favicon to be stored"); 165 } 166 ); 167 });