browser_resources_network_events_navigation.js (6480B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test the ResourceCommand API around NETWORK_EVENT when navigating 7 8 const TEST_URI = `${URL_ROOT_SSL}network_document_navigation.html`; 9 const JS_URI = TEST_URI.replace( 10 "network_document_navigation.html", 11 "network_navigation.js" 12 ); 13 const IFRAME_URI = TEST_URI.replace( 14 "network_document_navigation.html", 15 "iframe_request.html" 16 ); 17 const IFRAME_JS_URI = TEST_URI.replace( 18 "network_document_navigation.html", 19 "iframe_request.js" 20 ); 21 22 add_task(async () => { 23 const tab = await addTab(TEST_URI); 24 const commands = await CommandsFactory.forTab(tab); 25 await commands.targetCommand.startListening(); 26 const { resourceCommand } = commands; 27 28 const receivedResources = []; 29 const onAvailable = resources => { 30 for (const resource of resources) { 31 is( 32 resource.resourceType, 33 resourceCommand.TYPES.NETWORK_EVENT, 34 "Received a network event resource" 35 ); 36 receivedResources.push(resource); 37 } 38 }; 39 const onUpdated = updates => { 40 for (const { resource } of updates) { 41 is( 42 resource.resourceType, 43 resourceCommand.TYPES.NETWORK_EVENT, 44 "Received a network update event resource" 45 ); 46 } 47 }; 48 49 // Ensure listening for DOCUMENT_EVENT so that requests are properly cleared on the backend side 50 // (this generates the will-navigate used to clear requests) 51 await resourceCommand.watchResources([resourceCommand.TYPES.DOCUMENT_EVENT], { 52 onAvailable() {}, 53 }); 54 55 await resourceCommand.watchResources([resourceCommand.TYPES.NETWORK_EVENT], { 56 ignoreExistingResources: true, 57 onAvailable, 58 onUpdated, 59 }); 60 61 await reloadBrowser(); 62 63 await waitFor(() => receivedResources.length == 4); 64 65 info("Remove the iframe, to ensure its request is still inspectable"); 66 await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { 67 content.document.querySelector("iframe").remove(); 68 }); 69 70 const navigationRequest = receivedResources.find(r => r.url === TEST_URI); 71 ok(navigationRequest, "The navigation request exists"); 72 73 const jsRequest = receivedResources.find(r => r.url === JS_URI); 74 ok(jsRequest, "The JavaScript request exists"); 75 76 const iframeRequest = receivedResources.find(r => r.url === IFRAME_URI); 77 ok(iframeRequest, "The iframe request exists"); 78 79 const iframeJsRequest = receivedResources.find(r => r.url === IFRAME_JS_URI); 80 ok(iframeJsRequest, "The iframe JavaScript request exists"); 81 82 async function getResponseContent(networkEvent) { 83 const packet = { 84 to: networkEvent.actor, 85 type: "getResponseContent", 86 }; 87 const response = await commands.client.request(packet); 88 return response.content.text; 89 } 90 91 const HTML_CONTENT = await (await fetch(TEST_URI)).text(); 92 const JS_CONTENT = await (await fetch(JS_URI)).text(); 93 const IFRAME_CONTENT = await (await fetch(IFRAME_URI)).text(); 94 const IFRAME_JS_CONTENT = await (await fetch(IFRAME_JS_URI)).text(); 95 96 const isNavigationCacheEnabled = Services.prefs.getBoolPref( 97 "dom.script_loader.experimental.navigation_cache" 98 ); 99 100 const htmlContent = await getResponseContent(navigationRequest); 101 is(htmlContent, HTML_CONTENT); 102 // FIXME: bug 1982557 103 if (!isNavigationCacheEnabled) { 104 const jsContent = await getResponseContent(jsRequest); 105 is(jsContent, JS_CONTENT); 106 } 107 const iframeContent = await getResponseContent(iframeRequest); 108 is(iframeContent, IFRAME_CONTENT); 109 // FIXME: bug 1982557 110 if (!isNavigationCacheEnabled) { 111 const iframeJsContent = await getResponseContent(iframeJsRequest); 112 is(iframeJsContent, IFRAME_JS_CONTENT); 113 } 114 115 await reloadBrowser(); 116 117 await waitFor(() => receivedResources.length == 8); 118 119 try { 120 await getResponseContent(navigationRequest); 121 ok(false, "Shouldn't work"); 122 } catch (e) { 123 is( 124 e.error, 125 "noSuchActor", 126 "Without persist, we can't fetch previous document network data" 127 ); 128 } 129 130 try { 131 await getResponseContent(jsRequest); 132 ok(false, "Shouldn't work"); 133 } catch (e) { 134 is( 135 e.error, 136 "noSuchActor", 137 "Without persist, we can't fetch previous document network data" 138 ); 139 } 140 141 const currentResources = receivedResources.slice(4); 142 143 const navigationRequest2 = currentResources.find(r => r.url === TEST_URI); 144 ok(navigationRequest2, "The navigation request exists"); 145 146 const jsRequest2 = currentResources.find(r => r.url === JS_URI); 147 ok(jsRequest2, "The JavaScript request exists"); 148 149 const iframeRequest2 = currentResources.find(r => r.url === IFRAME_URI); 150 ok(iframeRequest2, "The iframe request exists"); 151 152 const iframeJsRequest2 = currentResources.find(r => r.url === IFRAME_JS_URI); 153 ok(iframeJsRequest2, "The iframe JavaScript request exists"); 154 155 info("But we can fetch data for the last/new document"); 156 const htmlContent2 = await getResponseContent(navigationRequest2); 157 is(htmlContent2, HTML_CONTENT); 158 // FIXME: bug 1982557 159 if (!isNavigationCacheEnabled) { 160 const jsContent2 = await getResponseContent(jsRequest2); 161 is(jsContent2, JS_CONTENT); 162 } 163 const iframeContent2 = await getResponseContent(iframeRequest2); 164 is(iframeContent2, IFRAME_CONTENT); 165 // FIXME: bug 1982557 166 if (!isNavigationCacheEnabled) { 167 const iframeJsContent2 = await getResponseContent(iframeJsRequest2); 168 is(iframeJsContent2, IFRAME_JS_CONTENT); 169 } 170 171 info("Enable persist"); 172 const networkParentFront = 173 await commands.watcherFront.getNetworkParentActor(); 174 await networkParentFront.setPersist(true); 175 176 await reloadBrowser(); 177 178 await waitFor(() => receivedResources.length == 12); 179 180 info("With persist, we can fetch previous document network data"); 181 const htmlContent3 = await getResponseContent(navigationRequest2); 182 is(htmlContent3, HTML_CONTENT); 183 // FIXME: bug 1982557 184 if (!isNavigationCacheEnabled) { 185 const jsContent3 = await getResponseContent(jsRequest2); 186 is(jsContent3, JS_CONTENT); 187 } 188 const iframeContent3 = await getResponseContent(iframeRequest2); 189 is(iframeContent3, IFRAME_CONTENT); 190 // FIXME: bug 1982557 191 if (!isNavigationCacheEnabled) { 192 const iframeJsContent3 = await getResponseContent(iframeJsRequest2); 193 is(iframeJsContent3, IFRAME_JS_CONTENT); 194 } 195 196 await resourceCommand.unwatchResources( 197 [resourceCommand.TYPES.NETWORK_EVENT], 198 { 199 onAvailable, 200 onUpdated, 201 } 202 ); 203 204 await commands.destroy(); 205 BrowserTestUtils.removeTab(tab); 206 });