Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expose the override data to the request events #2241

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions src/bidiMapper/modules/network/NetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
computeHeadersSize,
bidiNetworkHeadersFromCdpNetworkHeaders,
cdpToBiDiCookie,
bidiNetworkHeadersFromCdpNetworkHeadersEntries,
} from './NetworkUtils.js';

const REALM_REGEX = /(?<=realm=").*(?=")/;
Expand Down Expand Up @@ -72,6 +73,7 @@ export class NetworkRequest {
info?: Protocol.Network.RequestWillBeSentEvent;
extraInfo?: Protocol.Network.RequestWillBeSentExtraInfoEvent;
paused?: Protocol.Fetch.RequestPausedEvent;
overrides?: Omit<Protocol.Fetch.ContinueRequestRequest, 'requestId'>;
auth?: Protocol.Fetch.AuthRequiredEvent;
} = {};

Expand Down Expand Up @@ -136,6 +138,7 @@ export class NetworkRequest {
const url =
this.#response.info?.url ??
this.#response.paused?.request.url ??
this.#request.overrides?.url ??
this.#request.auth?.request.url ??
this.#request.info?.request.url ??
this.#request.paused?.request.url ??
Expand All @@ -146,6 +149,7 @@ export class NetworkRequest {

get method(): string {
return (
this.#request.overrides?.method ??
this.#request.info?.request.method ??
this.#request.paused?.request.method ??
this.#request.auth?.request.method ??
Expand Down Expand Up @@ -405,21 +409,24 @@ export class NetworkRequest {
}

/** @see https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#method-continueRequest */
async continueRequest({
url,
method,
headers,
postData,
}: Omit<Protocol.Fetch.ContinueRequestRequest, 'requestId'> = {}) {
async continueRequest(
overrides: Omit<Protocol.Fetch.ContinueRequestRequest, 'requestId'> = {}
) {
assert(this.#fetchId, 'Network Interception not set-up.');

await this.cdpClient.sendCommand('Fetch.continueRequest', {
requestId: this.#fetchId,
url,
method,
headers,
postData,
url: overrides.url,
method: overrides.method,
headers: overrides.headers,
postData: overrides.postData,
});
// TODO: Store postData's size only
this.#request.overrides = {
url: overrides.url,
method: overrides.method,
headers: overrides.headers,
};
this.#interceptPhase = undefined;
}

Expand Down Expand Up @@ -560,10 +567,16 @@ export class NetworkRequest {
this.#response.extraInfo = undefined;
}

// TODO: get headers from Fetch.requestPaused
const headers = bidiNetworkHeadersFromCdpNetworkHeaders(
this.#response.info?.headers
);
const headers = [
...bidiNetworkHeadersFromCdpNetworkHeaders(this.#response.info?.headers),
...bidiNetworkHeadersFromCdpNetworkHeaders(
this.#response.extraInfo?.headers
),
// TODO: Verify how to dedupe these
// ...bidiNetworkHeadersFromCdpNetworkHeadersEntries(
// this.#response.paused?.responseHeaders
// ),
];

// TODO: get headers from Fetch.requestPaused
const authChallenges = this.#authChallenges(
Expand Down Expand Up @@ -615,9 +628,23 @@ export class NetworkRequest {
? NetworkRequest.#getCookies(this.#request.extraInfo.associatedCookies)
: [];

const headers = bidiNetworkHeadersFromCdpNetworkHeaders(
this.#request.info?.request.headers
);
let headers: Network.Header[] = [];
if (this.#request.overrides?.headers) {
headers = [
...bidiNetworkHeadersFromCdpNetworkHeadersEntries(
this.#request.overrides?.headers
),
];
} else {
headers = [
...bidiNetworkHeadersFromCdpNetworkHeaders(
this.#request.info?.request.headers
),
...bidiNetworkHeadersFromCdpNetworkHeaders(
this.#request.extraInfo?.headers
),
];
}

return {
request: this.#id,
Expand Down
19 changes: 18 additions & 1 deletion src/bidiMapper/modules/network/NetworkUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function computeHeadersSize(headers: Network.Header[]): number {
return new TextEncoder().encode(requestHeaders).length;
}

/** Converts from CDP Network domain headers to Bidi network headers. */
/** Converts from CDP Network domain headers to BiDi network headers. */
export function bidiNetworkHeadersFromCdpNetworkHeaders(
headers?: Protocol.Network.Headers
): Network.Header[] {
Expand All @@ -52,6 +52,23 @@ export function bidiNetworkHeadersFromCdpNetworkHeaders(
}));
}

/** Converts from CDP Fetch domain headers to BiDi network headers. */
export function bidiNetworkHeadersFromCdpNetworkHeadersEntries(
headers?: Protocol.Fetch.HeaderEntry[]
): Network.Header[] {
if (!headers) {
return [];
}

return headers.map(({name, value}) => ({
name,
value: {
type: 'string',
value,
},
}));
}

/** Converts from Bidi network headers to CDP Network domain headers. */
export function cdpNetworkHeadersFromBidiNetworkHeaders(
headers?: Network.Header[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
[headers.py]
[test_modify_headers[request_headers1-modified_headers1\]]
expected: FAIL

[test_modify_headers[request_headers2-modified_headers2\]]
expected: FAIL

[test_modify_headers[request_headers3-modified_headers3\]]
expected: FAIL

[test_override_cookies]
expected: FAIL

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
[headers.py]
[test_modify_headers[request_headers1-modified_headers1\]]
expected: FAIL

[test_modify_headers[request_headers2-modified_headers2\]]
expected: FAIL

[test_modify_headers[request_headers3-modified_headers3\]]
expected: FAIL

[test_override_cookies]
expected: FAIL
Loading
Loading