chore(webdriver): scaffold HTTPRequest.respond() (#12147)

This commit is contained in:
Nikolay Vitkov 2024-03-26 08:57:49 +01:00 committed by GitHub
parent 2cadcff06e
commit cd88110456
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 10 deletions

View File

@ -10,9 +10,14 @@ import type {
ContinueRequestOverrides,
ResponseForRequest,
} from '../api/HTTPRequest.js';
import {HTTPRequest, type ResourceType} from '../api/HTTPRequest.js';
import {
HTTPRequest,
STATUS_TEXTS,
type ResourceType,
} from '../api/HTTPRequest.js';
import {PageEvent} from '../api/Page.js';
import {UnsupportedOperation} from '../common/Errors.js';
import {isString} from '../common/util.js';
import type {Request} from './core/Request.js';
import type {BidiFrame} from './Frame.js';
@ -173,9 +178,60 @@ export class BidiHTTPRequest extends HTTPRequest {
}
override async respond(
_response: Partial<ResponseForRequest>,
response: Partial<ResponseForRequest>,
_priority?: number
): Promise<void> {
throw new UnsupportedOperation();
const responseBody: Buffer | null =
response.body && isString(response.body)
? Buffer.from(response.body)
: (response.body as Buffer) || null;
const headers: Bidi.Network.Header[] = [];
let hasContentLength = false;
for (const [name, value] of Object.entries(response.headers ?? [])) {
if (name.toLocaleLowerCase() === 'content-length') {
hasContentLength = true;
}
headers.push({
name: name.toLowerCase(),
value: {
type: 'string',
value: String(value),
},
});
}
if (response.contentType) {
headers.push({
name: 'content-type',
value: {
type: 'string',
value: response.contentType,
},
});
}
if (responseBody && !hasContentLength) {
headers.push({
name: 'content-length',
value: {
type: 'string',
value: String(Buffer.byteLength(responseBody)),
},
});
}
const status = response.status || 200;
return await this.#request.provideResponse({
statusCode: status,
headers: headers.length > 0 ? headers : undefined,
reasonPhrase: STATUS_TEXTS[status],
body: responseBody
? {
type: 'base64',
value: responseBody.toString('base64'),
}
: undefined,
});
}
}

View File

@ -166,6 +166,10 @@ export interface Commands {
params: Bidi.Network.FailRequestParameters;
returnType: Bidi.EmptyResult;
};
'network.provideResponse': {
params: Bidi.Network.ProvideResponseParameters;
returnType: Bidi.EmptyResult;
};
}
/**

View File

@ -169,6 +169,28 @@ export class Request extends EventEmitter<{
});
}
async provideResponse({
statusCode,
reasonPhrase,
headers,
body,
}: Omit<Bidi.Network.ProvideResponseParameters, 'request'>): Promise<void> {
if (!this.#event.isBlocked) {
throw new Error('Request Interception is not enabled!');
}
// Request interception is not supported for data: urls.
if (this.url.startsWith('data:')) {
return;
}
await this.#session.send('network.provideResponse', {
request: this.id,
statusCode,
reasonPhrase,
headers,
body,
});
}
@inertIfDisposed
private dispose(): void {
this[disposeSymbol]();

View File

@ -637,13 +637,6 @@
"expectations": ["SKIP"],
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
},
{
"testIdPattern": "[oopif.spec] OOPIF should report google.com frame",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["FAIL", "TIMEOUT"],
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
},
{
"testIdPattern": "[page.spec] Page Page.addScriptTag should throw when added with content to the CSP page",
"platforms": ["darwin", "linux", "win32"],
@ -2942,6 +2935,13 @@
"expectations": ["SKIP"],
"comment": "Failed previously and currently times out"
},
{
"testIdPattern": "[oopif.spec] OOPIF should report google.com frame",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["FAIL", "TIMEOUT"],
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
},
{
"testIdPattern": "[oopif.spec] OOPIF should support lazy OOP frames",
"platforms": ["darwin", "linux", "win32"],