/*
In project directory:
- npm install robohydra
- create file ./my_config.conf (content of file below)
{
"plugins": [
{
"name": "some_name",
"config": {}
}
]
}
- create directory structure for a custom plugin: ./robohydra/plugins/some_name/index.js (content of file below)
- notice "some_name" in plugin directory path matches content inside "my_config.conf"
- run `robohydra -p 3000 my_config.conf` to start RoboHydra server (port 3000 is the default so can be left out)
*/
// Content of ./robohydra/plugins/some_name/index.js
var robohydra = require('robohydra');
var Server = robohydra.heads.RoboHydraHead;
var StaticServer = robohydra.heads.RoboHydraHeadStatic;
var Response = robohydra.Response // used specifically by RoboHydraHead
exports.getBodyParts = function(config) {
return {
// Each "head" represents an endpoint
heads: [
new Server({
path: '/foobar',
// We need to manually write JSON
// compared to the StaticServer version (see below example)
handler: function(req, res) {
// Allow AJAX requests across domains
res.headers['Access-Control-Allow-Origin'] = '*';
res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With';
res.statusCode = 200;
res.send('{ "foo": 1, "bar": 2 }');
}
})
],
// "scenarios" are like categories
// you need to enable scenarios manually via admin area
// http://localhost:3000/robohydra-admin/scenarios/
scenarios: {
// a category I called "example" (it could be called anything you like)
// that has same endpoint as the above "default"
// but serves slightly different content
example: {
heads: [
new StaticServer({
path: '/foobar',
// Specifying an object for the content means
// RoboHydra will automatically send back JSON
content: {
foo: 3,
bar: 4
}
})
]
},
// a category I called "failure" (it could be called anything you like)
failure: {
heads: [
new StaticServer({
path: '/.*',
content: 'Unhandled exception of some kind (fake)',
statusCode: 500
})
]
},
// a category I called "timeout" (it could be called anything you like)
timeout: {
heads: [
new Server({
path: '/slow',
handler: function(req, res) {
// Allow AJAX requests across domains
res.headers['Access-Control-Allow-Origin'] = '*';
res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With';
// Mimic a slow connection
setTimeout(function() {
res.statusCode = 504;
res.send('The server did not receive a timely response (fake)');
}, 5000);
}
}),
new Server({
path: '/slower/:milliseconds',
handler: function(req, res) {
// Allow AJAX requests across domains
res.headers['Access-Control-Allow-Origin'] = '*';
res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With';
// User determines how slow the connection is
// http://localhost:3000/slower/10000
setTimeout(function() {
res.statusCode = 504;
res.send('The server did not receive a timely response (fake)');
}, req.params.milliseconds);
}
})
]
}
}
};
};