summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_throttling_profiles.js
blob: 707efb818966c82ba66a99140070c60770c533fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test all the network throttling profiles

"use strict";

requestLongerTimeout(2);

const throttlingProfiles = require("resource://devtools/client/shared/components/throttling/profiles.js");

const httpServer = createTestHTTPServer();
httpServer.registerPathHandler(`/`, function (request, response) {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.write(`<meta charset=utf8><h1>Test throttling profiles</h1>`);
});

// The "data" path takes a size query parameter and will return a body of the
// requested size.
httpServer.registerPathHandler("/data", function (request, response) {
  const size = request.queryString.match(/size=(\d+)/)[1];
  response.setHeader("Content-Type", "text/plain");

  response.setStatusLine(request.httpVersion, 200, "OK");
  const body = new Array(size * 1).join("a");
  response.bodyOutputStream.write(body, body.length);
});

const TEST_URI = `http://localhost:${httpServer.identity.primaryPort}/`;

add_task(async function () {
  await pushPref("devtools.cache.disabled", true);

  const { monitor } = await initNetMonitor(TEST_URI, { requestCount: 1 });
  const { store, connector, windowRequire } = monitor.panelWin;
  const { updateNetworkThrottling } = connector;

  const { getSortedRequests } = windowRequire(
    "devtools/client/netmonitor/src/selectors/index"
  );

  for (const profile of throttlingProfiles) {
    info(`Starting test for throttling profile ${JSON.stringify(profile)}`);

    info("sending throttle request");
    await updateNetworkThrottling(true, profile);

    const onRequest = waitForNetworkEvents(monitor, 1);
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [profile], _profile => {
      // Size must be greater than the profile download cap.
      const size = _profile.download * 2;
      content.fetch("data?size=" + size);
    });
    await onRequest;

    info(`Wait for eventTimings for throttling profile ${profile.id}`);
    await waitForRequestData(store, ["eventTimings"]);

    const requestItem = getSortedRequests(store.getState()).at(-1);
    ok(
      requestItem.eventTimings.timings.receive > 1000,
      `Request was properly throttled for profile ${profile.id}`
    );
  }

  await teardown(monitor);
});