more events

This commit is contained in:
Simon Knott 2025-01-27 17:01:35 +01:00
parent 75a8ace20e
commit bcf1240818
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 17 additions and 5 deletions

View file

@ -18,7 +18,7 @@ import { MockingProxy } from '../mockingProxy';
import type { RootDispatcher } from './dispatcher';
import { Dispatcher, existingDispatcher } from './dispatcher';
import type * as channels from '@protocol/channels';
import { APIRequestContextDispatcher, RequestDispatcher, RouteDispatcher } from './networkDispatchers';
import { APIRequestContextDispatcher, RequestDispatcher, ResponseDispatcher, RouteDispatcher } from './networkDispatchers';
import type { Request, Route } from '../network';
import { urlMatches } from '../../utils/isomorphic/urlMatch';
@ -43,6 +43,20 @@ export class MockingProxyDispatcher extends Dispatcher<MockingProxy, channels.Mo
this.addObjectListener(MockingProxy.Events.Request, ({ request, correlation }: { request: Request, correlation?: string }) => {
this._dispatchEvent('request', { request: RequestDispatcher.from(this as any, request), correlation });
});
this.addObjectListener(MockingProxy.Events.RequestFailed, (request: Request) => {
this._dispatchEvent('requestFailed', {
request: RequestDispatcher.from(this as any, request),
failureText: request._failureText ?? undefined,
responseEndTiming: request._responseEndTiming,
});
});
this.addObjectListener(MockingProxy.Events.RequestFinished, (request: Request) => {
this._dispatchEvent('requestFinished', {
request: RequestDispatcher.from(this as any, request),
response: ResponseDispatcher.fromNullable(this as any, request._existingResponse()),
responseEndTiming: request._responseEndTiming,
});
});
}
async setInterceptionPatterns(params: channels.MockingProxySetInterceptionPatternsParams, metadata?: CallMetadata): Promise<channels.MockingProxySetInterceptionPatternsResult> {

View file

@ -170,7 +170,7 @@ export class MockingProxy extends SdkObject implements RequestContext {
response.setTransferSize(transferSize);
response.setEncodedBodySize(encodedBodySize);
response.setResponseHeadersSize(transferSize - encodedBodySize);
this.emit(MockingProxy.Events.RequestFinished, { request, response });
this.emit(MockingProxy.Events.RequestFinished, request);
resolve();
} catch (error) {
request._setFailureText('' + error);

View file

@ -134,11 +134,9 @@ test('all properties are populated', async ({ runInlineTest, server, request })
expect(await response.headersArray()).toContainEqual({ name: 'foo', value: 'bar' });
expect(await response.body()).toEqual(Buffer.from('fallback'));
// TODO: implement, this currently blocks because requestFinished isn't emitted
// expect(await response.finished()).toBe(null);
expect(await response.finished()).toBe(null);
expect(request.serviceWorker()).toBe(null);
expect(request.frame()).not.toBe(null);
expect(response.frame()).not.toBe(null);
expect(request.failure()).toBe(null);
expect(request.isNavigationRequest()).toBe(false);