browser_net_chunked-response.js (3577B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests that delayed chunked responses are displayed correctly. 8 * The chunks should be displayed as they are received from the backend. 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 requests blocked before reaching the server</body> 19 </html> 20 ` 21 ); 22 }); 23 24 let chunkedResponse; 25 httpServer.registerPathHandler("/chunked-data", function (request, response) { 26 response.processAsync(); 27 response.setStatusLine(request.httpVersion, 400, "OK"); 28 response.setHeader("Content-Type", "text/plain", false); 29 response.setHeader("Transfer-Encoding", "chunked", false); 30 response.write("7\r\n"); 31 response.write("Mozilla\r\n"); 32 chunkedResponse = response; 33 }); 34 35 const completeResponse = () => { 36 chunkedResponse.write("11\r\n"); 37 chunkedResponse.write("Developer Network\r\n"); 38 chunkedResponse.write("0\r\n"); 39 chunkedResponse.write("\r\n"); 40 chunkedResponse.finish(); 41 }; 42 43 return { httpServer, completeResponse }; 44 } 45 46 add_task(async function () { 47 const { httpServer, completeResponse } = setupTestServer(); 48 const port = httpServer.identity.primaryPort; 49 50 const { tab, monitor } = await initNetMonitor( 51 `http://localhost:${port}/index.html`, 52 { requestCount: 1 } 53 ); 54 info("Starting test... "); 55 56 const { document, store, windowRequire } = monitor.panelWin; 57 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 58 59 store.dispatch(Actions.batchEnable(false)); 60 61 info("Start fetching chunked content"); 62 const waitForRequest = waitUntil(() => { 63 const request = document.querySelector(".request-list-item"); 64 return ( 65 request && request.querySelector(".requests-list-transferred").textContent 66 ); 67 }); 68 await SpecialPowers.spawn( 69 tab.linkedBrowser, 70 [`http://localhost:${port}/chunked-data`], 71 async url => { 72 await content.wrappedJSObject.fetch(url); 73 } 74 ); 75 await waitForRequest; 76 77 info("Assert the size of the transferred chunk in the column"); 78 const chunkedRequest = document.querySelector(".request-list-item"); 79 is( 80 chunkedRequest.querySelector(".requests-list-transferred").textContent, 81 "154 B", 82 "The transferred data should be correct" 83 ); 84 85 info("Open the response panel and wait for initial chunk of the data"); 86 const waitForResponsePanel = waitForDOM( 87 document, 88 "#response-panel .cm-content" 89 ); 90 store.dispatch(Actions.toggleNetworkDetails()); 91 clickOnSidebarTab(document, "response"); 92 await waitForResponsePanel; 93 94 is( 95 getCodeMirrorValue(monitor), 96 "Mozilla", 97 "The text shown in the source editor is correct." 98 ); 99 100 info("Send the last chunk of data"); 101 completeResponse(); 102 103 info("Wait for the last chunk of the data"); 104 await waitUntil(() => getCodeMirrorValue(monitor).length > 7); 105 106 info("Assert the size of the all the transferred data in the column"); 107 is( 108 getCodeMirrorValue(monitor), 109 "MozillaDeveloper Network", 110 "The text shown in the source editor is correct." 111 ); 112 is( 113 chunkedRequest.querySelector(".requests-list-transferred").textContent, 114 "171 B", 115 "The transferred data is correct" 116 ); 117 118 await teardown(monitor); 119 });