@@ -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
3145objects :
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
4576items:
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
0 commit comments