summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/network/browser_requestWillBeSent.js
blob: 9a4745952c45713a93a5c48268e209baba4b3db5 (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
/* 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_URL_JS = `${BASE_PATH}/file_framesetEvents.js`;
const PAGE_EMPTY_URL = `${BASE_PATH}/doc_empty.html`;
const PAGE_URL = `${BASE_PATH}/doc_networkEvents.html`;
const PAGE_EMPTY_HASH = `#`;
const PAGE_HASH = `#foo`;
const PAGE_URL_WITH_HASH = `${PAGE_URL}${PAGE_HASH}`;
const PAGE_URL_WITH_EMPTY_HASH = `${PAGE_URL}${PAGE_EMPTY_HASH}`;
const PAGE_URL_JS = `${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.requestWillBeSent events");

  // Check top-level document request
  const docRequest = events[0].payload;
  is(docRequest.type, "Document", "Document request has the expected type");
  is(docRequest.documentURL, FRAMESET_URL, "documentURL matches requested url");
  is(docRequest.frameId, frameIdNav, "Got the expected frame id");
  is(docRequest.request.url, FRAMESET_URL, "Got the Document request");
  is(docRequest.request.urlFragment, undefined, "Has no URL fragment set");
  is(docRequest.request.method, "GET", "Has the expected request method");
  is(
    docRequest.requestId,
    docRequest.loaderId,
    "The request id is equal to the loader id"
  );
  is(
    docRequest.request.headers.host,
    "example.com",
    "Document request has headers"
  );

  // Check top-level script request
  const scriptRequest = events[1].payload;
  is(scriptRequest.type, "Script", "Script request has the expected type");
  is(
    scriptRequest.documentURL,
    FRAMESET_URL,
    "documentURL is trigger document for the script request"
  );
  is(scriptRequest.frameId, frameIdNav, "Got the expected frame id");
  is(scriptRequest.request.url, FRAMESET_URL_JS, "Got the Script request");
  is(scriptRequest.request.method, "GET", "Has the expected request method");
  is(
    scriptRequest.request.headers.host,
    "example.com",
    "Script request has headers"
  );
  todo(
    scriptRequest.loaderId === docRequest.loaderId,
    "The same loaderId is used for dependent requests (Bug 1637838)"
  );
  assertEventOrder(events[0], events[1]);

  // Check subdocument request
  const subdocRequest = events[2].payload;
  is(
    subdocRequest.type,
    "Subdocument",
    "Subdocument request has the expected type"
  );
  is(subdocRequest.documentURL, FRAMESET_URL, "documenURL matches request url");
  is(subdocRequest.frameId, frameIdSubFrame, "Got the expected frame id");
  is(
    subdocRequest.requestId,
    subdocRequest.loaderId,
    "The request id is equal to the loader id"
  );
  is(subdocRequest.request.url, PAGE_URL, "Got the Subdocument request");
  is(subdocRequest.request.method, "GET", "Has the expected request method");
  is(
    subdocRequest.request.headers.host,
    "example.com",
    "Subdocument request has headers"
  );
  assertEventOrder(events[1], events[2]);

  // Check script request (frame)
  const subscriptRequest = events[3].payload;
  is(subscriptRequest.type, "Script", "Script request has the expected type");
  is(
    subscriptRequest.documentURL,
    PAGE_URL,
    "documentURL is trigger document for the script request"
  );
  is(subscriptRequest.frameId, frameIdSubFrame, "Got the expected frame id");
  todo(
    subscriptRequest.loaderId === docRequest.loaderId,
    "The same loaderId is used for dependent requests (Bug 1637838)"
  );
  is(subscriptRequest.request.url, PAGE_URL_JS, "Got the Script request");
  is(
    subscriptRequest.request.method,
    "GET",
    "Script request has the expected method"
  );
  is(
    subscriptRequest.request.headers.host,
    "example.com",
    "Script request has headers"
  );
  assertEventOrder(events[2], events[3]);
});

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

  await loadURL(PAGE_EMPTY_URL);

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

  const history = configureHistory(client, 4);

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

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

  // Check top-level document request only for fragment usage
  const docRequest = events[0].payload;
  is(docRequest.documentURL, PAGE_URL, "documentURL matches requested URL");
  is(docRequest.request.url, PAGE_URL, "Request url matches requested URL");
  is(
    docRequest.request.urlFragment,
    PAGE_HASH,
    "Request URL fragment is present"
  );
});

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

  await loadURL(PAGE_EMPTY_URL);

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

  const history = configureHistory(client, 4);

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

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

  // Check top-level document request only for fragment usage
  const docRequest = events[0].payload;
  is(docRequest.documentURL, PAGE_URL, "documentURL matches requested URL");
  is(docRequest.request.url, PAGE_URL, "Request url matches requested URL");
  is(
    docRequest.request.urlFragment,
    PAGE_EMPTY_HASH,
    "Request URL fragment is present"
  );
});

function configureHistory(client, total) {
  const REQUEST = "Network.requestWillBeSent";

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

  history.addRecorder({
    event: Network.requestWillBeSent,
    eventName: REQUEST,
    messageFn: payload => {
      return `Received ${REQUEST} for ${payload.request.url}`;
    },
  });

  return history;
}