RequestMock.onRequestTo().respond Method
Specifies the mock response.
requestMock.onRequestTo().respond([body] [, statusCode] [, headers]) → this
Parameter | Type | Description | Default |
---|---|---|---|
body (optional) |
Object | String | Function | Buffer | A mocked response body. Pass an object for a JSON response, a string for an HTML response or a function to build a custom response. | An empty HTML page is returned with the response. |
statusCode (optional) |
Number | The response status code. | 200 |
headers (optional) |
Object | Custom headers added to the response in the property-value form. | The content-type header. If the header is not provided, it is set depending on the body parameter’s type. If body is an object, the content-type header is set to application/json. If body has another type, the content-type header is set to text/html; charset=utf-8. |
const mock = RequestMock()
.onRequestTo('https://devexpress.github.io/testcafe/example/json')
.respond({ data: 123 }) // a JSON response
.onRequestTo('https://devexpress.github.io/testcafe/example/html')
.respond('<html></html>') // an HTML response
.onRequestTo('https://devexpress.github.io/testcafe/example/empty')
.respond(null, 204) // an empty response with a status code
.onRequestTo('https://devexpress.github.io/testcafe/example/headers')
.respond('<html_markup>', 200, { // a response with custom headers
'server': 'nginx/1.10.3',
})
.onRequestTo('https://devexpress.github.io/testcafe/example/binary')
.respond(Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])) // a response with binary data
.onRequestTo(/\/custom\?.*/)
.respond((req, res) => { // a custom response
res.headers['x-calculated-header'] = 'calculated-value';
res.statusCode = '200';
const parsedUrl = url.parse(req.url, true);
res.setBody('calculated body ' + parsedUrl.query['param']);
});
A Custom Response Function
const templateMock = RequestMock()
.onRequestTo(/\*...\*/)
.respond((req, res) => {
// ...
});
A custom response function takes two parameters.
Parameter | Type | Description |
---|---|---|
req |
RequestOptions | A request to be mocked. |
res |
Object | A mocked response. |
Use information provided by the req
parameter about the request to configure the response using the res
parameter.
The res
exposes the following members:
Property | Type | Description |
---|---|---|
headers |
Object | The response headers. |
statusCode |
Number | The response status code. |
Method | Description |
---|---|
setBody(value) |
Sets the response body. Accepts a string as a parameter. |
The response function can be synchronous or asynchronous:
const mock = RequestMock()
.onRequestTo('https://devexpress.github.io/testcafe/example/page')
.respond((req, res) => {
res.setBody('<html><body><h1>This is a page</h1></body></html>');
})
.onRequestTo('https://devexpress.github.io/testcafe/example/todo')
.respond(async (req, res) => {
const data = await promisifiedHttpsGet('https://jsonplaceholder.typicode.com/todos/1');
res.headers['access-control-allow-origin'] = '*';
res.setBody(data.body);
});
RequestOptions
The RequestOptions
object contains the request parameters.
Property | Type | Description |
---|---|---|
headers |
Object | The request headers in the property-value form. |
body |
Buffer | The request body. |
url |
String | The URL to which the request is sent. |
protocol |
String | The protocol to use. Default: http:. |
hostname |
String | The alias for the host. |
host |
String | The domain name or IP address of the server to issue the request to. Default: localhost. |
port |
Number | The port of the remote server. Default: 80. |
path |
String | The request path. Should include query string if any. E.G. ‘/index.html?page=12’. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future. Default: ‘/‘. |
method |
String | The string specifying the HTTP request method. Default: ‘GET’. |
credentials |
Object | Credentials to use for Windows (NTLM) or Basic authentication. HTTP Basic authentication requires a username and a password . NTLM authentication services may need to know your workstation and domain . See HTTP Authentication. |
proxy |
Object | If a proxy is used, the property contains information about its host , hostname , port , proxyAuth , authHeader and bypassRules . |