Skip to content

Commit

Permalink
docs: more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Ottun committed Oct 13, 2021
1 parent acf5038 commit 29daa52
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 206 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
TARGET_BRANCH: gh-pages
BUILD_SCRIPT: yarn && yarn build:docs
BUILD_DIR: docs/.vuepress/dist/
COMMIT_MESSAGE: New Docs
202 changes: 30 additions & 172 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,207 +28,65 @@ Deputy can also act as a testing server to validate what requests made by system
# Quick Start

```shell
npx @sayjava/deputy
docker run -p 8080:8080 -p 8081:8081 ghcr.io/sayjava/deputy
```

or
and test a sample endpoint

```shell
docker run -p 8080:8080 ghcr.io/sayjava/deputy
```


and test the endpoint

```shell
curl -X GET http://localhost:8080/hello
curl http://localhost:8080/who-am-i
```

# Features

- [Declarative request matching and response](https://sayjava.github.io/deputy/guide)
- [Regex based request matching](https://sayjava.github.io/deputy/guide) (request path, headers and body matchers)
- [Alternate and limit responses on same request](https://sayjava.github.io/deputy/guide)
- [Templated based response](https://sayjava.github.io/deputy/guide), e.g `"Task is {{req.queryParams.name}}"`
- [NodeJS HTTP/Express Middleware](https://sayjava.github.io/deputy/start)
- [HTTP request validations](https://sayjava.github.io/deputy/assertions)
- [Simulate response delays and network failures](https://sayjava.github.io/deputy/guide)
- [Serverless compatible](https://sayjava.github.io/deputy)

# Examples

Here are some scenarios where `Deputy` can be used to mock endpoints. Start the server on the default 8080 port

```shell
npx @sayjava/deputy
```

### Regex paths

```shell
npx @sayjava/deputy -b '[
{
"name": "Match any task with id",
"request": { "path": "/tasks/[0-9]+" },
"response": {
"body": "found it"
}
}
]
'
```

to match requests like these:

```shell
curl http://localhost:8080/tasks/2
curl http://localhost:8080/tasks/10
```
- [Declarative request matching and response](https://sayjava.github.io/deputy/guide)
- [Regex based request matching](https://sayjava.github.io/deputy/guide) (request path, headers and body matchers)
- [Alternate and limit responses on same request](https://sayjava.github.io/deputy/guide)
- [HTTP request validations](https://sayjava.github.io/deputy/assertions)
- [Simulate response delays and network failures](https://sayjava.github.io/deputy/guide)
- [Inspect HTTP Requests and Response in realtime](https://sayjava.github.io/deputy/start)

### Request headers

e.g `{"user-a∫gent": "Chrome|Apple*"}`
# Scenarios

```shell
npx @sayjava/deputy -b '[
{
"name": "Match requests coming from Apple devices or Chrome",
"request": {
"path": "/tasks/[0-9]+",
"headers": {
"user-agent": "Chrome|Apple*"
}
},
"response": {
"body": "Found on a mac or chrome"
}
}
]
'
```
Here are some scenarios that Deputy can aid in your next development

to match requests like:
## Transparently Mock & Forward API requests

```shell
curl http://localhost:8080/tasks/2 -H 'user-agent: Chrome'
```
Simulate unready APIs by mocking some APIs and have other requests transparently forwarded to remote APIs
See the examples/commerce folder using that uses the [next/commerce](next/commerces) + deputy

### Templated response
![Dev](./docs/media/dev_environment.png)

HTTP response can be templated using Handlebars syntax
## Application Testing

```shell
@sayava/deputy -b `[
{
"request": {
"path": "/greet/:name",
"pathParams": {
"name": "[a-z]+"
}
},
"response": {
"body": "Hello {{req.pathParams.name}}"
}
}
]`
```
Simulate complex HTTP requests and response scenarios in test environments

to match requests and respond with the template:
![Test](./docs/media/test_environment.png)

```shell
curl http://localhost:8080/greet/jane
```

and respond with `Hello jane`

### Request body matchers

e.g `{"user":"john_[a-z]+"}`

```shell
@sayjava/deputy -b '[{
"name": "Match requests with users with names like john_xxx",
"request": {
"path": "/tasks",
"method": "POST",
"body": {
"user": "john_[a-z]+"
}
},
"response": {
"statusCode": "201",
"body": "Task created by {{req.body.user}}"
}
}]
'
```

to match requests like:

```shell
curl -X POST http://localhost:8080/tasks -H "content-type:application/json" -d '{ "user": "john_doe", "name": "pay up" }'
```

### Asserts received requests

Asserts that Deputy has received a request with that path at least twice and at most 10 times

```shell
curl -X PUT http://localhost:8080/_/api/requests/assert -H "content-type:application/json" -d '[
{
"request": {
"path": "/todo/2",
"count": {
"atLeast": 2,
"atMost": 10
}
}
}
]'
```

### Asserts the sequence requests are received
see the [Mock Guide](http://sayjava.github.com/deputy)

Asserts that Deputy has received a request with that path at least twice and at most 10 times
## Deputy UI

```shell
curl -X PUT http://localhost:8080/_/api/requests/sequence -H "content-type:application/json" -d '[
{
"request": {
"path": "/todo/2"
}
},
{
"request": {
"path": "/todosdss/20"
}
}
]'
```
By default, Deputy server can be reached at `http://localhost:8081`.

see the [Mock Guide](http://sayjava.github.com/deputy)
### Logs View

### Programmatically Use cases (Express Middleware / NodeJS HTTP Middleware)
View and inspect http requests and responses from the Logs interface in realtime as requests are received

```javascript
const express = require('express');
const { deputyHandler } = require('@sayjava/deputy');
![Logs](./docs/media/logs.png)

const app = express();
app.use(express.static(__dirname + '/views'));
### Visualize

// Existing route
app.get('/', (req, res) => res.render('index', { title: 'Hey', message: 'Hello there!' }));
Deputy automatically creates a sequence diagram of requests it receives

// Mount the middleware on /api.
app.use('/api', deputyHandler({ config: { fromFile: 'api.json' } }));
![Visualize](./docs/media/visualize.png)

app.listen(3000, () => console.info(`App started on 3000`));
```
### Mocking Interface

### Serverless Mock Server
Mocks can be imported, exported, edited, cloned, disabled, and enabled from Deputy UI

See [Serverless Deployment](examples/deputy-with-lambda/README.md)
![Mocking Interface](./docs/media/disable_mocks.png)

### Full Documentation

Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ Selectively mock APIs, Deputy server will transparently forward unmatched reques

### Quick Start
```bash
docker run -p 8080:8080 ghcr.io/sayjava/deputy
docker run -p 8080:8080 -p 8081:8081 ghcr.io/sayjava/deputy
```

```bash
$ curl http://localhost:8080/whoami
$ curl http://localhost:8080/who-am-i
```

Visit the dashboard at `http://localhost:8080/_/dashboard`
Visit the dashboard at `http://localhost:8081/_/dashboard`
Binary file removed docs/media/assert_requests.png
Binary file not shown.
28 changes: 0 additions & 28 deletions docs/media/quick_start.svg

This file was deleted.

Binary file removed docs/media/server_mock_middleware.png
Binary file not shown.
Binary file removed docs/media/standalone_mock _server.png
Binary file not shown.
4 changes: 2 additions & 2 deletions mocks/hello_world.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- request:
path: /hi
path: /who-am-i
response:
body: Hello World
body: I am deputy, an HTTP Mocking Server
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.5",
"vuepress": "^1.5.3"
"vuepress": "^1.8.2"
},
"dependencies": {
"axios": "^0.21.4",
Expand Down

0 comments on commit 29daa52

Please sign in to comment.