browser_net_block-before-network.js (3349B)
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 request blocking prevents requests from reaching the server. 8 */ 9 10 add_task(async function () { 11 const httpServer = setupTestServer(); 12 const port = httpServer.identity.primaryPort; 13 14 const { tab, monitor } = await initNetMonitor( 15 `http://localhost:${port}/index.html`, 16 { 17 requestCount: 1, 18 } 19 ); 20 info("Starting test... "); 21 22 const { document, store, windowRequire } = monitor.panelWin; 23 24 // Action should be processed synchronously in tests 25 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 26 store.dispatch(Actions.batchEnable(false)); 27 28 // Open the request blocking panel 29 store.dispatch(Actions.toggleRequestBlockingPanel()); 30 31 // Helper to send a request from the content page to /count and return the 32 // count. 33 async function fetchCount() { 34 return ContentTask.spawn( 35 tab.linkedBrowser, 36 `http://localhost:${port}/count`, 37 async function (url) { 38 const response = await content.wrappedJSObject.fetch(url); 39 const _count = await response.text(); 40 return _count * 1; 41 } 42 ); 43 } 44 45 info("Send a first request to /count, non-blocked"); 46 let onNetworkEvent = waitForNetworkEvents(monitor, 1); 47 let count = await fetchCount(); 48 await onNetworkEvent; 49 50 is(count, 1, "Count is set to 1"); 51 52 info("Block requests matching the pattern 'count'"); 53 await addBlockedRequest("count", monitor); 54 55 info("Send several requests to /count, blocked"); 56 // The bug can be reliably reproduced in some scenarios, but I could not find 57 // a consistent way to make it fail with browser mochitests. 58 // With 100 requests, the old implementation would usually let a few requests 59 // go through. 60 const blockedRequestsCount = 100; 61 onNetworkEvent = waitForNetworkEvents(monitor, blockedRequestsCount); 62 for (let i = 0; i < blockedRequestsCount; i++) { 63 try { 64 await fetchCount(); 65 } catch (e) {} 66 } 67 await onNetworkEvent; 68 69 info("Unblock requests matching the pattern 'count'"); 70 const requestItems = document.querySelectorAll(".request-list-item"); 71 EventUtils.sendMouseEvent({ type: "mousedown" }, requestItems[1]); 72 await toggleBlockedUrl(requestItems[1], monitor, store, "unblock"); 73 74 info("Send a last request to /count, non-blocked"); 75 onNetworkEvent = waitForNetworkEvents(monitor, 1); 76 count = await fetchCount(); 77 await onNetworkEvent; 78 79 // Count should only be set to 2, because the second request never reached 80 // the server. 81 is(count, 2, "Count is set to 2"); 82 83 await teardown(monitor); 84 }); 85 86 function setupTestServer() { 87 const httpServer = createTestHTTPServer(); 88 httpServer.registerContentType("html", "text/html"); 89 90 httpServer.registerPathHandler("/index.html", function (request, response) { 91 response.setStatusLine(request.httpVersion, 200, "OK"); 92 response.write(`<!DOCTYPE html> 93 <html><body><h1>Test requests blocked before reaching the server 94 `); 95 }); 96 97 let counter = 0; 98 httpServer.registerPathHandler("/count", function (request, response) { 99 response.setStatusLine(request.httpVersion, 200, "OK"); 100 counter++; 101 response.setHeader("Content-Type", "text/plain", false); 102 response.write(counter); 103 }); 104 105 return httpServer; 106 }