summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/test/browser_net_headers_sorted.js
blob: 2965ea8d3ce3a683cb8ca097aa207a874b7140ac (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests if Request-Headers and Response-Headers are sorted in Headers tab.
 * The test also verifies that headers with the same name and headers
 * with an empty value are also displayed.
 *
 * The test also checks that raw headers are displayed in the original
 * order and not sorted.
 */
add_task(async function () {
  const { monitor } = await initNetMonitor(HTTPS_SIMPLE_SJS, {
    requestCount: 1,
  });
  info("Starting test... ");

  const { store, windowRequire } = monitor.panelWin;
  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
  store.dispatch(Actions.batchEnable(false));

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

  // Verify request and response headers.
  await verifyHeaders(monitor);
  await verifyRawHeaders(monitor);

  // Clean up
  await teardown(monitor);
});

async function verifyHeaders(monitor) {
  const { document, store } = monitor.panelWin;

  info("Check if Request-Headers and Response-Headers are sorted");

  const wait = waitForDOM(document, ".headers-overview");
  EventUtils.sendMouseEvent(
    { type: "mousedown" },
    document.querySelectorAll(".request-list-item")[0]
  );
  await wait;

  await waitForRequestData(store, ["requestHeaders", "responseHeaders"]);

  const expectedResponseHeaders = [
    "cache-control",
    "connection",
    "content-length",
    "content-type",
    "date",
    "expires",
    "foo-bar",
    "foo-bar",
    "foo-bar",
    "pragma",
    "server",
    "set-cookie",
    "set-cookie",
  ];
  const expectedRequestHeaders = [
    "Accept",
    "Accept-Encoding",
    "Accept-Language",
    "Cache-Control",
    "Connection",
    "Cookie",
    "Host",
    "Pragma",
    "Sec-Fetch-Dest",
    "Sec-Fetch-Mode",
    "Sec-Fetch-Site",
    "Upgrade-Insecure-Requests",
    "User-Agent",
  ];

  const responseLabelCells = document.querySelectorAll(
    "#responseHeaders .treeLabelCell"
  );
  const requestLabelCells = document.querySelectorAll(
    "#requestHeaders .treeLabelCell"
  );
  const actualResponseHeaders = [];
  const actualRequestHeaders = [];

  for (let i = 0; i < responseLabelCells.length; i++) {
    actualResponseHeaders.push(responseLabelCells[i].innerText);
  }

  for (let i = 0; i < requestLabelCells.length; i++) {
    actualRequestHeaders.push(requestLabelCells[i].innerText);
  }

  is(
    actualResponseHeaders.toString(),
    expectedResponseHeaders.toString(),
    "Response Headers are sorted"
  );

  is(
    actualRequestHeaders.toString(),
    expectedRequestHeaders.toString(),
    "Request Headers are sorted"
  );
}

async function verifyRawHeaders(monitor) {
  const { document } = monitor.panelWin;

  info("Check if raw Request-Headers and raw Response-Headers are not sorted");

  const actualResponseHeaders = [];
  const actualRequestHeaders = [];

  const expectedResponseHeaders = [
    "cache-control",
    "pragma",
    "expires",
    "set-cookie",
    "set-cookie",
    "content-type",
    "foo-bar",
    "foo-bar",
    "foo-bar",
    "connection",
    "server",
    "date",
    "content-length",
  ];

  const expectedRequestHeaders = [
    "Host",
    "User-Agent",
    "Accept",
    "Accept-Language",
    "Accept-Encoding",
    "Connection",
    "Cookie",
    "Upgrade-Insecure-Requests",
    "Sec-Fetch-Dest",
    "Sec-Fetch-Mode",
    "Sec-Fetch-Site",
    "Pragma",
    "Cache-Control",
  ];

  // Click the 'Raw headers' toggle to show original headers source.
  for (const rawToggleInput of document.querySelectorAll(
    ".devtools-checkbox-toggle"
  )) {
    rawToggleInput.click();
  }

  // Wait till raw headers are available.
  let rawArr;
  await waitUntil(() => {
    rawArr = document.querySelectorAll("textarea.raw-headers");
    // Both raw headers must be present
    return rawArr.length > 1;
  });

  // Request headers are rendered first, so it is element with index 1
  const requestHeadersText = rawArr[1].textContent;
  // Response headers are rendered first, so it is element with index 0
  const responseHeadersText = rawArr[0].textContent;

  const rawRequestHeadersArray = requestHeadersText.split("\n");
  for (let i = 1; i < rawRequestHeadersArray.length; i++) {
    const header = rawRequestHeadersArray[i];
    actualRequestHeaders.push(header.split(":")[0]);
  }

  const rawResponseHeadersArray = responseHeadersText.split("\n");
  for (let i = 1; i < rawResponseHeadersArray.length; i++) {
    const header = rawResponseHeadersArray[i];
    actualResponseHeaders.push(header.split(":")[0]);
  }

  is(
    actualResponseHeaders.toString(),
    expectedResponseHeaders.toString(),
    "Raw Response Headers are not sorted"
  );

  is(
    actualRequestHeaders.toString(),
    expectedRequestHeaders.toString(),
    "Raw Request Headers are not sorted"
  );
}