Overview
NativeAutomationRequestHookEventProvider and RequestPausedEventBasedEventFactory implement the request hook lifecycle for TestCafe's native automation mode, translating CDP Fetch.RequestPaused events into the hammerhead request-hook event model so that user-defined RequestHook subclasses (like RequestLogger and RequestMock) work transparently.
Description
These two classes bridge the gap between the CDP Fetch domain and TestCafe's request hook API:
- NativeAutomationRequestHookEventProvider (
src/native-automation/request-hooks/event-provider.ts, 93 lines) extends hammerhead's RequestHookEventProvider. Its onRequest method is called when a request is paused at the request stage; it delegates to the pipeline context's onRequestHookRequest. Its onResponse method is called when the same request is paused at the response stage; it updates the event factory with the response data, runs onRequestHookConfigureResponse (allowing hooks to modify headers), optionally fetches the response body if any hook requested includeBody, and then fires all registered onResponse event-data callbacks. It returns a boolean indicating whether response headers were modified (so the pipeline knows to re-fulfil the request).
- RequestPausedEventBasedEventFactory (
src/native-automation/request-hooks/event-factory/request-paused-event-based.ts, 129 lines) extends hammerhead's BaseRequestHookEventFactory. It wraps a CDP RequestPausedEvent and produces the standard hammerhead event objects: createRequestInfo() builds a RequestInfo from the CDP request, createRequestOptions() builds RequestOptions with parsed URL components, createConfigureResponseEvent() creates a ConfigureResponseEvent with setHeader/removeHeader modifier functions that mutate the response headers in-place, and createResponseInfo() builds a ResponseInfo from the CDP response data plus the buffered response body.
Usage
These classes are used internally by the NativeAutomationRequestPipeline. When a test registers request hooks (e.g., RequestLogger, RequestMock), the pipeline calls the event provider's onRequest and onResponse methods at the appropriate Fetch interception points. The event factory is created per-request and stored in the pipeline's context info map.
Code Reference
Source Location
Signature
// NativeAutomationRequestHookEventProvider
// (src/native-automation/request-hooks/event-provider.ts)
export default class NativeAutomationRequestHookEventProvider extends RequestHookEventProvider {
public async onRequest (
event: RequestPausedEvent,
contextInfo: NativeAutomationRequestContextInfo,
): Promise<void>;
public async onResponse (
event: RequestPausedEvent,
resourceBody: Buffer | null,
contextInfo: NativeAutomationRequestContextInfo,
client: ProtocolApi,
): Promise<boolean>;
}
// RequestPausedEventBasedEventFactory
// (src/native-automation/request-hooks/event-factory/request-paused-event-based.ts)
export default class RequestPausedEventBasedEventFactory extends BaseRequestHookEventFactory {
public headersModified: boolean;
public constructor (event: RequestPausedEvent, sessionId: string);
public update (event: RequestPausedEvent): void;
public setResponseBody (body: Buffer): void;
public createRequestInfo (): RequestInfo;
public createRequestOptions (): RequestOptions;
public createConfigureResponseEvent (rule: RequestFilterRule): ConfigureResponseEvent;
public createResponseInfo (): ResponseInfo;
}
Import
import NativeAutomationRequestHookEventProvider from '../native-automation/request-hooks/event-provider';
import RequestPausedEventBasedEventFactory from '../native-automation/request-hooks/event-factory/request-paused-event-based';
I/O Contract
NativeAutomationRequestHookEventProvider.onRequest
| Parameter |
Type |
Description
|
event |
RequestPausedEvent |
CDP Fetch paused event at the request stage
|
contextInfo |
NativeAutomationRequestContextInfo |
Maps request IDs to their pipeline context and event factory
|
| Return Type |
Description
|
Promise<void> |
Request hooks' onRequest handlers are invoked; no-op if no listeners
|
NativeAutomationRequestHookEventProvider.onResponse
| Parameter |
Type |
Description
|
event |
RequestPausedEvent |
CDP Fetch paused event at the response stage
|
resourceBody |
Buffer | null |
Pre-fetched response body, or null if not yet retrieved
|
contextInfo |
NativeAutomationRequestContextInfo |
Context map for the request
|
client |
ProtocolApi |
CDP client for fetching response body on demand
|
| Return Type |
Description
|
Promise<boolean> |
true if any hook modified the response headers, false otherwise
|
RequestPausedEventBasedEventFactory Constructor
| Parameter |
Type |
Description
|
event |
RequestPausedEvent |
The initial CDP request-paused event
|
sessionId |
string |
Current CDP session identifier
|
RequestPausedEventBasedEventFactory Outputs
| Method |
Return Type |
Description
|
createRequestInfo() |
RequestInfo |
Hammerhead request info with URL, method, headers, body, isAjax, userAgent
|
createRequestOptions() |
RequestOptions |
Parsed request options including protocol, hostname, port, path, auth
|
createConfigureResponseEvent(rule) |
ConfigureResponseEvent |
Event with setHeader/removeHeader functions for response modification
|
createResponseInfo() |
ResponseInfo |
Response info with statusCode, headers, body, sessionId, requestId
|
Usage Examples
import NativeAutomationRequestHookEventProvider from '../request-hooks/event-provider';
import RequestPausedEventBasedEventFactory from '../request-hooks/event-factory/request-paused-event-based';
// Create the event provider (typically done once per request pipeline)
const hookEventProvider = new NativeAutomationRequestHookEventProvider();
// Register request hooks (done by the test framework)
hookEventProvider.addRequestEventListeners(requestFilterRule, {
onRequest: requestHandler,
onConfigureResponse: configureResponseHandler,
onResponse: responseHandler,
});
// When a request is paused at the request stage
await hookEventProvider.onRequest(requestPausedEvent, contextInfo);
// When the same request is paused at the response stage
const headersModified = await hookEventProvider.onResponse(
responsePausedEvent,
null, // body will be fetched on demand if needed
contextInfo,
cdpClient,
);
if (headersModified) {
// Re-fulfil with modified headers
}
// The event factory is used internally to create hammerhead-compatible events
const factory = new RequestPausedEventBasedEventFactory(pausedEvent, sessionId);
const requestInfo = factory.createRequestInfo();
const responseInfo = factory.createResponseInfo();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.