feat(har): track address for api requests

This commit is contained in:
Simon Knott 2024-09-17 19:41:17 +02:00
parent 8761dafc73
commit fa2fff4bac
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 14 additions and 0 deletions

View file

@ -73,6 +73,8 @@ export type APIRequestFinishedEvent = {
statusMessage: string; statusMessage: string;
body?: Buffer; body?: Buffer;
timings: har.Timings; timings: har.Timings;
serverIPAddress?: string;
serverPort?: number;
}; };
type SendRequestOptions = https.RequestOptions & { type SendRequestOptions = https.RequestOptions & {
@ -302,6 +304,8 @@ export abstract class APIRequestContext extends SdkObject {
let tcpConnectionAt: number | undefined; let tcpConnectionAt: number | undefined;
let tlsHandshakeAt: number | undefined; let tlsHandshakeAt: number | undefined;
let requestFinishAt: number | undefined; let requestFinishAt: number | undefined;
let serverIPAddress: string | undefined;
let serverPort: number | undefined;
const request = requestConstructor(url, requestOptions as any, async response => { const request = requestConstructor(url, requestOptions as any, async response => {
const responseAt = monotonicTime(); const responseAt = monotonicTime();
@ -328,6 +332,8 @@ export abstract class APIRequestContext extends SdkObject {
cookies, cookies,
body, body,
timings, timings,
serverIPAddress,
serverPort,
}; };
this.emit(APIRequestContext.Events.RequestFinished, requestFinishedEvent); this.emit(APIRequestContext.Events.RequestFinished, requestFinishedEvent);
}; };
@ -483,6 +489,9 @@ export abstract class APIRequestContext extends SdkObject {
socket.on('lookup', () => { dnsLookupAt = monotonicTime(); }); socket.on('lookup', () => { dnsLookupAt = monotonicTime(); });
socket.on('connect', () => { tcpConnectionAt = monotonicTime(); }); socket.on('connect', () => { tcpConnectionAt = monotonicTime(); });
socket.on('secureConnect', () => { tlsHandshakeAt = monotonicTime(); }); socket.on('secureConnect', () => { tlsHandshakeAt = monotonicTime(); });
serverIPAddress = socket.remoteAddress;
serverPort = socket.remotePort;
}); });
request.on('finish', () => { requestFinishAt = monotonicTime(); }); request.on('finish', () => { requestFinishAt = monotonicTime(); });

View file

@ -208,6 +208,8 @@ export class HarTracer {
if (!harEntry) if (!harEntry)
return; return;
harEntry.serverIPAddress = event.serverIPAddress;
harEntry._serverPort = event.serverPort;
harEntry.response.status = event.statusCode; harEntry.response.status = event.statusCode;
harEntry.response.statusText = event.statusMessage; harEntry.response.statusText = event.statusMessage;
harEntry.response.httpVersion = event.httpVersion; harEntry.response.httpVersion = event.httpVersion;

View file

@ -831,6 +831,9 @@ it('should include API request', async ({ contextFactory, server }, testInfo) =>
ssl: expect.any(Number), ssl: expect.any(Number),
wait: expect.any(Number), wait: expect.any(Number),
})); }));
expect(entry.serverIPAddress).toBeDefined();
expect(entry._serverPort).toEqual(server.PORT);
}); });
it('should respect minimal mode for API Requests', async ({ contextFactory, server }, testInfo) => { it('should respect minimal mode for API Requests', async ({ contextFactory, server }, testInfo) => {