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

Commit 70f8408

Browse files
authored
Merge pull request #11 from mocks-server/link-to-web
Link to web
2 parents c8613f4 + f393abd commit 70f8408

2 files changed

Lines changed: 15 additions & 211 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Removed
1616

1717
## [1.1.1] - 2019-11-12
18+
### Changed
19+
- Change readme. Add links to docs website.
1820

1921
## [1.1.0] - 2019-11-08
2022
### Changed

README.md

Lines changed: 13 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -4,232 +4,34 @@
44

55
[![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url]
66

7-
# Mocks Server
87

9-
Mocks server with extensible fixtures groupables in predefined behaviors. Behavior can be changed using built-in CLI or REST API.
8+
# [![Mocks Server][logo-url]][website-url] Mocks Server
109

11-
## Getting Started
10+
This package provides a server that simulates API behaviors. It can be added as a dependency of your project, and started simply running an NPM command.
1211

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".
12+
## Documentation
1413

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.
14+
Please refer to the [project documentation website][website-url]:
1615

17-
## Installation
16+
* [Get started](https://www.mocks-server.org/docs/get-started-intro)
17+
* [Tutorials](https://www.mocks-server.org/docs/tutorials-static)
18+
* [configuration](https://www.mocks-server.org/docs/configuration-command-line-arguments)
1819

19-
```bash
20-
npm i @mocks-server/main --save-dev
21-
```
20+
## Why a mocks server?
2221

23-
## Usage
22+
Controlling the responses of the api will improve the front-end development workflow, avoiding early dependencies with back-end. It also improves the testing and development of error cases or another cases that are commonly hard to reproduce in the real api.
2423

25-
Add an script to your `package.json` file, including the path to your mocks folder:
24+
Defining the api responses during the earliest phases of development will improve the communication among team members and align their expectations.
2625

27-
```json
28-
"scripts": {
29-
"mocks-server" : "mocks-server --behaviors=./mocks"
30-
}
31-
```
32-
33-
Now, you can start the mocks server CLI simply typing:
34-
35-
```bash
36-
npm run mocks-server
37-
```
38-
39-
![cli-home](./assets/cli_animation.gif)
40-
41-
## Options
42-
43-
* port `<Number>` Por number for the Server to be listening.
44-
* host `<String>` Host for the server. Default is "0.0.0.0" (Listen to any local host).
45-
* log `<String>` Logs level. Can be one of "silly", "debug", "verbose", "info", "warn", "error".
46-
* watch `<Boolean>` Watch behaviors folder, and restart server on changes. Default is `true`.
47-
* behavior `<String>` Selected behavior when server is started.
48-
* delay `<Number` Responses delay time in milliseconds.
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.
51-
* cli `<Boolean>` Start interactive CLI. Default is `true`.
52-
53-
## Defining mocks
54-
55-
The Mocks server handles two main concepts for defining mocks:
56-
57-
### Behaviors
58-
59-
Each behavior consists in a set of "fixtures", which are server responses for specific uris.
60-
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.
62-
63-
For creating a Behavior, you have to use the mocks-server "Behavior" class, providing an array of "fixtures" to it:
64-
65-
```js
66-
// Behaviors file 1
67-
68-
const { Behavior } = require("@mocks-server/main");
69-
70-
const fixtures = require("./fixtures");
71-
72-
const myBehavior = new Behavior([fixtures.uri_1_fixture, fixtures.uri_2_fixture]);
73-
74-
module.exports = {
75-
myBehavior
76-
};
77-
```
78-
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:
80-
81-
```js
82-
// Behaviors file 2
83-
84-
const { myBehavior } = require("./behaviors");
85-
86-
const fixtures = require("./fixtures");
87-
88-
const myBehavior2 = myBehavior.extend([fixtures.uri_2_different_fixture]);
89-
90-
module.exports = {
91-
myBehavior2
92-
};
93-
```
94-
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)
96-
97-
### Fixtures
98-
99-
A "fixture" defines the response for an specific uri. It has to be an object containing properties:
100-
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.
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.
103-
* response `<Object>` Defines the response that the Mocks Server will send to the request:
104-
* status `<Number>` Status code to send.
105-
* body `<Object>` Json object to send as body in the response.
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.
107-
108-
```js
109-
// Fixtures file
110-
111-
// fixtures with static responses
112-
const uri_1_fixture = {
113-
url: "/api/foo-uri",
114-
method: "GET",
115-
response: {
116-
status: 200,
117-
body: [
118-
{
119-
name: "foo-name"
120-
}
121-
]
122-
}
123-
};
124-
125-
const uri_2_fixture = {
126-
url: "/api/foo-uri-2/:id",
127-
method: "PUT",
128-
response: {
129-
status: 204
130-
}
131-
};
132-
133-
// fixture with a dynamic response
134-
const uri_2_different_fixture = {
135-
url: "/api/foo-uri-2/:id",
136-
method: "PUT",
137-
response: (req, res) => {
138-
res.status(404);
139-
res.send({
140-
message: `${req.params.id} was not found`
141-
});
142-
}
143-
};
144-
145-
module.exports = {
146-
uri_1_fixture,
147-
uri_2_fixture,
148-
uri_2_different_fixture
149-
};
150-
```
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:
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].
26+
Working with Node.js, it integrates better in front-end projects as any other NPM dependency, and it will be easier for front-end developers to maintain the mocks.
22727

22828
## Contributing
22929

23030
Contributors are welcome.
23131
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).
23232

33+
[website-url]: https://www.mocks-server.org
34+
[logo-url]: https://www.mocks-server.org/img/logo_120.png
23335
[inquirer-url]: https://www.npmjs.com/package/inquirer#support-os-terminals
23436
[inquirer-support]: https://www.npmjs.com/package/inquirer#support-os-terminals
23537

0 commit comments

Comments
 (0)