browser_net_block-pattern.js (3775B)
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 basic request blocking functionality for patterns 8 * Ensures that request blocking unblocks a relevant pattern and not just 9 * an exact URL match 10 */ 11 12 add_task(async function () { 13 await pushPref("devtools.netmonitor.features.requestBlocking", true); 14 15 const { tab, monitor } = await initNetMonitor(HTTPS_CUSTOM_GET_URL, { 16 requestCount: 1, 17 }); 18 info("Starting test... "); 19 20 const { document, store, windowRequire } = monitor.panelWin; 21 22 // Action should be processed synchronously in tests 23 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 24 store.dispatch(Actions.batchEnable(false)); 25 26 // Open the request blocking panel 27 store.dispatch(Actions.toggleRequestBlockingPanel()); 28 29 // Add patterns which should block some of the requests 30 await addBlockedRequest("test1", monitor); 31 await addBlockedRequest("test/*/test3", monitor); 32 33 // Close the blocking panel to ensure it's opened by the context menu later 34 store.dispatch(Actions.toggleRequestBlockingPanel()); 35 36 // Execute two XHRs (the same URL) and wait till they're finished 37 const TEST_URL_1 = HTTPS_SEARCH_SJS + "?value=test1"; 38 const TEST_URL_2 = HTTPS_SEARCH_SJS + "?value=test2"; 39 const TEST_URL_3 = HTTPS_SEARCH_SJS + "test/something/test3"; 40 const TEST_URL_4 = HTTPS_SEARCH_SJS + "test/something/test4"; 41 42 let wait = waitForNetworkEvents(monitor, 4); 43 await ContentTask.spawn(tab.linkedBrowser, TEST_URL_1, async function (url) { 44 content.wrappedJSObject.performRequests(1, url); 45 }); 46 await ContentTask.spawn(tab.linkedBrowser, TEST_URL_2, async function (url) { 47 content.wrappedJSObject.performRequests(1, url); 48 }); 49 await ContentTask.spawn(tab.linkedBrowser, TEST_URL_3, async function (url) { 50 content.wrappedJSObject.performRequests(1, url); 51 }); 52 await ContentTask.spawn(tab.linkedBrowser, TEST_URL_4, async function (url) { 53 content.wrappedJSObject.performRequests(1, url); 54 }); 55 await wait; 56 57 // Wait till there are four resources rendered in the results 58 await waitForDOMIfNeeded(document, ".request-list-item", 4); 59 60 let requestItems = document.querySelectorAll(".request-list-item"); 61 // Ensure that test1 item was blocked and test2 item wasn't 62 ok( 63 checkRequestListItemBlocked(requestItems[0]), 64 "The first request was blocked" 65 ); 66 ok( 67 !checkRequestListItemBlocked(requestItems[1]), 68 "The second request was not blocked" 69 ); 70 // Ensure that test3 item was blocked and test4 item wasn't 71 ok( 72 checkRequestListItemBlocked(requestItems[2]), 73 "The third request was blocked" 74 ); 75 ok( 76 !checkRequestListItemBlocked(requestItems[3]), 77 "The fourth request was not blocked" 78 ); 79 80 EventUtils.sendMouseEvent({ type: "mousedown" }, requestItems[0]); 81 // Right-click test1, select "Unblock URL" from its context menu 82 await toggleBlockedUrl(requestItems[0], monitor, store, "unblock"); 83 84 // Ensure that the request blocking panel is now open and the item is unchecked 85 ok( 86 !document.querySelector(".request-blocking-list .devtools-checkbox") 87 .checked, 88 "The blocking pattern is disabled from context menu" 89 ); 90 91 // Request the unblocked URL again, ensure the URL was not blocked 92 wait = waitForNetworkEvents(monitor, 1); 93 await ContentTask.spawn(tab.linkedBrowser, TEST_URL_1, async function (url) { 94 content.wrappedJSObject.performRequests(1, url); 95 }); 96 await wait; 97 98 await waitForDOMIfNeeded(document, ".request-list-item", 5); 99 requestItems = document.querySelectorAll(".request-list-item"); 100 ok( 101 !checkRequestListItemBlocked(requestItems[4]), 102 "The fifth request was not blocked" 103 ); 104 105 await teardown(monitor); 106 });