summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_copy_headers.js
blob: 458a19af9742b9827042be821e50ca547cb89530 (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
95
96
97
98
99
100
101
102
103
104
105
106
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests if copying a request's request/response headers works.
 */

add_task(async function () {
  const { monitor } = await initNetMonitor(SIMPLE_URL, {
    requestCount: 1,
  });
  info("Starting test... ");

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

  const wait = waitForNetworkEvents(monitor, 1);
  await reloadBrowser();
  await wait;

  EventUtils.sendMouseEvent(
    { type: "mousedown" },
    document.querySelectorAll(".request-list-item")[0]
  );

  const requestItem = getSortedRequests(store.getState())[0];
  const { method, httpVersion, status, statusText } = requestItem;

  EventUtils.sendMouseEvent(
    { type: "contextmenu" },
    document.querySelectorAll(".request-list-item")[0]
  );

  const selectedRequest = getSelectedRequest(store.getState());
  is(selectedRequest, requestItem, "Proper request is selected");

  const EXPECTED_REQUEST_HEADERS = [
    `${method} ${SIMPLE_URL.split("example.com")[1]} ${httpVersion}`,
    "Host: example.com",
    "User-Agent: " + navigator.userAgent + "",
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language: " + navigator.languages.join(",") + ";q=0.5",
    "Accept-Encoding: gzip, deflate",
    "Connection: keep-alive",
    "Upgrade-Insecure-Requests: 1",
    "Priority: u=1",
    "Pragma: no-cache",
    "Cache-Control: no-cache",
  ].join("\n");

  await waitForClipboardPromise(
    async function setup() {
      await selectContextMenuItem(
        monitor,
        "request-list-context-copy-request-headers"
      );
    },
    function validate(result) {
      // Sometimes, a "Cookie" header is left over from other tests. Remove it:
      result = String(result).replace(/Cookie: [^\n]+\n/, "");
      return result === EXPECTED_REQUEST_HEADERS;
    }
  );
  info("Clipboard contains the currently selected item's request headers.");

  const EXPECTED_RESPONSE_HEADERS = [
    `${httpVersion} ${status} ${statusText}`,
    "last-modified: Sun, 3 May 2015 11:11:11 GMT",
    "content-type: text/html",
    "content-length: 465",
    "connection: close",
    "server: httpd.js",
    "date: Sun, 3 May 2015 11:11:11 GMT",
  ].join("\n");

  EventUtils.sendMouseEvent(
    { type: "contextmenu" },
    document.querySelectorAll(".request-list-item")[0]
  );

  await waitForClipboardPromise(
    async function setup() {
      await selectContextMenuItem(
        monitor,
        "response-list-context-copy-response-headers"
      );
    },
    function validate(result) {
      // Fake the "Last-Modified" and "Date" headers because they will vary:
      result = String(result)
        .replace(
          /last-modified: [^\n]+ GMT/,
          "last-modified: Sun, 3 May 2015 11:11:11 GMT"
        )
        .replace(/date: [^\n]+ GMT/, "date: Sun, 3 May 2015 11:11:11 GMT");
      return result === EXPECTED_RESPONSE_HEADERS;
    }
  );
  info("Clipboard contains the currently selected item's response headers.");

  await teardown(monitor);
});