Skip to content

Commit

Permalink
fix: expose the override data to the request events (#2241)
Browse files Browse the repository at this point in the history
This PR make sure that updates via interception to the Request are
properly show in the `request` property of `responseStarted` and
`responseCompleted` events.
This should only be applicable to the request part as we get the update
data from the `responseRecived` CDP events.
  • Loading branch information
Lightning00Blade authored May 27, 2024
1 parent 8c8590f commit 947bb8e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 423 deletions.
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

0 comments on commit 947bb8e

Please sign in to comment.