Implementation:DevExpress Testcafe RequestHook
| Knowledge Sources | |
|---|---|
| Domains | Testing, Network Interception |
| Principle | Request hook interception pattern |
| Last Updated | 2026-02-12 12:00 GMT |
Overview
RequestHook is the abstract base class that all TestCafe request hook implementations (such as RequestLogger and RequestMock) extend in order to intercept HTTP requests and responses during test execution.
Description
The RequestHook class, defined in src/api/request-hooks/hook.ts, provides the foundational hook-lifecycle contract for intercepting network traffic routed through the testcafe-hammerhead proxy. It accepts an optional set of RequestFilterRule instances (or initializers) that determine which requests the hook should match, plus optional ConfigureResponseEventOptions that control whether response headers and/or body are forwarded to the hook.
Each instance receives a unique id (generated via generateUniqueId from testcafe-hammerhead), and stores a reference to a WarningLog that is injected at runtime. Concrete subclasses must override the onRequest and onResponse methods; the base class throws a RequestHookNotImplementedMethodError if either is called without being overridden.
The private helper _prepareRules normalizes the filter-rule input: an empty array yields no rules, undefined or omitted input yields RequestFilterRule.ANY (match everything), and all other inputs are converted via RequestFilterRule.fromArray.
Usage
You do not instantiate RequestHook directly. Instead, extend it when building a custom request hook that needs to observe or modify HTTP traffic during tests. Built-in subclasses include RequestLogger (logging) and RequestMock (response mocking). Attach hooks to tests or fixtures using the .requestHooks() API method.
Code Reference
Source Location
- Repository: DevExpress_Testcafe
- File: src/api/request-hooks/hook.ts
- Lines: 1-54
Signature
export default abstract class RequestHook {
public _requestFilterRules: RequestFilterRule[];
public readonly _responseEventConfigureOpts?: ConfigureResponseEventOptions;
public _warningLog: WarningLog | null;
public readonly id: string;
public _className: string;
protected constructor (
ruleInit?: RequestFilterRuleInit | RequestFilterRuleInit[],
responseEventConfigureOpts?: ConfigureResponseEventOptions
);
private _prepareRules (ruleInit?: RequestFilterRuleInit | RequestFilterRuleInit[]): RequestFilterRule[];
public async onRequest (event: RequestEvent): Promise<void>;
public async _onConfigureResponse (event: ConfigureResponseEvent): Promise<void>;
public async onResponse (event: ResponseEvent): Promise<void>;
}
Import
import RequestHook from './hook';
// or from the package root:
// import { RequestHook } from 'testcafe';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ruleInit | RequestFilterRuleInit[] | No | One or more filter-rule initializers specifying which URLs/requests to intercept. If omitted or empty, RequestFilterRule.ANY is used (matches all requests).
|
| responseEventConfigureOpts | ConfigureResponseEventOptions |
No | Options controlling whether the response event includes headers and/or body data. |
Outputs
| Name | Type | Description |
|---|---|---|
| id | string |
A unique identifier generated for each hook instance via generateUniqueId().
|
| _requestFilterRules | RequestFilterRule[] |
The normalized array of filter rules the hook will match against. |
| _responseEventConfigureOpts | undefined | The stored response-event configuration options. |
| _warningLog | null | Reference to the warning log, injected at runtime (initially null).
|
| _className | string |
The constructor name of the concrete subclass, used in error messages. |
Usage Examples
import { RequestHook } from 'testcafe';
class CustomRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventConfigureOpts) {
super(requestFilterRules, responseEventConfigureOpts);
}
async onRequest (event) {
// Inspect or modify the outgoing request
console.log(`Request to: ${event._requestInfo.url}`);
}
async onResponse (event) {
// Inspect the response
console.log(`Response status: ${event.statusCode}`);
}
}
// Attach to a fixture
fixture `My Fixture`
.page `https://example.com`
.requestHooks(new CustomRequestHook(/api\/data/));
Related Pages
Note: This is an orphan Implementation — request hook interception pattern is an internal API with no dedicated Principle page.