Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: imporove mock object with standard crud #553

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/api-testing-mock-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,7 @@
"properties": {
"name": {"type": "string"},
"initCount": {"type": "integer"},
"sample": {"type": "string"},
"fields": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"kind": {"type": "string"}
},
"required": ["name", "kind"]
}
}
"sample": {"type": "string"}
},
"required": ["name"]
}
Expand Down Expand Up @@ -66,6 +55,17 @@
"required": ["name", "request", "response"]
}
},
"proxies": {
"type": "array",
"items": {
"type": "object",
"properties": {
"path": {"type": "string"},
"target": {"type": "string"}
},
"required": ["path", "target"]
}
},
"webhooks": {
"type": "array",
"items": {
Expand Down
3 changes: 1 addition & 2 deletions docs/site/content/zh/about/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ linkTitle: 关于

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

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

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

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

{{% /blocks/section %}}

17 changes: 0 additions & 17 deletions docs/site/content/zh/latest/tasks/mock-server.md

This file was deleted.

122 changes: 102 additions & 20 deletions docs/site/content/zh/latest/tasks/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,138 @@ atest mock --prefix / --port 9090 mock.yaml

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

## Mock Docker Registry

您可以通过执行下面的命令 mock 一个容器仓库服务[container registry](https://distribution.github.io/distribution/):

```shell
atest mock --prefix / mock/image-registry.yaml
```

之后,您可以通过使用如下的命令使用 mock 功能。

```shell
docker pull localhost:6060/repo/name:tag
```

## 语法

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

* 针对某个数据对象的 CRUD
* 任意 HTTP 服务
* 面向对象的 CRUD
* 自定义 HTTP 服务

下面是一个具体的例子:
### 面对对象

```yaml
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
objects:
- name: repo
fields:
- name: name
kind: string
- name: url
kind: string
- name: projects
initCount: 3
sample: |
{
"name": "api-testing",
"name": "atest",
"color": "{{ randEnum "blue" "read" "pink" }}"
}
```

上面 `projects` 的配置,会自动提供该对象的 CRUD(创建、查找、更新、删除)的 API,你可以通过 `atest` 或类似工具发出 HTTP 请求。例如:

```shell
curl http://localhost:6060/mock/projects

curl http://localhost:6060/mock/projects/atest

curl http://localhost:6060/mock/projects -X POST -d '{"name": "new"}'

curl http://localhost:6060/mock/projects -X PUT -d '{"name": "new", "remark": "this is a project"}'

curl http://localhost:6060/mock/projects/atest -X DELETE
```

> `initCount` 是指按照 `sample` 给定的数据初始化多少个对象;如果没有指定的话,则默认值为 1.

### 自定义

```yaml
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
items:
- name: base64
request:
path: /v1/base64
response:
body: aGVsbG8=
encoder: base64
- name: prList
request:
path: /v1/repos/{repo}/prs
header:
name: rick
path: /api/v1/repos/{repo}/prs
response:
header:
server: mock
Content-Type: application/json
body: |
{
"count": 1,
"items": [{
"title": "fix: there is a bug on page {{ randEnum "one" }}",
"title": "fix: there is a bug on page {{ randEnum "one", "two" }}",
"number": 123,
"message": "{{.Response.Header.server}}",
"author": "someone",
"status": "success"
}]
}
```

启动 Mock 服务后,我们就可以发起如下的请求:

```shell
curl http://localhost:6060/mock/api/v1/repos/atest/prs -v
```

另外,为了满足复杂的场景,还可以对 Response Body 做特定的解码,目前支持:`base64`、`url`:

```yaml
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
items:
- name: base64
request:
path: /api/v1/base64
response:
body: aGVsbG8=
encoder: base64
```

上面 Body 的内容是经过 `base64` 编码的,这可以用于不希望直接明文显示,或者是图片的场景:

```shell
curl http://localhost:6060/mock/api/v1/base64
```

如果你的 Body 内容可以通过另外一个 HTTP 请求(GET)获得,那么你可以这么写:

```yaml
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
items:
- name: baidu
request:
path: /api/v1/baidu
response:
body: https://baidu.com
encoder: url
```

在实际情况中,往往是向已有系统或平台添加新的 API,此时要 Mock 所有已经存在的 API 就既没必要也需要很多工作量。因此,我们提供了一种简单的方式,即可以增加**代理**的方式把已有的 API 请求转发到实际的地址,只对新增的 API 进行 Mock 处理。如下所示:

```yaml
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
proxies:
- path: /api/v1/{part}
target: http://atest.localhost:8080
```

当我们发起如下的请求时,实际请求的地址为 `http://atest.localhost:8080/api/v1/projects`

```shell
curl http://localhost:6060/mock/api/v1/projects
```

> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux
10 changes: 10 additions & 0 deletions docs/site/content/zh/latest/tasks/mock/object.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
objects:
- name: projects
initCount: 3
sample: |
{
"name": "atest",
"color": "{{ randEnum "blue" "read" "pink" }}"
}
35 changes: 35 additions & 0 deletions docs/site/content/zh/latest/tasks/mock/simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!api-testing-mock
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
items:
- name: prList
request:
path: /api/v1/repos/{repo}/prs
response:
header:
server: mock
body: |
{
"count": 1,
"items": [{
"title": "fix: there is a bug on page {{ randEnum "one" }}",
"number": 123,
"message": "{{.Response.Header.server}}",
"author": "someone",
"status": "success"
}]
}
- name: base64
request:
path: /api/v1/base64
response:
body: aGVsbG8=
encoder: base64
- name: baidu
request:
path: /v1/baidu
response:
body: https://baidu.com
encoder: url
proxies:
- path: /api/v1/{part}
target: http://atest.localhost:8080
Loading
Loading