Skip to content

Commit 3a7d629

Browse files
authored
chore(rpc): pass more network tests (#2762)
1 parent 0963c19 commit 3a7d629

14 files changed

+79
-44
lines changed

src/chromium/crNetworkManager.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,12 @@ class InterceptableRequest implements network.RouteDelegate {
361361
});
362362
}
363363

364-
async fulfill(response: types.FulfillResponse) {
364+
async fulfill(response: types.NormalizedFulfillResponse) {
365365
const responseBody = response.body && helper.isString(response.body) ? Buffer.from(response.body) : (response.body || null);
366366

367367
const responseHeaders: { [s: string]: string; } = {};
368-
if (response.headers) {
369-
for (const header of Object.keys(response.headers))
370-
responseHeaders[header.toLowerCase()] = response.headers[header];
371-
}
368+
for (const header of Object.keys(response.headers))
369+
responseHeaders[header.toLowerCase()] = response.headers[header];
372370
if (response.contentType)
373371
responseHeaders['content-type'] = response.contentType;
374372
if (responseBody && !('content-length' in responseHeaders))
@@ -378,8 +376,8 @@ class InterceptableRequest implements network.RouteDelegate {
378376
// or the page was closed. We should tolerate these errors.
379377
await this._client._sendMayFail('Fetch.fulfillRequest', {
380378
requestId: this._interceptionId!,
381-
responseCode: response.status || 200,
382-
responsePhrase: network.STATUS_TEXTS[String(response.status || 200)],
379+
responseCode: response.status,
380+
responsePhrase: network.STATUS_TEXTS[String(response.status)],
383381
responseHeaders: headersArray(responseHeaders),
384382
body: responseBody ? responseBody.toString('base64') : undefined,
385383
});

src/firefox/ffNetworkManager.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,21 @@ class InterceptableRequest implements network.RouteDelegate {
172172
});
173173
}
174174

175-
async fulfill(response: types.FulfillResponse) {
175+
async fulfill(response: types.NormalizedFulfillResponse) {
176176
const responseBody = response.body && helper.isString(response.body) ? Buffer.from(response.body) : (response.body || null);
177177

178178
const responseHeaders: { [s: string]: string; } = {};
179-
if (response.headers) {
180-
for (const header of Object.keys(response.headers))
181-
responseHeaders[header.toLowerCase()] = response.headers[header];
182-
}
179+
for (const header of Object.keys(response.headers))
180+
responseHeaders[header.toLowerCase()] = response.headers[header];
183181
if (response.contentType)
184182
responseHeaders['content-type'] = response.contentType;
185183
if (responseBody && !('content-length' in responseHeaders))
186184
responseHeaders['content-length'] = String(Buffer.byteLength(responseBody));
187185

188186
await this._session.sendMayFail('Network.fulfillInterceptedRequest', {
189187
requestId: this._id,
190-
status: response.status || 200,
191-
statusText: network.STATUS_TEXTS[String(response.status || 200)] || '',
188+
status: response.status,
189+
statusText: network.STATUS_TEXTS[String(response.status)] || '',
192190
headers: headersArray(responseHeaders),
193191
base64body: responseBody ? responseBody.toString('base64') : undefined,
194192
});

src/network.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17-
import * as fs from 'fs';
18-
import * as mime from 'mime';
19-
import * as util from 'util';
2017
import * as frames from './frames';
2118
import * as types from './types';
2219
import { assert, helper } from './helper';
2320
import { URLSearchParams } from 'url';
21+
import { normalizeFulfillParameters } from './rpc/serializers';
2422

2523
export function filterCookies(cookies: types.NetworkCookie[], urls: string[]): types.NetworkCookie[] {
2624
const parsedURLs = urls.map(s => new URL(s));
@@ -220,15 +218,7 @@ export class Route {
220218
async fulfill(response: types.FulfillResponse & { path?: string }) {
221219
assert(!this._handled, 'Route is already handled!');
222220
this._handled = true;
223-
if (response.path) {
224-
response = {
225-
status: response.status,
226-
headers: response.headers,
227-
contentType: mime.getType(response.path) || 'application/octet-stream',
228-
body: await util.promisify(fs.readFile)(response.path)
229-
};
230-
}
231-
await this._delegate.fulfill(response);
221+
await this._delegate.fulfill(await normalizeFulfillParameters(response));
232222
}
233223

234224
async continue(overrides: { method?: string; headers?: types.Headers; postData?: string } = {}) {
@@ -325,7 +315,7 @@ export class Response {
325315

326316
export interface RouteDelegate {
327317
abort(errorCode: string): Promise<void>;
328-
fulfill(response: types.FulfillResponse): Promise<void>;
318+
fulfill(response: types.NormalizedFulfillResponse): Promise<void>;
329319
continue(overrides: { method?: string; headers?: types.Headers; postData?: string; }): Promise<void>;
330320
}
331321

src/rpc/channels.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import { EventEmitter } from 'events';
1818
import * as types from '../types';
1919

20+
export type Binary = string;
21+
2022
export interface Channel extends EventEmitter {
2123
_type: string;
2224
_guid: string;
@@ -101,7 +103,7 @@ export interface PageChannel extends Channel {
101103
goForward(params: { options?: types.NavigateOptions }): Promise<ResponseChannel | null>;
102104
opener(): Promise<PageChannel | null>;
103105
reload(params: { options?: types.NavigateOptions }): Promise<ResponseChannel | null>;
104-
screenshot(params: { options?: types.ScreenshotOptions }): Promise<string>;
106+
screenshot(params: { options?: types.ScreenshotOptions }): Promise<Binary>;
105107
setExtraHTTPHeaders(params: { headers: types.Headers }): Promise<void>;
106108
setNetworkInterceptionEnabled(params: { enabled: boolean }): Promise<void>;
107109
setViewportSize(params: { viewportSize: types.Size }): Promise<void>;
@@ -199,7 +201,7 @@ export interface ElementHandleChannel extends JSHandleChannel {
199201
press(params: { key: string; options?: { delay?: number } & types.TimeoutOptions & { noWaitAfter?: boolean } }): Promise<void>;
200202
querySelector(params: { selector: string }): Promise<ElementHandleChannel | null>;
201203
querySelectorAll(params: { selector: string }): Promise<ElementHandleChannel[]>;
202-
screenshot(params: { options?: types.ElementScreenshotOptions }): Promise<string>;
204+
screenshot(params: { options?: types.ElementScreenshotOptions }): Promise<Binary>;
203205
scrollIntoViewIfNeeded(params: { options?: types.TimeoutOptions }): Promise<void>;
204206
selectOption(params: { values: string | ElementHandleChannel | types.SelectOption | string[] | ElementHandleChannel[] | types.SelectOption[] | null; options?: types.NavigatingActionWaitOptions }): string[] | Promise<string[]>;
205207
selectText(params: { options?: types.TimeoutOptions }): Promise<void>;
@@ -228,7 +230,14 @@ export type RequestInitializer = {
228230
export interface RouteChannel extends Channel {
229231
abort(params: { errorCode: string }): Promise<void>;
230232
continue(params: { overrides: { method?: string, headers?: types.Headers, postData?: string } }): Promise<void>;
231-
fulfill(params: { response: types.FulfillResponse & { path?: string } }): Promise<void>;
233+
fulfill(params: {
234+
response: {
235+
status?: number,
236+
headers?: types.Headers,
237+
contentType?: string,
238+
body: Binary,
239+
}
240+
}): Promise<void>;
232241
}
233242
export type RouteInitializer = {
234243
request: RequestChannel,

src/rpc/client/browser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
5555
const context = await this.newContext(options);
5656
const page = await context.newPage();
5757
page._ownedContext = context;
58+
context._ownerPage = page;
5859
return page;
5960
}
6061

src/rpc/client/browserContext.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
3434
readonly _bindings = new Map<string, frames.FunctionWithSource>();
3535
private _pendingWaitForEvents = new Map<(error: Error) => void, string>();
3636
_timeoutSettings = new TimeoutSettings();
37+
_ownerPage: Page | undefined;
3738

3839
static from(context: BrowserContextChannel): BrowserContext {
3940
return context._object;
@@ -91,6 +92,8 @@ export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserC
9192
}
9293

9394
async newPage(): Promise<Page> {
95+
if (this._ownerPage)
96+
throw new Error('Please use browser.newContext()');
9497
return Page.from(await this._channel.newPage());
9598
}
9699

src/rpc/client/jsHandle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class JSHandle<T = any> extends ChannelOwner<JSHandleChannel, JSHandleIni
8282
}
8383

8484
async jsonValue(): Promise<T> {
85-
return await this._channel.jsonValue();
85+
return parseResult(await this._channel.jsonValue());
8686
}
8787

8888
asElement(): ElementHandle | null {

src/rpc/client/network.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { RequestChannel, ResponseChannel, RouteChannel, RequestInitializer, Resp
2020
import { ChannelOwner } from './channelOwner';
2121
import { Frame } from './frame';
2222
import { Connection } from '../connection';
23+
import { normalizeFulfillParameters } from '../serializers';
2324

2425
export type NetworkCookie = {
2526
name: string,
@@ -150,7 +151,13 @@ export class Route extends ChannelOwner<RouteChannel, RouteInitializer> {
150151
}
151152

152153
async fulfill(response: types.FulfillResponse & { path?: string }) {
153-
await this._channel.fulfill({ response });
154+
const normalized = await normalizeFulfillParameters(response);
155+
await this._channel.fulfill({ response: {
156+
status: normalized.status,
157+
headers: normalized.headers,
158+
contentType: normalized.contentType,
159+
body: (typeof normalized.body === 'string' ? Buffer.from(normalized.body) : normalized.body).toString('base64')
160+
}});
154161
}
155162

156163
async continue(overrides: { method?: string; headers?: types.Headers; postData?: string } = {}) {

src/rpc/client/page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export class Page extends ChannelOwner<PageChannel, PageInitializer> {
334334
}
335335

336336
async setViewportSize(viewportSize: types.Size) {
337+
this._viewportSize = viewportSize;
337338
await this._channel.setViewportSize({ viewportSize });
338339
}
339340

src/rpc/serializers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,20 @@ export async function normalizeFilePayloads(files: string | types.FilePayload |
6262
}
6363
return filePayloads;
6464
}
65+
66+
export async function normalizeFulfillParameters(params: types.FulfillResponse & { path?: string }): Promise<types.NormalizedFulfillResponse> {
67+
if (params.path) {
68+
return {
69+
status: params.status || 200,
70+
headers: params.headers || {},
71+
contentType: mime.getType(params.path) || 'application/octet-stream',
72+
body: await util.promisify(fs.readFile)(params.path)
73+
};
74+
}
75+
return {
76+
status: params.status || 200,
77+
headers: params.headers || {},
78+
contentType: params.contentType,
79+
body: params.body || ''
80+
};
81+
}

0 commit comments

Comments
 (0)