browser_net_delayed-response.js (5207B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Test that for delayed responses the network details are displayed 8 * before the response is complete. 9 */ 10 function setupTestServer() { 11 const httpServer = createTestHTTPServer(); 12 httpServer.registerContentType("html", "text/html"); 13 httpServer.registerPathHandler("/index.html", function (request, response) { 14 response.setStatusLine(request.httpVersion, 200, "OK"); 15 response.write( 16 `<!DOCTYPE html> 17 <html> 18 <body>Test for delayed responses</body> 19 </html> 20 ` 21 ); 22 }); 23 24 const { promise, resolve } = Promise.withResolvers(); 25 let delayedResponse; 26 httpServer.registerPathHandler( 27 "/delayed-response", 28 function (request, response) { 29 response.processAsync(); 30 delayedResponse = response; 31 resolve(); 32 } 33 ); 34 35 const resolveResponse = () => { 36 delayedResponse.setStatusLine(delayedResponse.httpVersion, 200, "OK"); 37 delayedResponse.setHeader("Content-Type", "text/plain", false); 38 delayedResponse.write("Here is some content"); 39 delayedResponse.finish(); 40 }; 41 return { httpServer, responseCreated: promise, resolveResponse }; 42 } 43 44 add_task(async function () { 45 const { httpServer, responseCreated, resolveResponse } = setupTestServer(); 46 const port = httpServer.identity.primaryPort; 47 48 const { tab, monitor } = await initNetMonitor( 49 `http://localhost:${port}/index.html`, 50 { requestCount: 1 } 51 ); 52 info("Starting test... "); 53 54 const { document, store, windowRequire } = monitor.panelWin; 55 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 56 57 store.dispatch(Actions.batchEnable(false)); 58 59 const URL = `http://localhost:${port}/delayed-response`; 60 61 info("Start fetching delayed content"); 62 const waitForRequest = waitUntil(() => 63 document.querySelector(".request-list-item") 64 ); 65 SpecialPowers.spawn(tab.linkedBrowser, [URL], async url => { 66 await content.wrappedJSObject.fetch(url); 67 }); 68 await Promise.all([responseCreated, waitForRequest]); 69 70 let request = document.querySelector(".request-list-item"); 71 72 // Should be available when the network event is created 73 info("Assert that the URL is in the column"); 74 is( 75 request.querySelector(".requests-list-url")?.textContent, 76 URL, 77 "The URL of the request should be displayed" 78 ); 79 80 // Should be available on the `response start` event 81 info("Assert that the status is not yet in the column"); 82 is( 83 request.querySelector(".requests-list-status-code")?.textContent, 84 undefined, 85 "The status of the request should not be displayed" 86 ); 87 88 info("Open the headers panel and check that the panel is not empty"); 89 let waitForHeadersPanel = waitForDOM(document, "#headers-panel"); 90 store.dispatch(Actions.toggleNetworkDetails()); 91 EventUtils.sendMouseEvent({ type: "mousedown" }, request); 92 await waitForHeadersPanel; 93 94 info("Assert that the status is not yet in the column"); 95 is( 96 request.querySelector(".requests-list-status-code")?.textContent, 97 undefined, 98 "The status of the request should not yet be displayed" 99 ); 100 101 info("Wait for the response headers"); 102 await waitForDOM(document, "#requestHeaders"); 103 104 ok( 105 !!document.querySelectorAll("#requestHeaders .treeRow").length, 106 "The list of request headers should be visible" 107 ); 108 109 ok( 110 !document.querySelectorAll("#responseHeaders .treeRow").length, 111 "The list of response headers should not be visible yet" 112 ); 113 114 info("Open the response panel and wait for the response content"); 115 let waitForResponsePanel = waitForDOM( 116 document, 117 "#response-panel .empty-notice" 118 ); 119 clickOnSidebarTab(document, "response"); 120 await waitForResponsePanel; 121 122 is( 123 document.querySelector("#response-panel .empty-notice").innerText, 124 "No response data available for this request", 125 "The response text is not available yet" 126 ); 127 128 info("Switch back to the headers panel"); 129 waitForHeadersPanel = waitForDOM(document, "#headers-panel"); 130 clickOnSidebarTab(document, "headers"); 131 await waitForHeadersPanel; 132 133 info("Complete the response"); 134 resolveResponse(); 135 136 info("Wait for the response headers"); 137 await waitForDOM(document, "#responseHeaders"); 138 139 info("Assert that the status is now in the column"); 140 request = document.querySelector(".request-list-item"); 141 is( 142 request.querySelector(".requests-list-status-code").textContent, 143 "200", 144 "The status of the request should be displayed" 145 ); 146 147 ok( 148 !!document.querySelectorAll("#requestHeaders .treeRow").length, 149 "The list of request headers should still be visible" 150 ); 151 152 ok( 153 !!document.querySelectorAll("#responseHeaders .treeRow").length, 154 "The list of response headers should now be visible" 155 ); 156 157 info("Open the response panel and wait for the response content"); 158 waitForResponsePanel = waitForDOM(document, "#response-panel .cm-content"); 159 clickOnSidebarTab(document, "response"); 160 await waitForResponsePanel; 161 162 is( 163 getCodeMirrorValue(monitor), 164 "Here is some content", 165 "The text shown in the source editor is correct." 166 ); 167 168 await teardown(monitor); 169 });