You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 28, 2022. It is now read-only.
Mocks server with extensible fixtures groupables in predefined behaviors. Behavior can be changed using built-in CLI or REST API.
10
10
11
-
## Table of contents
12
-
13
-
-[Getting started](#getting-started)
14
-
-[Installation](#installation)
15
-
-[Usage](#usage)
16
-
- [Interactive CLI](#interactive-cli)
17
-
-[Rest Api](#rest-api)
18
-
- [Programmatic usage](#programmatic-usage)
19
-
- [CLI](#cli)
20
-
- [Server](#server)
21
-
- [Global usage](#global-usage)
22
-
-[Options](#Options)
23
-
-[Defining mocks](#defining-mocks)
24
-
25
11
## Getting Started
26
12
27
-
This package provides a server that simulates API behaviors. As input, it needs "fixtures", which are responses for specific uris, and "features", which are sets of "fixtures".
13
+
This package provides a server that simulates API behaviors. As input, it needs "fixtures", which are responses for specific uris, and "behaviors", which are sets of "fixtures".
28
14
29
-
It also provide a built-in CLI and a REST API which allows to change the currently used "feature" in any moment simply making an http request.
15
+
It also provide a built-in CLI and a REST API which allows to change the currently used "behavior" in any moment simply making an http request.
30
16
31
17
## Installation
32
18
@@ -36,13 +22,11 @@ npm i @mocks-server/main --save-dev
36
22
37
23
## Usage
38
24
39
-
### Interactive CLI
40
-
41
25
Add an script to your `package.json` file, including the path to your mocks folder:
@@ -52,166 +36,63 @@ Now, you can start the mocks server CLI simply typing:
52
36
npm run mocks-server
53
37
```
54
38
55
-

56
-
57
-
### REST API
58
-
59
-
The server includes a REST API that allows to change dinamically the current feature, change delay time, etc.
60
-
61
-
Available api resources are:
62
-
63
-
*`GET``/mocks/features` Returns an array containing all available features.
64
-
*`GET``/mocks/features/current` Returns current feature.
65
-
*`PUT``/mocks/features/current` Set current feature.
66
-
* Request body example: `{ "name": "feature-name" }`
67
-
*`GET``/mocks/settings` Return current server settings.
68
-
* Response body example: `{ "delay": 0 }`
69
-
*`PUT``/mocks/settings` Change current server settings.
70
-
* Request body example: `{ "delay": 3000 }`
71
-
72
-
### Programmatic usage
73
-
74
-
#### CLI
75
-
76
-
The interactive CLI can be instantiated and started programmatically:
77
-
78
-
```js
79
-
const { Cli } =require("@mocks-server/main");
80
-
81
-
conststartMyCli= () => {
82
-
constcli=newCli({
83
-
port:3200,
84
-
log:"debug",
85
-
watch:false
86
-
});
87
-
88
-
returncli.start();
89
-
};
90
-
91
-
startMyCli().catch(err=> {
92
-
console.log("Error starting CLI", err);
93
-
});
94
-
```
95
-
96
-
##### `Cli` (\[options\]\[,customQuitMethod\])
97
-
For first argument options, please read the [options](#options) chapter of this documentation. Available methods of an instance are:
98
-
-`start` ()
99
-
Inits the server in case it was stopped, adds the watch listeners, and renders main menu.
100
-
-`initServer` ()
101
-
Inits the server in case it was stopped, adds the watch listeners.
102
-
-`stopListeningServerWatch` ()
103
-
When server watch is active, the main menu will be displayed on file changes. This behavior can be deactivated using this method. This is useful when this CLI is loaded as a submenu of another CLI, for example.
104
-
105
-
#### Server
106
-
107
-
The server can be instantiated and started programmatically:
First argument is mandatory, and has to be a path to a folder containing "features". All files in the folder will be loaded recursively, including subfolders.
130
-
For second argument options, please read the [options](#options) chapter of this documentation.
131
-
132
-
Available methods of an instance are:
133
-
134
-
-`start` (). Starts the server.
135
-
-`stop` (). Stops the server.
136
-
-`restart` (). Stops the server, initializes it again (reloading features files), and starts it again.
137
-
-`switchWatch` (state `<Boolean>`). Enable or disable features files watch, depending of the received "state" value.
138
-
139
-
Available getters are:
140
-
141
-
-`features`. Returns loaded features object.
142
-
-`watchEnabled`. Current state of the features files watcher.
143
-
-`error`. When server has returned an error, or an error ocurred loading features, it is available in this property.
144
-
-`events`. Returns server events object. A "watch-reload" event is emitted when the server watch detects changes in any features file, and restarts the server.
145
-
146
-
### Global usage
147
-
148
-
The mocks server can be used as a global dependency as well:
149
-
150
-
```bash
151
-
npm i @mocks-server/main -g
152
-
```
153
-
154
-
Now, you can start the built-in command line interface from anywhere, providing a path to a features folder:
155
-
156
-
```bash
157
-
mocks-server --features=./path-to-features
158
-
```
39
+

159
40
160
41
## Options
161
42
162
43
* port `<Number>` Por number for the Server to be listening.
163
44
* host `<String>` Host for the server. Default is "0.0.0.0" (Listen to any local host).
164
45
* log `<String>` Logs level. Can be one of "silly", "debug", "verbose", "info", "warn", "error".
165
-
* watch `<Boolean>` Watch features folder, and restart server on changes. Default is `true`.
166
-
*feature`<String>` Selected feature when server is started.
46
+
* watch `<Boolean>` Watch behaviors folder, and restart server on changes. Default is `true`.
47
+
*behavior`<String>` Selected behavior when server is started.
167
48
* delay `<Number` Responses delay time in milliseconds.
168
-
*features`Path as <String>` Path to a folder containing features to be used by the server.
169
-
* recursive `<Boolean>` Load features recursively. Watch is not affected by this option, it is always recursive.
49
+
*behaviors`Path as <String>` Path to a folder containing behaviors to be used by the server.
50
+
* recursive `<Boolean>` Load behaviors recursively. Watch is not affected by this option, it is always recursive.
170
51
* cli `<Boolean>` Start interactive CLI. Default is `true`.
171
52
172
53
## Defining mocks
173
54
174
55
The Mocks server handles two main concepts for defining mocks:
175
56
176
-
### Features
57
+
### Behaviors
177
58
178
-
Each feature consists in a set of "fixtures", which are server responses for specific uris.
59
+
Each behavior consists in a set of "fixtures", which are server responses for specific uris.
179
60
180
-
Features are extensibles, so, you can have a "base" feature, which defines the standard behavior of the mocks server and responses for all api uris, and change this behavior creating new features that changes only responses for certain "uris". All features are extensible as well.
61
+
Behaviors are extensibles, so, you can have a "base" behavior, which defines the standard behavior of the mocks server and responses for all api uris, and change this behavior creating new behaviors that changes only responses for certain "uris". All extended behaviors are extensible as well.
181
62
182
-
For creating a Feature, you have to use the mocks-server "Feature" class, providing an array of "fixtures" to it:
63
+
For creating a Behavior, you have to use the mocks-server "Behavior" class, providing an array of "fixtures" to it:
Now, when loaded, the server will have available a "myFeature" feature, which contains two fixtures. You can add more features extending the first one and changing only the response for "uri_2", for example:
79
+
Now, when loaded, the server will have available a "myBehavior" behavior, which contains two fixtures. You can add more behaviors extending the first one and changing only the response for "uri_2", for example:
Now, server will have available "myFeature" and "myFeature2" features. And "myFeature2" will send a different response only for "uri_2" (supossing that "uri_2_fixture" and "uri_2_different_fixture" were defined with the same uri)
95
+
Now, server will have available "myBehavior" and "myBehavior2" behaviors. And "myBehavior2" will send a different response only for "uri_2" (supossing that "uri_2_fixture" and "uri_2_different_fixture" were defined with the same uri)
215
96
216
97
### Fixtures
217
98
@@ -220,13 +101,14 @@ A "fixture" defines the response for an specific uri. It has to be an object con
220
101
* url `uri as <String>` Uri of the resource. It can contains expressions for matching dynamic uris. Read the [route-parser](https://www.npmjs.com/package/route-parser) documentation for further info about how to use dynamic routing.
221
102
* method `<String>` Method of the request. Defines to which method will response this fixture. Valid values are http request methods, such as "GET", "POST", "PUT", etc.
222
103
* response `<Object>` Defines the response that the Mocks Server will send to the request:
223
-
* status `<Number>` Status code to send.
224
-
* body `<Object>` Json object to send as body in the response.
104
+
* status `<Number>` Status code to send.
105
+
* body `<Object>` Json object to send as body in the response.
225
106
* response `<Function>` Response can be defined as a function too. The function will receive the [express](http://expressjs.com/es/api.html)`request`, `response` and `next` arguments, so you are free to handle the server request as you need.
226
107
227
108
```js
228
109
// Fixtures file
229
110
111
+
// fixtures with static responses
230
112
consturi_1_fixture= {
231
113
url:"/api/foo-uri",
232
114
method:"GET",
@@ -248,6 +130,7 @@ const uri_2_fixture = {
248
130
}
249
131
};
250
132
133
+
// fixture with a dynamic response
251
134
consturi_2_different_fixture= {
252
135
url:"/api/foo-uri-2/:id",
253
136
method:"PUT",
@@ -266,11 +149,90 @@ module.exports = {
266
149
};
267
150
```
268
151
152
+
## REST API
153
+
154
+
The server includes a REST API that allows to change dinamically the current behavior, change delay time, etc. This is __very useful when running acceptance tests, as you can change the behavior of the api__ simply with a request in your tests `before` method.
155
+
156
+
Available api resources are:
157
+
158
+
*`GET``/mocks/behaviors` Returns an array containing all available behaviors.
159
+
*`GET``/mocks/behaviors/current` Returns current behavior.
160
+
*`PUT``/mocks/behaviors/current` Set current behavior.
161
+
* Request body example: `{ "name": "behavior-name" }`
162
+
*`GET``/mocks/settings` Return current server settings.
163
+
* Response body example: `{ "delay": 0 }`
164
+
*`PUT``/mocks/settings` Change current server settings.
165
+
* Request body example: `{ "delay": 3000 }`
166
+
167
+
## Programmatic usage
168
+
169
+
The server can be instantiated and started programmatically:
First argument is mandatory, and has to be a path to a folder containing "behaviors" and "fixtures". All files in the folder will be loaded recursively, including subfolders.
192
+
For second argument options, please read the [options](#options) chapter of this documentation.
193
+
194
+
Available methods of an instance are:
195
+
196
+
-`start` (). Starts the server.
197
+
-`stop` (). Stops the server.
198
+
-`restart` (). Stops the server, initializes it again (reloading behaviors files), and starts it again.
199
+
-`switchWatch` (state `<Boolean>`). Enable or disable behaviors files watch, depending of the received "state" value.
200
+
201
+
Available getters are:
202
+
203
+
-`behaviors`. Returns loaded behaviors object.
204
+
-`watchEnabled`. Current state of the behaviors files watcher.
205
+
-`error`. When server has returned an error, or an error ocurred loading behaviors, it is available in this property.
206
+
-`events`. Returns server events object. A "watch-reload" event is emitted when the server watch detects changes in any behaviors or fixtures file, and restarts the server.
207
+
208
+
> The interactive CLI can be started programatically too. Read the [cli advanced docs](./docs/cli.md) for further info.
209
+
210
+
## Global usage
211
+
212
+
The mocks server can be used as a global dependency as well:
213
+
214
+
```bash
215
+
npm i @mocks-server/main -g
216
+
```
217
+
218
+
Now, you can start the built-in command line interface from anywhere, providing a path to a folder containing behaviors:
219
+
220
+
```bash
221
+
mocks-server --behaviors=./path-to-behaviors
222
+
```
223
+
224
+
## Support (OS Terminals)
225
+
226
+
This package uses [inquirer][inquirer-url] for displaying CLI. You can [consult his OS Terminals support here][inquirer-support].
227
+
269
228
## Contributing
270
229
271
230
Contributors are welcome.
272
231
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).
0 commit comments