RequestHook Constructor
Initializes the request hook.
class TemplateRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventOptions) {
/* ... */
}
}
Parameter | Type | Description |
---|---|---|
requestFilterRules |
String | RegExp | Object | Predicate | Array | Specifies which requests the hook should handle. See Select Requests to be Handled by the Hook. |
responseEventOptions |
Object | The responseEventOptions.includeHeaders and responseEventOptions.includeBody properties indicate if the response’s headers and body should be passed to the onResponse method. |
Pass the requestFilterRules
and responseEventOptions
parameters to the base class constructor. This enables TestCafe to handle and apply these settings:
class MyRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventOptions) {
super(requestFilterRules, responseEventOptions);
}
}
Select Requests to be Handled by the Hook
Filter by URL
Specify the Exact URL
Pass a string with a URL to intercept all requests sent to this exact URL.
const logger = RequestLogger(['https://devexpress.github.io/testcafe/example/', 'http://localhost:8080']);
const mock = RequestMock()
.onRequestTo('http://external-service.com/api/')
.respond('Mocked respond');
Use a Regular Expression to Specify the URL
You can also specify a regular expression that matches the desired URLs.
const logger = RequestLogger(/example/);
const mock = RequestMock()
.onRequestTo(/\/api\/users\//)
.respond('Mocked respond');
Filter by Parameters
You can filter requests by combining the URL and the request method.
In this instance, you need to use an object that contains the following fields:
Property | Type | Description |
---|---|---|
url |
String | A URL to which a request is sent. |
method |
String | The request method. |
isAjax |
Boolean | Specifies whether this is an AJAX request. |
const logger = RequestLogger({ url: 'https://devexpress.github.io/testcafe/example/', method: 'GET', isAjax: false });
const mock = RequestMock()
.onRequestTo({ url: 'http://external-service.com/api/users/', method: 'POST', isAjax: true })
.respond((req, res) => {
res.headers['access-control-allow-origin'] = '*';
res.setBody(JSON.stringify('users'));
});
Filter with a Predicate
You can get more request parameters and determine whether to handle the request with the predicate functions.
const logger = RequestLogger(async request => {
return request.method === 'post' &&
request.headers['content-type'] === 'text/plain;charset=UTF-8';
});
const mock = RequestMock()
.onRequestTo(async request => {
return request.url === 'http://external-service.com/api/users/' &&
request.method === 'post' &&
request.isAjax &&
request.body.toString() === 'foo=bar';
})
.respond((req, res) => {
res.headers['access-control-allow-origin'] = '*';
res.setBody(JSON.stringify('users'));
});
This predicate takes the request
parameter that provides the following properties:
Property | Type | Description |
---|---|---|
userAgent |
String | Identifies the user agent that originated the request. Contains the formatted name and version of the browser and operating system (Firefox 69.0 / Windows 10.0.0 , Chrome 77.0.3865.120 / macOS 10.15.1 ). |
url |
String | The URL to which the request is sent. |
method |
String | The request method. |
isAjax |
Boolean | Specifies whether this is an AJAX request. |
headers |
Object | The request headers in the property-value form. |
body |
String | A stringified request body. |