connect-chaotic-response v1.0.3
Connect Chaotic Response
A lightweight connect/express middleware that plugs chaotic http behaviour into your server
Chaotic is intended for anyone who needs to test scenarios where his server might become flaky in the best case, non-responsive in the worst case and anything between. It enables to plug (in a configurable fashion) random http errors and timeouts into any node server that uses connect/express (or connect/express like) HTTP server framework.
By default Chaotic will run in the optimistic mode that for 99% of requests to your server, will do nothing special for successful responses (http 2xx codes) and http 3xx responses. 1% of your server requests will be "hijacked" by the Chaotic middleware and will randomly generate error responses (http 4xx or 5xx codes) or a timed out (successful) response. For other, more "interesting" ;) modes, see the Configuration section.
Installation
npm install connect-chaotic-response --saveUsage as an express/connect middleware
const express = require('connect');
// get the Chaotic response module
const ChaoticResponse = require('connect-chaotic-response');
const app = connect();
// Create a new chaoticResponse, optionaly with options
const chaoticResponse = new ChaoticResponse(options);
// wire your app with the Chaoutic middleware
app.use(chaoticResponse.middleware);
app.listen(3000);Configuration
Chaotic supports these modes:
optimistic- 99% - normal, 0.5% - 401, 0.1% - 429, 0.1% - 500, 0.1% - 503, 0.1% - 504, 0.1% - 7 seconds (by default) timeoutpessimistic- 50% - normal, 5% - 401, 5% - 429, 10% - 500, 10% - 503, 10% - 504, 10% - 7 seconds (by default) timeouttimeout- 1% - normal, 1% - 401, 6% - 429, 1% - 500, 1% - 503, 10% - 504, 80% - 7 seconds (by default) timeoutfailure- 1% - normal, 1% - 401, 5% - 429, 40% - 500, 40% - 503, 10% - 504, 3% - 7 seconds (by default) timeout
To set a specific mode:
// Create a new chaoticResponse, with the 'pessimistic' mode
const chaoticResponse = new ChaoticResponse({mode: 'pessimistic'});
// wire your app with the Chaoutic middleware
app.use(chaoticResponse.middleware);You could also change the mode sometime later in your program by calling ChaoticResponse.setMode(mode);.
Options
The chaoticResponse constructor accepts an optional object with these options:
mode- As explained above. Supports optimistic, pessimistic, timeout, failuretimeout- The timeout in milliseconds for timed out responsescustomMode- Enables to create a personal chaotic mode that consists of any (allowed) http responses and their related weights.customModeaccepts an object of two arrays:responsesandweightsthat represent the desired mix of the server's flaky behaviour. For a the full list of allowed http codes see the responses list. If thecustomModeoption is provided together with themodeoption, Chaotic will ignore themodeoption and use thecustomModebehaviour.
An example of using customMode + changing default timeout:
// Set a custom mode and a 10 seconds timeout
const options = {
customMode: {
responses: [200, 201, 409, 500, 0],
weights: [5, 5, 2, 2, 1]
},
timeout: 10000
};
// Create a new chaoticResponse, with the above options
const chaoticResponse = new ChaoticResponse(options);
// wire your app with the Chaoutic middleware
app.use(chaoticResponse.middleware);Callback for error responses
By default the middleware doesn't call next() for an error response and simply returns an error. If you require to
run a function that is fired whenever an error occurs, you can add you callback function by setting the ChaoticResponse.callbackOnError.
Contributing
- Fork it
- Install dependencies
npm install - Ensure
npm testandnpm run lintrun successfully - Submit a pull request