Skip to content

Commit 2db1624

Browse files
authored
feat: imporove mock object with standard crud (#553)
* feat: imporove mock object with standard crud * add more document of mock * add a common proxy on mock server * test pass with the mock proxy * update mock json schema --------- Co-authored-by: rick <[email protected]>
1 parent bbaf91f commit 2db1624

File tree

10 files changed

+249
-114
lines changed

10 files changed

+249
-114
lines changed

docs/api-testing-mock-schema.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,7 @@
1010
"properties": {
1111
"name": {"type": "string"},
1212
"initCount": {"type": "integer"},
13-
"sample": {"type": "string"},
14-
"fields": {
15-
"type": "array",
16-
"items": {
17-
"type": "object",
18-
"properties": {
19-
"name": {"type": "string"},
20-
"kind": {"type": "string"}
21-
},
22-
"required": ["name", "kind"]
23-
}
24-
}
13+
"sample": {"type": "string"}
2514
},
2615
"required": ["name"]
2716
}
@@ -66,6 +55,17 @@
6655
"required": ["name", "request", "response"]
6756
}
6857
},
58+
"proxies": {
59+
"type": "array",
60+
"items": {
61+
"type": "object",
62+
"properties": {
63+
"path": {"type": "string"},
64+
"target": {"type": "string"}
65+
},
66+
"required": ["path", "target"]
67+
}
68+
},
6969
"webhooks": {
7070
"type": "array",
7171
"items": {

docs/site/content/zh/about/index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ linkTitle: 关于
55

66
{{% blocks/cover title="About API Testing" height="auto" %}}
77

8-
API Testing 是一个接口调试和测试工具的开源项目。
8+
API Testing (atest)是一个接口调试和测试工具的开源项目。
99

1010
请继续阅读以了解更多信息,或访问我们的 [文档](/latest/) 开始使用!
1111

@@ -20,4 +20,3 @@ API Testing 是一个接口调试和测试工具的开源项目。
2020
// TBD.
2121

2222
{{% /blocks/section %}}
23-

docs/site/content/zh/latest/tasks/mock-server.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/site/content/zh/latest/tasks/mock.md

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,138 @@ atest mock --prefix / --port 9090 mock.yaml
1616

1717
在 UI 上可以实现和命令行相同的功能,并可以通过页面编辑的方式修改、加载 Mock 服务配置。
1818

19+
## Mock Docker Registry
20+
21+
您可以通过执行下面的命令 mock 一个容器仓库服务[container registry](https://distribution.github.io/distribution/):
22+
23+
```shell
24+
atest mock --prefix / mock/image-registry.yaml
25+
```
26+
27+
之后,您可以通过使用如下的命令使用 mock 功能。
28+
29+
```shell
30+
docker pull localhost:6060/repo/name:tag
31+
```
32+
1933
## 语法
2034

2135
从整体上来看,我们的写法和 HTTP 的名称基本保持一致,用户无需再了解额外的名词。此外,提供两种描述 Mock 服务的方式:
2236

23-
* 针对某个数据对象的 CRUD
24-
* 任意 HTTP 服务
37+
* 面向对象的 CRUD
38+
* 自定义 HTTP 服务
2539

26-
下面是一个具体的例子:
40+
### 面对对象
2741

2842
```yaml
2943
#!api-testing-mock
3044
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
3145
objects:
32-
- name: repo
33-
fields:
34-
- name: name
35-
kind: string
36-
- name: url
37-
kind: string
3846
- name: projects
3947
initCount: 3
4048
sample: |
4149
{
42-
"name": "api-testing",
50+
"name": "atest",
4351
"color": "{{ randEnum "blue" "read" "pink" }}"
4452
}
53+
```
54+
55+
上面 `projects` 的配置,会自动提供该对象的 CRUD(创建、查找、更新、删除)的 API,你可以通过 `atest` 或类似工具发出 HTTP 请求。例如:
56+
57+
```shell
58+
curl http://localhost:6060/mock/projects
59+
60+
curl http://localhost:6060/mock/projects/atest
61+
62+
curl http://localhost:6060/mock/projects -X POST -d '{"name": "new"}'
63+
64+
curl http://localhost:6060/mock/projects -X PUT -d '{"name": "new", "remark": "this is a project"}'
65+
66+
curl http://localhost:6060/mock/projects/atest -X DELETE
67+
```
68+
69+
> `initCount` 是指按照 `sample` 给定的数据初始化多少个对象;如果没有指定的话,则默认值为 1.
70+
71+
### 自定义
72+
73+
```yaml
74+
#!api-testing-mock
75+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
4576
items:
46-
- name: base64
47-
request:
48-
path: /v1/base64
49-
response:
50-
body: aGVsbG8=
51-
encoder: base64
5277
- name: prList
5378
request:
54-
path: /v1/repos/{repo}/prs
55-
header:
56-
name: rick
79+
path: /api/v1/repos/{repo}/prs
5780
response:
5881
header:
5982
server: mock
83+
Content-Type: application/json
6084
body: |
6185
{
6286
"count": 1,
6387
"items": [{
64-
"title": "fix: there is a bug on page {{ randEnum "one" }}",
88+
"title": "fix: there is a bug on page {{ randEnum "one", "two" }}",
6589
"number": 123,
6690
"message": "{{.Response.Header.server}}",
6791
"author": "someone",
6892
"status": "success"
6993
}]
7094
}
7195
```
96+
97+
启动 Mock 服务后,我们就可以发起如下的请求:
98+
99+
```shell
100+
curl http://localhost:6060/mock/api/v1/repos/atest/prs -v
101+
```
102+
103+
另外,为了满足复杂的场景,还可以对 Response Body 做特定的解码,目前支持:`base64`、`url`:
104+
105+
```yaml
106+
#!api-testing-mock
107+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
108+
items:
109+
- name: base64
110+
request:
111+
path: /api/v1/base64
112+
response:
113+
body: aGVsbG8=
114+
encoder: base64
115+
```
116+
117+
上面 Body 的内容是经过 `base64` 编码的,这可以用于不希望直接明文显示,或者是图片的场景:
118+
119+
```shell
120+
curl http://localhost:6060/mock/api/v1/base64
121+
```
122+
123+
如果你的 Body 内容可以通过另外一个 HTTP 请求(GET)获得,那么你可以这么写:
124+
125+
```yaml
126+
#!api-testing-mock
127+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
128+
items:
129+
- name: baidu
130+
request:
131+
path: /api/v1/baidu
132+
response:
133+
body: https://baidu.com
134+
encoder: url
135+
```
136+
137+
在实际情况中,往往是向已有系统或平台添加新的 API,此时要 Mock 所有已经存在的 API 就既没必要也需要很多工作量。因此,我们提供了一种简单的方式,即可以增加**代理**的方式把已有的 API 请求转发到实际的地址,只对新增的 API 进行 Mock 处理。如下所示:
138+
139+
```yaml
140+
#!api-testing-mock
141+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
142+
proxies:
143+
- path: /api/v1/{part}
144+
target: http://atest.localhost:8080
145+
```
146+
147+
当我们发起如下的请求时,实际请求的地址为 `http://atest.localhost:8080/api/v1/projects`
148+
149+
```shell
150+
curl http://localhost:6060/mock/api/v1/projects
151+
```
152+
153+
> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!api-testing-mock
2+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
3+
objects:
4+
- name: projects
5+
initCount: 3
6+
sample: |
7+
{
8+
"name": "atest",
9+
"color": "{{ randEnum "blue" "read" "pink" }}"
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!api-testing-mock
2+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
3+
items:
4+
- name: prList
5+
request:
6+
path: /api/v1/repos/{repo}/prs
7+
response:
8+
header:
9+
server: mock
10+
body: |
11+
{
12+
"count": 1,
13+
"items": [{
14+
"title": "fix: there is a bug on page {{ randEnum "one" }}",
15+
"number": 123,
16+
"message": "{{.Response.Header.server}}",
17+
"author": "someone",
18+
"status": "success"
19+
}]
20+
}
21+
- name: base64
22+
request:
23+
path: /api/v1/base64
24+
response:
25+
body: aGVsbG8=
26+
encoder: base64
27+
- name: baidu
28+
request:
29+
path: /v1/baidu
30+
response:
31+
body: https://baidu.com
32+
encoder: url
33+
proxies:
34+
- path: /api/v1/{part}
35+
target: http://atest.localhost:8080

0 commit comments

Comments
 (0)