summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_websocket_stacks.js
blob: cb9acf9d5ba678d4384884f31adfbb21845eb056 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that we get stack traces for the network requests made when creating
// web sockets on the main or worker threads.

const TOP_FILE_NAME = "html_websocket-test-page.html";
const TOP_URL = HTTPS_EXAMPLE_URL + TOP_FILE_NAME;
const WORKER_FILE_NAME = "js_websocket-worker-test.js";

const EXPECTED_REQUESTS = {
  [TOP_URL]: {
    method: "GET",
    url: TOP_URL,
    causeType: "document",
    causeUri: null,
    stack: false,
  },
  "ws://localhost:8080/": {
    method: "GET",
    url: "ws://localhost:8080/",
    causeType: "websocket",
    causeUri: TOP_URL,
    stack: [
      { fn: "openSocket", file: TOP_URL, line: 6 },
      { file: TOP_FILE_NAME, line: 3 },
    ],
  },
  [HTTPS_EXAMPLE_URL + WORKER_FILE_NAME]: {
    method: "GET",
    url: HTTPS_EXAMPLE_URL + WORKER_FILE_NAME,
    causeType: "script",
    causeUri: TOP_URL,
    stack: [{ file: TOP_URL, line: 9 }],
  },
  "wss://localhost:8081/": {
    method: "GET",
    url: "wss://localhost:8081/",
    causeType: "websocket",
    causeUri: TOP_URL,
    stack: [
      {
        fn: "openWorkerSocket",
        file: HTTPS_EXAMPLE_URL + WORKER_FILE_NAME,
        line: 5,
      },
      { file: WORKER_FILE_NAME, line: 2 },
    ],
  },
};

add_task(async function () {
  // Load a different URL first to instantiate the network monitor before we
  // load the page we're really interested in.
  const { monitor } = await initNetMonitor(SIMPLE_URL, { requestCount: 1 });

  const { store, windowRequire, connector } = monitor.panelWin;
  const { getSortedRequests } = windowRequire(
    "devtools/client/netmonitor/src/selectors/index"
  );

  const onNetworkEvents = waitForNetworkEvents(
    monitor,
    Object.keys(EXPECTED_REQUESTS).length
  );
  await navigateTo(TOP_URL);
  await onNetworkEvents;

  is(
    store.getState().requests.requests.length,
    Object.keys(EXPECTED_REQUESTS).length,
    "All the page events should be recorded."
  );

  const requests = getSortedRequests(store.getState());
  // The expected requests in the same order as the
  // requests. The platform does not guarantee the order so the
  // tests should not depend on that.
  const expectedRequests = [];

  // Wait for stack traces from all requests.
  await Promise.all(
    requests.map(request => {
      expectedRequests.push(EXPECTED_REQUESTS[request.url]);
      return connector.requestData(request.id, "stackTrace");
    })
  );

  validateRequests(expectedRequests, monitor);

  await teardown(monitor);
});