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.
@@ -24,9 +24,9 @@ Mocks server with extensible fixtures groupables in predefined behaviors. Behavi
24
24
25
25
## Getting Started
26
26
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".
27
+
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
28
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.
29
+
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
30
31
31
## Installation
32
32
@@ -42,7 +42,7 @@ Add an script to your `package.json` file, including the path to your mocks fold
@@ -52,166 +52,63 @@ Now, you can start the mocks server CLI simply typing:
52
52
npm run mocks-server
53
53
```
54
54
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
-
```
55
+

159
56
160
57
## Options
161
58
162
59
* port `<Number>` Por number for the Server to be listening.
163
60
* host `<String>` Host for the server. Default is "0.0.0.0" (Listen to any local host).
164
61
* 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.
62
+
* watch `<Boolean>` Watch behaviors folder, and restart server on changes. Default is `true`.
63
+
*behavior`<String>` Selected behavior when server is started.
167
64
* 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.
65
+
*behaviors`Path as <String>` Path to a folder containing behaviors to be used by the server.
66
+
* recursive `<Boolean>` Load behaviors recursively. Watch is not affected by this option, it is always recursive.
170
67
* cli `<Boolean>` Start interactive CLI. Default is `true`.
171
68
172
69
## Defining mocks
173
70
174
71
The Mocks server handles two main concepts for defining mocks:
175
72
176
-
### Features
73
+
### Behaviors
177
74
178
-
Each feature consists in a set of "fixtures", which are server responses for specific uris.
75
+
Each behavior consists in a set of "fixtures", which are server responses for specific uris.
179
76
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.
77
+
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
78
182
-
For creating a Feature, you have to use the mocks-server "Feature" class, providing an array of "fixtures" to it:
79
+
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:
95
+
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)
111
+
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
112
216
113
### Fixtures
217
114
@@ -220,8 +117,8 @@ A "fixture" defines the response for an specific uri. It has to be an object con
220
117
* 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
118
* 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
119
* 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.
120
+
* status `<Number>` Status code to send.
121
+
* body `<Object>` Json object to send as body in the response.
225
122
* 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
123
227
124
```js
@@ -266,11 +163,92 @@ module.exports = {
266
163
};
267
164
```
268
165
166
+
### REST API
167
+
168
+
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.
169
+
170
+
Available api resources are:
171
+
172
+
*`GET``/mocks/behaviors` Returns an array containing all available behaviors.
173
+
*`GET``/mocks/behaviors/current` Returns current behavior.
174
+
*`PUT``/mocks/behaviors/current` Set current behavior.
175
+
* Request body example: `{ "name": "behavior-name" }`
176
+
*`GET``/mocks/settings` Return current server settings.
177
+
* Response body example: `{ "delay": 0 }`
178
+
*`PUT``/mocks/settings` Change current server settings.
179
+
* Request body example: `{ "delay": 3000 }`
180
+
181
+
### Programmatic usage
182
+
183
+
#### Server
184
+
185
+
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.
208
+
For second argument options, please read the [options](#options) chapter of this documentation.
209
+
210
+
Available methods of an instance are:
211
+
212
+
-`start` (). Starts the server.
213
+
-`stop` (). Stops the server.
214
+
-`restart` (). Stops the server, initializes it again (reloading behaviors files), and starts it again.
215
+
-`switchWatch` (state `<Boolean>`). Enable or disable behaviors files watch, depending of the received "state" value.
216
+
217
+
Available getters are:
218
+
219
+
-`behaviors`. Returns loaded behaviors object.
220
+
-`watchEnabled`. Current state of the behaviors files watcher.
221
+
-`error`. When server has returned an error, or an error ocurred loading behaviors, it is available in this property.
222
+
-`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.
223
+
224
+
> The interactive CLI can be started programatically too. Read the [cli advanced docs](./docs/cli.md) for further info.
225
+
226
+
### Global usage
227
+
228
+
The mocks server can be used as a global dependency as well:
229
+
230
+
```bash
231
+
npm i @mocks-server/main -g
232
+
```
233
+
234
+
Now, you can start the built-in command line interface from anywhere, providing a path to a folder containing behaviors:
235
+
236
+
```bash
237
+
mocks-server --behaviors=./path-to-behaviors
238
+
```
239
+
240
+
### Support (OS Terminals)
241
+
242
+
This package uses [inquirer][inquirer-url] for displaying CLI. You can [consult his OS Terminals support here][inquirer-support].
243
+
269
244
## Contributing
270
245
271
246
Contributors are welcome.
272
247
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).
The interactive CLI can be instantiated and started programmatically:
4
+
5
+
```js
6
+
const { Cli } =require("@mocks-server/main");
7
+
8
+
conststartMyCli= () => {
9
+
constcli=newCli({
10
+
port:3200,
11
+
log:"debug",
12
+
watch:false
13
+
});
14
+
15
+
returncli.start();
16
+
};
17
+
18
+
startMyCli().catch(err=> {
19
+
console.log("Error starting CLI", err);
20
+
});
21
+
```
22
+
23
+
##### `Cli` (\[options\]\[,customQuitMethod\])
24
+
For first argument options, please read the [options](#options) chapter of this documentation. Available methods of an instance are:
25
+
-`start` ()
26
+
Inits the server in case it was stopped, adds the watch listeners, and renders main menu.
27
+
-`initServer` ()
28
+
Inits the server in case it was stopped, adds the watch listeners.
29
+
-`stopListeningServerWatch` ()
30
+
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.
Copy file name to clipboardExpand all lines: index.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
/*
2
+
Copyright 2019 Javier Brea
2
3
Copyright 2019 XbyOrange
3
4
4
5
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
0 commit comments