kodokojo-mocks
v0.2.9
Published
Standalone mock server using Restify
Downloads
57
Readme
Kodokojo-mocks is an initiative from Kodokojo project
Standalone mocking server using Restify
Install
npm install kodokojo-mocks --save-dev
or
npm install -g kodokojo-mocks
Then create a folder to store your mock files
e.g: {your_project_path}/mocks
Create a config file
e.g: {your_project_path}/mocks_config.json
Config file example :
{
"port":8075,
"logs":false,
"prefix": "api/v1",
"path": "test/test_mocks",
"routes":[
{"path":"/user","method":"POST","mockType":"raw","serve":"0821b5c16a367e5df4044b183af3f0d18235d832"},
{"path":"/user/:id","method":"GET","mockType":"file","serve":"user.get.json"},
{"path":"/user","method":"PATCH","mockType":"func","serve":"user.patch.js"},
{"path":"/auth/:token","method":"GET","mockType":"func","serve":"auth.get.js"},
{"path":"/visits","method":"GET","mockType":"func","serve":"visits.get.js"}
],
"memoryStorage": false,
"persistStorage": true
}
port
(default: 8080) : Bind server to the specified port (eg: 8080)
logs
(default: true) : Enable request and error logging
prefix
(default: "") : Prefix path (eg: http://localhost:8080 /api/v1/...)
path
(default: "") : Relative path to mocks folder (from your project root folder, where your package.json was created)
routes
(default: []) : An array containing the routes you want to mock. ExpressJs format for parameters and paths
memoryStorage
(default: false) : Use memory only storage
persistStorage
(default: false) : Previous storage file is used if memoryStorage
is false else create a new empty storage file
Usage
Inside your module
Start
var MockServer = require('kodokojo-mocks');
var mockServer = new MockServer(__dirname+"/mocks_config.json");
mockServer.start().then(function(state){
console.log(state); // Should display : { ready: true }
console.log(server.config); // you can access to parsed configuration
});
Stop
// ... Server should be started
mockServer.stop();
CLI
Npm script
To run as a npm script. Add the CLI in your package.json
replace {config_file_path}
by your config file path.
{ ...
"scripts": {
"mockserver": "./node_modules/kodokojo-mocks/bin/mockserver.js {config_file_path}"
}
... }
Global installation
npm install -g kodokojo-mocks
Usage
replace {config_file_path}
by your config file path.
mockserver {config_file_path}
Have a look at ./test/index.js to see some examples.
Create your mocks
There is 3 ways to serve a mock : as you can see in the config file above, every mock have a type ("mockType" property) so you can define how the mock will be loaded.
Raw data
Return the value that you specified in your config file ("serve" property)
{... "mockType":"raw","serve":"[value_to_serve]"}
Json file content
Return the content of the given json file
{... "mockType":"file","serve":"[file_name]"}
The mock file should be a standard json file. You can write data from the incoming request content inside your json file by using mustach syntax, eg :
{ ...
"identifier": "{{req.id}}",
... }
{{req.NAME_OF_PROPERTY_FROM_REQ_OBJECT}}
ExpressJs controller
You can use a classic ExpressJs controller to serve your data, it's allow you to write some custom/business logic as a mock, eg :
{... "mockType":"func","serve":"[file_name]"}
Controller example:
exports.controller = function(req, res, next, server) {
// Your code here ...
// Classic ExpressJs api below
res.contentType = "application/json";
res.send(200, data_to_serve);
next();
};
Persistence
A file based (in-memory if memoryStorage
is true) key-value store is exposed as server.store
that can be
used to store and retrieve data in a custom controller.
exports.controller = function(req, res, next, server) {
store.get('visits', function (err, value) {
// visits = 0
store.put('visits', value+1, function (err) {
// visits = 1
});
});
res.contentType = "application/json";
res.send(200, data_to_serve);
next();
};
To do
Enhanced logs Rename type 'file' to 'json' rename type 'func' to 'controller' Npm script