Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / devtools / client / netmonitor / test / browser_net_block-before-network.js
blob2f585781f1baa06223b7c01cd555769f10ba49be
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 /**
7 * Test that request blocking prevents requests from reaching the server.
8 */
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`,
17 requestCount: 1,
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
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;
45 info("Send a first request to /count, non-blocked");
46 let onNetworkEvent = waitForNetworkEvents(monitor, 1);
47 let count = await fetchCount();
48 await onNetworkEvent;
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
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) {}
67 await onNetworkEvent;
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();
77 await onNetworkEvent;
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");
83 await teardown(monitor);
84 });
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
94 `);
95 });
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);
105 return httpServer;