Skip to content

Commit 7486b07

Browse files
committed
feat: support read mock response from local file
1 parent bff5a58 commit 7486b07

File tree

8 files changed

+2083
-2028
lines changed

8 files changed

+2083
-2028
lines changed

docs/api-testing-mock-schema.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@
6060
"type": "object",
6161
"properties": {
6262
"encoder": {
63-
"type": "string"
63+
"type": "string",
64+
"enum": [
65+
"base64",
66+
"url",
67+
"raw"
68+
]
6469
},
6570
"body": {
6671
"type": "string"
6772
},
73+
"bodyFromFile": {
74+
"type": "string"
75+
},
6876
"header": {
6977
"type": "object",
7078
"description": "HTTP response headers. Common headers include 'Content-Type', 'Cache-Control', 'Set-Cookie', etc.",

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ items:
102102
curl http://localhost:6060/mock/api/v1/repos/atest/prs -v
103103
```
104104

105-
另外,为了满足复杂的场景,还可以对 Response Body 做特定的解码,目前支持:`base64`、`url`:
105+
另外,为了满足复杂的场景,还可以对 Response Body 做特定的解码,目前支持:`base64`、`url`、`raw`:
106+
107+
> encoder 为 `raw` 时,表示不进行处理
106108

107109
```yaml
108110
#!api-testing-mock
@@ -136,6 +138,19 @@ items:
136138
encoder: url
137139
```
138140

141+
如果你的响应内容比较大,或者保存在一个本地文件中,那么你可以这么写:
142+
143+
```yaml
144+
#!api-testing-mock
145+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
146+
items:
147+
- name: baidu
148+
request:
149+
path: /api/v1/baidu
150+
response:
151+
bodyFromFile: /tmp/baidu.html
152+
```
153+
139154
在实际情况中,往往是向已有系统或平台添加新的 API,此时要 Mock 所有已经存在的 API 就既没必要也需要很多工作量。因此,我们提供了一种简单的方式,即可以增加**代理**的方式把已有的 API 请求转发到实际的地址,只对新增的 API 进行 Mock 处理。如下所示:
140155

141156
```yaml

0 commit comments

Comments
 (0)