From 7d538f90f6011258d8db54c9c2438529f7c404aa Mon Sep 17 00:00:00 2001 From: Dakuan Date: Wed, 28 Jan 2015 19:05:21 +0000 Subject: [PATCH] different flavour of chaos on each request --- example/index.js | 13 +++++++++++++ index.js | 15 ++++++++++----- package.json | 2 +- try.js | 9 --------- 4 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 example/index.js delete mode 100644 try.js diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000..3e99292 --- /dev/null +++ b/example/index.js @@ -0,0 +1,13 @@ +var express = require('express'), + app = express(), + chaos = require('../index'); + +app.use(chaos()); + +app.get('/', function(req, res, next) { + res.send('CHAOS: hello from example, it looks like this request got through'); +}); + +app.listen(3009, function() { + console.log('booted chos example app on 3009'); +}); diff --git a/index.js b/index.js index f460304..2a9f66b 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ function _truthy(val) { // calls a method on a object with the args // flipped to accept the object last -var _flipFunc = R.curry(function(method, args, obj){ +var _flipFunc = R.curry(function(method, args, obj) { return obj[method].apply(null, args); }); @@ -28,12 +28,17 @@ var _handlersForOptions = R.curry(function(handlers, opts) { // full list of built handlers var allHandlers = R.map(R.func('factory'), handlers); -// find the relevent handlers for the provided options and build them -// if no options are provided, all handlers are built -// after that, pick a random one and return it -var chaos = R.compose( +// pick the handlers that match the args +var pickFromArgs = R.compose( _randomElement, R.ifElse(_truthy, _handlersForOptions(handlers), R.always(allHandlers)) ); +var chaos = function(opts) { + console.log('CHAOS: Running in CHAOS MODE. Requests will be delayed, error or worse...'); + return function (req, res, next) { + return pickFromArgs(opts).apply(null, arguments); + } +}; + module.exports = chaos; diff --git a/package.json b/package.json index 87e196b..e2d5cb2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "connect-chaos", - "version": "0.0.8", + "version": "0.1.0", "description": "connect / express middleware that causes chaos", "main": "index.js", "scripts": { diff --git a/try.js b/try.js deleted file mode 100644 index b6acdb7..0000000 --- a/try.js +++ /dev/null @@ -1,9 +0,0 @@ -var chaos = require('./index'); - -chaos({ - error: 400, - delay: 100 -})(null, { - status: function() {}, - end: function() {} -}, function() {});