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

Commit 173e0b5

Browse files
authored
Merge pull request #9 from mocks-server/change-feature-by-behavior
Change feature by behavior
2 parents 8da8723 + c36fc85 commit 173e0b5

22 files changed

Lines changed: 2157 additions & 2076 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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ 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

13-
## [1.1.0]
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
1421

1522
## [1.0.3] - 2019-11-08
1623
### Fixed

README.md

Lines changed: 105 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Mocks server with extensible fixtures groupables in predefined behaviors. Behavi
2424

2525
## Getting Started
2626

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

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

3131
## Installation
3232

@@ -42,7 +42,7 @@ Add an script to your `package.json` file, including the path to your mocks fold
4242

4343
```json
4444
"scripts": {
45-
"mocks-server" : "mocks-server --features=./mocks"
45+
"mocks-server" : "mocks-server --behaviors=./mocks"
4646
}
4747
```
4848

@@ -52,166 +52,63 @@ Now, you can start the mocks server CLI simply typing:
5252
npm run mocks-server
5353
```
5454

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-
```
55+
![cli-home](./assets/cli_animation.gif)
15956

16057
## Options
16158

16259
* port `<Number>` Por number for the Server to be listening.
16360
* host `<String>` Host for the server. Default is "0.0.0.0" (Listen to any local host).
16461
* 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.
16764
* 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.
17067
* cli `<Boolean>` Start interactive CLI. Default is `true`.
17168

17269
## Defining mocks
17370

17471
The Mocks server handles two main concepts for defining mocks:
17572

176-
### Features
73+
### Behaviors
17774

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

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

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

18481
```js
185-
// Features file 1
82+
// Behaviors file 1
18683

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

18986
const fixtures = require("./fixtures");
19087

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

19390
module.exports = {
194-
myFeature
91+
myBehavior
19592
};
19693
```
19794

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

20097
```js
201-
// Features file 2
98+
// Behaviors file 2
20299

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

205102
const fixtures = require("./fixtures");
206103

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

209106
module.exports = {
210-
myFeature2
107+
myBehavior2
211108
};
212109
```
213110

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)
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)
215112

216113
### Fixtures
217114

@@ -220,8 +117,8 @@ A "fixture" defines the response for an specific uri. It has to be an object con
220117
* 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.
221118
* 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.
222119
* 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.
225122
* 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.
226123

227124
```js
@@ -266,11 +163,92 @@ module.exports = {
266163
};
267164
```
268165

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:
186+
187+
```js
188+
const { Server } = require("@mocks-server/main");
189+
190+
const startMyServer = () => {
191+
const server = new Server(path.resolve(__dirname, "mocks"), {
192+
port: 3200,
193+
log: "debug",
194+
watch: false
195+
});
196+
197+
return server.start();
198+
};
199+
200+
startMyServer().then(server => {
201+
console.log("Server started", server);
202+
});
203+
```
204+
205+
##### `Server` (behaviorsFolder \[,options\])
206+
207+
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+
269244
## Contributing
270245

271246
Contributors are welcome.
272247
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).
273248

249+
[inquirer-url]: https://www.npmjs.com/package/inquirer#support-os-terminals
250+
[inquirer-support]: https://www.npmjs.com/package/inquirer#support-os-terminals
251+
274252
[coveralls-image]: https://coveralls.io/repos/github/mocks-server/main/badge.svg
275253
[coveralls-url]: https://coveralls.io/github/mocks-server/main
276254
[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.

docs/cli.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#### CLI
2+
3+
The interactive CLI can be instantiated and started programmatically:
4+
5+
```js
6+
const { Cli } = require("@mocks-server/main");
7+
8+
const startMyCli = () => {
9+
const cli = new Cli({
10+
port: 3200,
11+
log: "debug",
12+
watch: false
13+
});
14+
15+
return cli.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.

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Copyright 2019 Javier Brea
23
Copyright 2019 XbyOrange
34
45
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
@@ -17,5 +18,6 @@ const Feature = require("./lib/features/Feature");
1718
module.exports = {
1819
Cli,
1920
Server,
20-
Feature
21+
Feature,
22+
Behavior: Feature
2123
};

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ module.exports = {
2828
testEnvironment: "node",
2929

3030
// The glob patterns Jest uses to detect test files
31-
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"]
31+
testMatch: ["**/test/unit/**/?(*.)+(spec|test).js?(x)"],
32+
//testMatch: ["**/test/unit/**/options.spec.js"]
3233
};

0 commit comments

Comments
 (0)