Reference
Please find below all attributes that can be configured on a Stubr scenario.
Attribute | Type | required | Description |
---|---|---|---|
group | string | no | Groups help structuring scenarios. Group is exposed via Stubr UI and as response header. |
name | string | yes | Unique descriptive name of scenario. Name is exposed via Stubr UI and as response header. |
route | string | yes | Route for which scenario should match. It can also contain dynamic path segments (e.g. /my/{dynamic}/route ) |
method | enum | yes | Determines the http method for which scenario should match. One of: GET , POST , PUT , PATCH , DELETE , OPTIONS , HEAD |
delay | number | no | Introduces an artificial delay before the response is sent. This is helpful to e.g. simulate time consuming backend activities. |
validate | boolean | function | no | If defined as function, the function optionally receives requestHeaders , requestBody and requestParams as parameters and is supposed to return a boolean if defined. If the function returns true , the scenario is considered to be matched and thus used to resolve the response. |
responseCode | number | yes | Http response code to be returned with response (e.g. 200 indicating a default success or 400 indicating a bad request) |
responseHeaders | object | function | no | Can either be an object or a function. The function optionally receives requestHeaders , requestBody and requestParams as parameters to determine dynamic headers to be returned. |
responseFilePath | string | no | Defines the path to a static file to be exposed as response. The path needs to be a string and is interpreted relative to the script file, which defines the register() function. The file type of referenced file is being used to auto populate the Content-Type response header. If responseFilePath is specified, then a potentially additionally defined responseBody would be ignored. |
responseBody | object | function | no | Can either be an object or a function. The function optionally receives requestHeaders , requestBody and requestParams as parameters to determine a dynamic response body to be returned. |
Example:
stubr.register({
// scenarios can be grouped (optional)
group: "My Group",
// required
name: "Scenario 1",
// required
route: "/my/first/route",
// GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
method: Method.GET,
// delay response (optional)
delay: 2000,
// headers, parameters and body are passed to provide context for validation
validate: (requestHeaders, requestBody, requestParams) => {
return requestHeaders.Authorization !== undefined; // disallow unauthorized requests
},
// http response code
responseCode: 200,
// optionally you can receive headers and body to construct dynamic response headers based on request
responseHeaders: (requestHeaders, requestBody, requestParams) => {
"X-Custom-Header-Attribute": "abc"
},
// optionally you can receive headers and body to construct dynamic response body based on request
responseBody: (requestHeaders, requestBody, requestParams) => {
data: "json response"
}
});
stubr.register({
// scenarios can be grouped (optional)
group: "My Group",
// required
name: "Scenario 1",
// required
route: "/my/first/route",
// GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
method: "GET",
// delay response (optional)
delay: 2000,
// headers, parameters and body are passed to provide context for validation
validate: (requestHeaders, requestBody, requestParams) => {
return requestHeaders.Authorization !== undefined; // disallow unauthorized requests
},
// http response code
responseCode: 200,
// optionally you can receive headers and body to construct dynamic response headers based on request
responseHeaders: (requestHeaders, requestBody, requestParams) => {
"X-Custom-Header-Attribute": "abc"
},
// optionally you can receive headers and body to construct dynamic response body based on request
responseBody: (requestHeaders, requestBody, requestParams) => {
data: "json response"
}
});
Updated almost 3 years ago