@ -69,7 +69,62 @@ Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:80
### Mock responses
Imagine the network is down or you're working offline, proxied requests to `https://internal-service.local/api/users/1` would fail. In this case, Mock Responses can fill the gap. Export your mock responses from a module.
Imagine the network is down or you're working offline, proxied requests to `https://internal-service.local/api/users/1` would fail. In this case, Mock Responses can fill the gap. Mocks are defined in a module which can be reused between projects.
Trivial example - respond to a request for `/rivers` with some JSON. Save the following Javascript in a file named `example-mocks.js`.
```js
module.exports = MockBase => class MockRivers extends MockBase {
mocks () {
return {
route: '/rivers',
responses: [
{
response: { type: 'json', body: [
{ name: 'Volga', drainsInto: 'Caspian Sea' },
{ name: 'Danube', drainsInto: 'Black Sea' },
{ name: 'Ural', drainsInto: 'Caspian Sea' },
{ name: 'Dnieper', drainsInto: 'Black Sea' }
]}
}
]
}
}
}
```
Launch `ws` passing in your mocks module.
```sh
$ ws --mocks example-mocks.js
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
```
GET your rivers.
```sh
$ curl http://127.0.0.1:8000/rivers
[
{
"name": "Volga",
"drainsInto": "Caspian Sea"
},
{
"name": "Danube",
"drainsInto": "Black Sea"
},
{
"name": "Ural",
"drainsInto": "Caspian Sea"
},
{
"name": "Dnieper",
"drainsInto": "Black Sea"
}
]
```
More detail can be added to mocks. This example, a RESTful `/users` API, adds responses handling `PUT`, `DELETE` and `POST`.