summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/network/browser_responseReceived.js
blob: 5319f0eb65635e4c301f94f5b9ab945fcdce8b32 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const BASE_PATH = "https://example.com/browser/remote/cdp/test/browser/network";
const FRAMESET_URL = `${BASE_PATH}/doc_frameset.html`;
const FRAMESET_JS_URL = `${BASE_PATH}/file_framesetEvents.js`;
const PAGE_URL = `${BASE_PATH}/doc_networkEvents.html`;
const PAGE_JS_URL = `${BASE_PATH}/file_networkEvents.js`;

add_task(async function noEventsWhenNetworkDomainDisabled({ client }) {
  const history = configureHistory(client, 0);
  await loadURL(PAGE_URL);

  const events = await history.record();
  is(events.length, 0, "Expected no Network.responseReceived events");
});

add_task(async function noEventsAfterNetworkDomainDisabled({ client }) {
  const { Network } = client;

  const history = configureHistory(client, 0);
  await Network.enable();
  await Network.disable();
  await loadURL(PAGE_URL);

  const events = await history.record();
  is(events.length, 0, "Expected no Network.responseReceived events");
});

add_task(async function documentNavigationWithResource({ client }) {
  const { Page, Network } = client;

  await Network.enable();
  await Page.enable();

  const history = configureHistory(client, 4);

  const frameAttached = Page.frameAttached();
  const { frameId: frameIdNav } = await Page.navigate({ url: FRAMESET_URL });
  const { frameId: frameIdSubframe } = await frameAttached;
  ok(frameIdNav, "Page.navigate returned a frameId");

  info("Wait for Network events");
  const events = await history.record();
  is(events.length, 4, "Expected number of Network.responseReceived events");

  // Check top-level document response
  const docResponse = events[0].payload;
  is(docResponse.type, "Document", "Document response has expected type");
  is(docResponse.frameId, frameIdNav, "Got the expected frame id");
  is(
    docResponse.requestId,
    docResponse.loaderId,
    "The response id is equal to the loader id"
  );
  is(docResponse.response.url, FRAMESET_URL, "Got the Document response");
  is(
    docResponse.response.mimeType,
    "text/html",
    "Document response has expected mimeType"
  );
  ok(!!docResponse.response.headers.server, "Document response has headers");
  is(docResponse.response.status, 200, "Document response has expected status");
  is(
    docResponse.response.statusText,
    "OK",
    "Document response has expected status text"
  );
  if (docResponse.response.fromDiskCache === false) {
    is(
      docResponse.response.remoteIPAddress,
      "127.0.0.1",
      "Document response has the expected IP address"
    );
    Assert.equal(
      typeof docResponse.response.remotePort,
      "number",
      "Document response has a remotePort"
    );
  }
  is(
    docResponse.response.protocol,
    "http/1.1",
    "Document response has expected protocol"
  );

  // Check top-level script response
  const scriptResponse = events[1].payload;
  is(scriptResponse.type, "Script", "Script response has expected type");
  is(scriptResponse.frameId, frameIdNav, "Got the expected frame id");
  is(scriptResponse.response.url, FRAMESET_JS_URL, "Got the Script response");
  is(
    scriptResponse.response.mimeType,
    "application/x-javascript",
    "Script response has expected mimeType"
  );
  ok(!!scriptResponse.response.headers.server, "Script response has headers");
  is(
    scriptResponse.response.status,
    200,
    "Script response has the expected status"
  );
  is(
    scriptResponse.response.statusText,
    "OK",
    "Script response has the expected status text"
  );
  if (scriptResponse.response.fromDiskCache === false) {
    is(
      scriptResponse.response.remoteIPAddress,
      docResponse.response.remoteIPAddress,
      "Script response has same IP address as document response"
    );
    Assert.equal(
      typeof scriptResponse.response.remotePort,
      "number",
      "Script response has a remotePort"
    );
  }
  is(
    scriptResponse.response.protocol,
    "http/1.1",
    "Script response has the expected protocol"
  );

  // Check subdocument response
  const frameDocResponse = events[2].payload;
  is(
    frameDocResponse.type,
    "Subdocument",
    "Subdocument response has expected type"
  );
  is(frameDocResponse.frameId, frameIdSubframe, "Got the expected frame id");
  is(
    frameDocResponse.requestId,
    frameDocResponse.loaderId,
    "The response id is equal to the loader id"
  );
  is(
    frameDocResponse.response.url,
    PAGE_URL,
    "Got the expected Document response"
  );
  is(
    frameDocResponse.response.mimeType,
    "text/html",
    "Document response has expected mimeType"
  );
  ok(
    !!frameDocResponse.response.headers.server,
    "Subdocument response has headers"
  );
  is(
    frameDocResponse.response.status,
    200,
    "Subdocument response has expected status"
  );
  is(
    frameDocResponse.response.statusText,
    "OK",
    "Subdocument response has expected status text"
  );
  if (frameDocResponse.response.fromDiskCache === false) {
    is(
      frameDocResponse.response.remoteIPAddress,
      "127.0.0.1",
      "Subdocument response has the expected IP address"
    );
    Assert.equal(
      typeof frameDocResponse.response.remotePort,
      "number",
      "Subdocument response has a remotePort"
    );
  }
  is(
    frameDocResponse.response.protocol,
    "http/1.1",
    "Subdocument response has expected protocol"
  );

  // Check frame script response
  const frameScriptResponse = events[3].payload;
  is(frameScriptResponse.type, "Script", "Script response has expected type");
  is(frameScriptResponse.frameId, frameIdSubframe, "Got the expected frame id");
  is(frameScriptResponse.response.url, PAGE_JS_URL, "Got the Script response");
  is(
    frameScriptResponse.response.mimeType,
    "application/x-javascript",
    "Script response has expected mimeType"
  );
  ok(
    !!frameScriptResponse.response.headers.server,
    "Script response has headers"
  );
  is(
    frameScriptResponse.response.status,
    200,
    "Script response has the expected status"
  );
  is(
    frameScriptResponse.response.statusText,
    "OK",
    "Script response has the expected status text"
  );
  if (frameScriptResponse.response.fromDiskCache === false) {
    is(
      frameScriptResponse.response.remoteIPAddress,
      docResponse.response.remoteIPAddress,
      "Script response has same IP address as document response"
    );
    Assert.equal(
      typeof frameScriptResponse.response.remotePort,
      "number",
      "Script response has a remotePort"
    );
  }
  is(
    frameScriptResponse.response.protocol,
    "http/1.1",
    "Script response has the expected protocol"
  );
});

function configureHistory(client, total) {
  const RESPONSE = "Network.responseReceived";

  const { Network } = client;
  const history = new RecordEvents(total);

  history.addRecorder({
    event: Network.responseReceived,
    eventName: RESPONSE,
    messageFn: payload => {
      return `Received ${RESPONSE} for ${payload.response.url}`;
    },
  });
  return history;
}