UNPKG

3.9 kBMarkdownView Raw
1# YesMan
2Diff based a **mocking server** built on express.js. It is designed for use with RESTful json APIs.
3
4## Overview
5
6Each mocked service has a json template file which defines the structure of the response. Diffs or deltas are then applied to the template to produce the desired response. Both the template and the diffs are pure json.
7
8## Install
9```shell
10npm install -g yesman
11```
12
13## Create a yesman app
14```shell
15yesman init
16```
17
18## Start the yesman server
19```shell
20yesman start <app-path>
21```
22
23## Help
24```shell
25yesman help
26```
27
28## Services / URLs
29All available urls are defined in the services.json file. Each url points to a folder which contains the template, config and diffs for that service:
30```json
31{
32 "services" : {
33 "/customer" : "<customer-dir>"
34 , "/product" : "<product-dir>"
35 , "/currencies" : "<currencies-dir>"
36 },
37 .
38 .
39}
40```
41
42## Forwards
43Services can also be configured to forward to an external url:
44```json
45{
46 .
47 .
48 "forwards" : {
49 "/countries" : "<external-url>"
50 }
51}
52```
53
54## Template
55A template is pure json and is named <service-name>-config.json. A simple service can function with just a template file and no config file or diff files (see the currencies service). In this case, the template itself is returned unmodified.
56
57For more complex services, multiple diffs can be applied to the template to produce the desired output. See [json-cascade](https://www.npmjs.com/package/json-cascade) for more on how templates work.
58
59## Config
60Services are configured in the <servicedir>/<servicename>-config.json file. The available configuration values are:
61
62- `verbs` - a list of HTTP request methods for this service eg `["GET", "POST"]`
63- `template` - path to the template file relative to the templates folder eg `/customer.json`
64- `reqParser` - the name of a function in `req-parsers.js` that will parse the request body and produce a diff to be applied to the template eg the `mirror` function returns the request body itself.
65- `mockGenerator` - the name of a function in `dummy-data-generator.js` that will dynamically generate a diff eg timestamps, random number generators etc
66- `diff` - path to a custom diff file.
67- `states` - list of available states. Each state may override the service level config options: `template`, `reqParser`, `mockGenerator`, `diff`
68
69Any values that are not set explicitly will be defaulted based on the file default-service-config.json.
70
71## Use with Grunt
72Yesman can be started via Grunt using the [grunt-express-server](https://github.com/ericclemmons/grunt-express-server) plugin. It can also be restarted automatically as your code changes using the [grunt-contrib-watch](https://github.com/gruntjs/grunt-contrib-watch) plugin. For an example, see [Gruntfile.js](https://github.com/KevinSheedy/YesMan/blob/master/Gruntfile.js)
73
74
75Yesman combines data from **4 sources** to build a mock json response.
76
77- **Template** - Defines the structure of the response and some default values.
78- **HttpRequest** - Values can be picked from the request and echoed back in the response.
79- **Generator** - Values can be generated dynamically eg timestamps
80- **State** - Each service can be put into a particular state. For example, for a customer lookup service, the state could be set to: CUSTOMER_FOUND, CUSTOMER_NOT_FOUND, UNAUTHORIZED, SERVER_ERROR
81
82
83## States
84Each service can be put into various states to mimic the behaviour of a stateful application (eg a database). State information is stored as a **diff** in .json format in the [diffs](diffs) folder.
85
86
87## Scenarios
88A **scenario** is a list of services and their current states. This allows us to define the behaviour for a particular sequence of service calls. See [scenarios.json](scenarios.json). The scenario under test is set in [config.json](config.json).
\No newline at end of file