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: get the correct status from last response extra info #2128

Merged
merged 3 commits into from
Apr 16, 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: 59 additions & 2 deletions src/bidiMapper/modules/network/NetworkModuleMocks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ export class MockCdpNetworkEvents {
initiator: {type: 'other'},
redirectHasExtraInfo: true,
redirectResponse: {
url: this.url,
url: `${this.url}/redirect`,
status: 302,
statusText: 'Found',
headers: {
Connection: 'keep-alive',
Date: 'Tue, 02 Apr 2024 13:07:22 GMT',
'Transfer-Encoding': 'chunked',
location: '/redirect',
location: this.url,
},
mimeType: '',
charset: '',
Expand Down Expand Up @@ -242,6 +242,26 @@ export class MockCdpNetworkEvents {
});
}

responseReceivedExtraInfoRedirect() {
this.cdpClient.emit('Network.responseReceivedExtraInfo', {
requestId: this.requestId,
blockedCookies: [],
headers: {
Connection: 'keep-alive',
Date: 'Mon, 15 Apr 2024 11:53:20 GMT',
'Transfer-Encoding': 'chunked',
location: this.url,
},
resourceIPAddressSpace: 'Local',
statusCode: 302,
headersText:
'HTTP/1.1 302 Found\r\nlocation: http://localhost:37363/empty.html\r\nDate: Mon, 15 Apr 2024 11:53:20 GMT\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n',
cookiePartitionKey: 'http://localhost',
cookiePartitionKeyOpaque: false,
exemptedCookies: [],
});
}

responseReceived(hasExtraInfo = false) {
this.cdpClient.emit('Network.responseReceived', {
requestId: this.requestId,
Expand Down Expand Up @@ -383,6 +403,43 @@ export class MockCdpNetworkEvents {
canceled: false,
});
}

setJsonEvent(json: string | Record<string, unknown>, _normalize = false) {
const event = json instanceof Object ? json : JSON.parse(json);

const replaceKeys = [
['requestId', this.requestId],
['networkId', this.requestId],
['loaderId', this.loaderId],
['frameId', this.frameId],
['url', this.url],
] as const;
for (const [key, value] of replaceKeys) {
this.findAndReplaceKey(event, key, value);
}

this.cdpClient.emit(event.method, event.params);
}

findAndReplaceKey(
source: Record<string, unknown>,
searchKey: string,
value: unknown
): void {
for (const key of Object.keys(source)) {
if (key === searchKey) {
source[key] = value;
return;
}
if (typeof source[key] === 'object' && !Array.isArray(source[key])) {
this.findAndReplaceKey(
source[key] as Record<string, unknown>,
searchKey,
value
);
}
}
}
}

export class MockCdpClient extends EventEmitter<CdpEvents> {
Expand Down
12 changes: 12 additions & 0 deletions src/bidiMapper/modules/network/NetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ export class NetworkRequest {
onResponseReceivedExtraInfoEvent(
event: Protocol.Network.ResponseReceivedExtraInfoEvent
) {
if (
event.statusCode >= 300 &&
event.statusCode <= 399 &&
this.#request.info &&
event.headers['location'] === this.#request.info.request.url
) {
// We received the Response Extra info for the redirect
// Too late so we need to skip it as it will
// fire wrongly for the last one
return;
}
this.#response.extraInfo = event;
this.#emitEventsIfReady();
}
Expand Down Expand Up @@ -500,6 +511,7 @@ export class NetworkRequest {
return;
}
this.#phaseChanged();

this.#emittedEvents[event.method] = true;
this.#eventManager.registerEvent(
Object.assign(event, {
Expand Down
19 changes: 18 additions & 1 deletion src/bidiMapper/modules/network/NetworkStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ describe('NetworkStorage', () => {

request.authRequired();
const event = await getEvent('network.authRequired');
event;
expect(event).to.deep.nested.include({
'request.request': request.fetchId,
'request.method': 'GET',
Expand Down Expand Up @@ -389,4 +388,22 @@ describe('NetworkStorage', () => {
expect(event).to.exist;
});
});

describe('redirects', () => {
it('should return 200 from last response', async () => {
const request = new MockCdpNetworkEvents(cdpClient);

request.requestWillBeSentRedirect();
request.responseReceivedExtraInfoRedirect();
request.requestWillBeSentExtraInfo();
request.responseReceived();
request.responseReceivedExtraInfo();

const event = await getEvent('network.responseCompleted');

expect(event).to.deep.nested.include({
'response.status': 200,
});
});
});
});
1 change: 1 addition & 0 deletions src/bidiMapper/modules/network/NetworkStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class NetworkStorage {
'Network.requestWillBeSent',
(params: Protocol.Network.RequestWillBeSentEvent) => {
const request = this.getRequestById(params.requestId);

if (request && request.isRedirecting()) {
request.handleRedirect(params);
this.deleteRequest(params.requestId);
Expand Down
Loading