1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
7 * Test that request blocking prevents requests from reaching the server.
10 add_task(async
function () {
11 const httpServer
= setupTestServer();
12 const port
= httpServer
.identity
.primaryPort
;
14 const { tab
, monitor
} = await
initNetMonitor(
15 `http://localhost:${port}/index.html`,
20 info("Starting test... ");
22 const { document
, store
, windowRequire
} = monitor
.panelWin
;
24 // Action should be processed synchronously in tests
25 const Actions
= windowRequire("devtools/client/netmonitor/src/actions/index");
26 store
.dispatch(Actions
.batchEnable(false));
28 // Open the request blocking panel
29 store
.dispatch(Actions
.toggleRequestBlockingPanel());
31 // Helper to send a request from the content page to /count and return the
33 async
function fetchCount() {
34 return ContentTask
.spawn(
36 `http://localhost:${port}/count`,
37 async
function (url
) {
38 const response
= await content
.wrappedJSObject
.fetch(url
);
39 const _count
= await response
.text();
45 info("Send a first request to /count, non-blocked");
46 let onNetworkEvent
= waitForNetworkEvents(monitor
, 1);
47 let count
= await
fetchCount();
50 is(count
, 1, "Count is set to 1");
52 info("Block requests matching the pattern 'count'");
53 await
addBlockedRequest("count", monitor
);
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
60 const blockedRequestsCount
= 100;
61 onNetworkEvent
= waitForNetworkEvents(monitor
, blockedRequestsCount
);
62 for (let i
= 0; i
< blockedRequestsCount
; i
++) {
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");
74 info("Send a last request to /count, non-blocked");
75 onNetworkEvent
= waitForNetworkEvents(monitor
, 1);
76 count
= await
fetchCount();
79 // Count should only be set to 2, because the second request never reached
81 is(count
, 2, "Count is set to 2");
83 await
teardown(monitor
);
86 function setupTestServer() {
87 const httpServer
= createTestHTTPServer();
88 httpServer
.registerContentType("html", "text/html");
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
98 httpServer
.registerPathHandler("/count", function (request
, response
) {
99 response
.setStatusLine(request
.httpVersion
, 200, "OK");
101 response
.setHeader("Content-Type", "text/plain", false);
102 response
.write(counter
);