Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit d56986d

Browse files
authored
Merge pull request #10 from mocks-server/v1.1.0
V1.1.0
2 parents 7ecc012 + e78570b commit d56986d

23 files changed

Lines changed: 2161 additions & 2094 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# tests
1010
/coverage
11+
/mocks
1112

1213
# misc
1314
.DS_Store

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [To be deprecated]
8+
- Deprecate options "features" and "feature".
9+
- Remove "/features" api path.
10+
711
## [unreleased]
812
### Added
913
### Changed
1014
### Fixed
1115
### Removed
1216

17+
## [1.1.0] - 2019-11-08
18+
### Changed
19+
- Change "feature" concept by "behavior". Maintain old "feature" options and urls as aliases for maintaining compatibility.
20+
- Upgrade dependencies
21+
1322
## [1.0.3] - 2019-11-08
1423
### Fixed
1524
- Fix examples and badges in readme.

README.md

Lines changed: 105 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@
88

99
Mocks server with extensible fixtures groupables in predefined behaviors. Behavior can be changed using built-in CLI or REST API.
1010

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-
2511
## Getting Started
2612

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".
2814

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.
3016

3117
## Installation
3218

@@ -36,13 +22,11 @@ npm i @mocks-server/main --save-dev
3622

3723
## Usage
3824

39-
### Interactive CLI
40-
4125
Add an script to your `package.json` file, including the path to your mocks folder:
4226

4327
```json
4428
"scripts": {
45-
"mocks-server" : "mocks-server --features=./mocks"
29+
"mocks-server" : "mocks-server --behaviors=./mocks"
4630
}
4731
```
4832

@@ -52,166 +36,63 @@ Now, you can start the mocks server CLI simply typing:
5236
npm run mocks-server
5337
```
5438

55-
![cli-home](./assets/cli_home.png)
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-
const startMyCli = () => {
82-
const cli = new Cli({
83-
port: 3200,
84-
log: "debug",
85-
watch: false
86-
});
87-
88-
return cli.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:
108-
109-
```js
110-
const { Server } = require("@mocks-server/main");
111-
112-
const startMyServer = () => {
113-
const server = new Server(path.resolve(__dirname, "mocks"), {
114-
port: 3200,
115-
log: "debug",
116-
watch: false
117-
});
118-
119-
return server.start();
120-
};
121-
122-
startMyServer().then(server => {
123-
console.log("Server started", server);
124-
});
125-
```
126-
127-
##### `Server` (featuresFolder \[,options\])
128-
129-
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+
![cli-home](./assets/cli_animation.gif)
15940

16041
## Options
16142

16243
* port `<Number>` Por number for the Server to be listening.
16344
* host `<String>` Host for the server. Default is "0.0.0.0" (Listen to any local host).
16445
* 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.
16748
* 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.
17051
* cli `<Boolean>` Start interactive CLI. Default is `true`.
17152

17253
## Defining mocks
17354

17455
The Mocks server handles two main concepts for defining mocks:
17556

176-
### Features
57+
### Behaviors
17758

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.
17960

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.
18162

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:
18364

18465
```js
185-
// Features file 1
66+
// Behaviors file 1
18667

187-
const { Feature } = require("@mocks-server/main");
68+
const { Behavior } = require("@mocks-server/main");
18869

18970
const fixtures = require("./fixtures");
19071

191-
const myFeature = new Feature([fixtures.uri_1_fixture, fixtures.uri_2_fixture]);
72+
const myBehavior = new Behavior([fixtures.uri_1_fixture, fixtures.uri_2_fixture]);
19273

19374
module.exports = {
194-
myFeature
75+
myBehavior
19576
};
19677
```
19778

198-
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:
19980

20081
```js
201-
// Features file 2
82+
// Behaviors file 2
20283

203-
const { myFeature } = require("./features");
84+
const { myBehavior } = require("./behaviors");
20485

20586
const fixtures = require("./fixtures");
20687

207-
const myFeature2 = myFeature.extend([fixtures.uri_2_different_fixture]);
88+
const myBehavior2 = myBehavior.extend([fixtures.uri_2_different_fixture]);
20889

20990
module.exports = {
210-
myFeature2
91+
myBehavior2
21192
};
21293
```
21394

214-
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)
21596

21697
### Fixtures
21798

@@ -220,13 +101,14 @@ A "fixture" defines the response for an specific uri. It has to be an object con
220101
* 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.
221102
* 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.
222103
* 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.
225106
* 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.
226107

227108
```js
228109
// Fixtures file
229110

111+
// fixtures with static responses
230112
const uri_1_fixture = {
231113
url: "/api/foo-uri",
232114
method: "GET",
@@ -248,6 +130,7 @@ const uri_2_fixture = {
248130
}
249131
};
250132

133+
// fixture with a dynamic response
251134
const uri_2_different_fixture = {
252135
url: "/api/foo-uri-2/:id",
253136
method: "PUT",
@@ -266,11 +149,90 @@ module.exports = {
266149
};
267150
```
268151

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:
170+
171+
```js
172+
const { Server } = require("@mocks-server/main");
173+
174+
const startMyServer = () => {
175+
const server = new Server(path.resolve(__dirname, "mocks"), {
176+
port: 3200,
177+
log: "debug",
178+
watch: false
179+
});
180+
181+
return server.start();
182+
};
183+
184+
startMyServer().then(server => {
185+
console.log("Server started", server);
186+
});
187+
```
188+
189+
#### `Server` (behaviorsFolder \[,options\])
190+
191+
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+
269228
## Contributing
270229

271230
Contributors are welcome.
272231
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).
273232

233+
[inquirer-url]: https://www.npmjs.com/package/inquirer#support-os-terminals
234+
[inquirer-support]: https://www.npmjs.com/package/inquirer#support-os-terminals
235+
274236
[coveralls-image]: https://coveralls.io/repos/github/mocks-server/main/badge.svg
275237
[coveralls-url]: https://coveralls.io/github/mocks-server/main
276238
[travisci-image]: https://travis-ci.com/mocks-server/main.svg?branch=master

assets/cli_animation.gif

1010 KB
Loading

assets/cli_home.png

-49.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)