Skip to content

Commit f5c0b22

Browse files
committed
fix(routeFromHar): address follow-up review feedback on interceptAPIRequests
- abort HAR lookup errors when notFound:'abort' (was silently falling through) - iterate a copy of the registrations to survive concurrent unroute - close the HAR last in HarRouter.dispose, after unrouting API registrations - look up LocalUtils via a typed DispatcherConnection.getDispatcher<T>() helper instead of an inline _dispatcherByGuid scan - assert response.serverAddr() in the intercepted-request test (record in 'full' mode so serverIPAddress/serverPort are persisted)
1 parent 9fab9ca commit f5c0b22

5 files changed

Lines changed: 18 additions & 9 deletions

File tree

‎packages/playwright-core/src/client/harRouter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ export class HarRouter {
136136
}
137137

138138
dispose() {
139-
this._localUtils.harClose({ harId: this._harId }).catch(() => {});
140139
for (const { context, registrationId } of this._apiRequestRegistrations)
141140
context._channel.unrouteAPIRequestsFromHar({ registrationId }).catch(() => {});
142141
this._apiRequestRegistrations = [];
142+
this._localUtils.harClose({ harId: this._harId }).catch(() => {});
143143
}
144144
}

‎packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
342342
// Reuse the HarBackend that was already opened via localUtils.harOpen for the page-side
343343
// route, rather than opening a second backend for the same HAR file. The backend is owned
344344
// by LocalUtils and closed via harClose, so this registration must not dispose it.
345-
const localUtils = [...this.connection._dispatcherByGuid.values()].find(d => d._type === 'LocalUtils') as LocalUtilsDispatcher | undefined;
346-
const harBackend = localUtils?.harBackendForId(params.harId);
345+
const harBackend = this.connection.getDispatcher<LocalUtilsDispatcher>('LocalUtils')?.harBackendForId(params.harId);
347346
if (!harBackend)
348347
throw new Error('Internal error: har was not opened');
349348
const urlMatch: URLMatch | undefined =

‎packages/playwright-core/src/server/dispatchers/dispatcher.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ export class DispatcherConnection {
263263
return this._dispatcherByObject.get(object) as DispatcherType | undefined;
264264
}
265265

266+
getDispatcher<DispatcherType>(type: string): DispatcherType | undefined {
267+
for (const dispatcher of this._dispatcherByGuid.values()) {
268+
if (dispatcher._type === type)
269+
return dispatcher as DispatcherType;
270+
}
271+
return undefined;
272+
}
273+
266274
registerDispatcher(dispatcher: DispatcherScope) {
267275
assert(!this._dispatcherByGuid.has(dispatcher._guid));
268276
this._dispatcherByGuid.set(dispatcher._guid, dispatcher);

‎packages/playwright-core/src/server/fetch.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,11 +709,9 @@ export class BrowserContextAPIRequestContext extends APIRequestContext {
709709
if (!urlMatches(registration.baseURL, urlString, registration.urlMatch))
710710
continue;
711711
const lookupResult = await progress.race(registration.harBackend.lookup(urlString, method, headersArray, postData, false, { apiRequestOnly: true, maxRedirects }));
712-
if (lookupResult.action === 'error') {
713-
fetchLog(`HAR: ${lookupResult.message ?? 'lookup failed'}`);
714-
continue;
715-
}
716-
if (lookupResult.action === 'noentry') {
712+
if (lookupResult.action === 'error' || lookupResult.action === 'noentry') {
713+
if (lookupResult.action === 'error')
714+
fetchLog(`HAR: ${lookupResult.message ?? 'lookup failed'}`);
717715
if (registration.notFound === 'abort')
718716
throw new Error(`Request "${method} ${urlString}" was not found in the HAR file`);
719717
continue;

‎tests/library/browsercontext-har.spec.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,8 @@ it.describe('interceptAPIRequests', () => {
867867

868868
const harPath = testInfo.outputPath('api.har');
869869
const context1 = await contextFactory();
870-
await context1.routeFromHAR(harPath, { update: true });
870+
// 'full' mode records serverIPAddress/serverPort; the default 'minimal' mode omits them.
871+
await context1.routeFromHAR(harPath, { update: true, updateMode: 'full' });
871872
const page1 = await context1.newPage();
872873
await page1.goto(server.EMPTY_PAGE);
873874
await page1.request.get(server.PREFIX + '/api/data');
@@ -880,6 +881,9 @@ it.describe('interceptAPIRequests', () => {
880881
const response = await page2.request.get(server.PREFIX + '/api/data');
881882
expect(response.status()).toBe(201);
882883
expect(response.statusText()).toBe('Created');
884+
const addr = await response.serverAddr();
885+
expect(addr!.ipAddress).toMatch(/127\.0\.0\.1|::1/);
886+
expect(addr!.port).toBe(server.PORT);
883887
});
884888

885889
it('should re-record intercepted APIRequestContext requests into a new HAR', async ({ contextFactory, server }, testInfo) => {

0 commit comments

Comments
 (0)