Skip to content

Commit 55243e1

Browse files
committed
update mock json schema
1 parent 875de99 commit 55243e1

File tree

5 files changed

+41
-32
lines changed

5 files changed

+41
-32
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/latest/tasks/mock.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ curl http://localhost:6060/mock/api/v1/base64
122122

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

125-
```
125+
```yaml
126126
#!api-testing-mock
127127
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-mock-schema.json
128128
items:
@@ -133,3 +133,21 @@ items:
133133
body: https://baidu.com
134134
encoder: url
135135
```
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

docs/site/content/zh/latest/tasks/mock/simple.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ items:
3131
body: https://baidu.com
3232
encoder: url
3333
proxies:
34-
- path: /api/v1/{point}
34+
- path: /api/v1/{part}
3535
target: http://atest.localhost:8080

pkg/mock/in_memory.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,13 @@ func (s *inMemoryServer) Load() (err error) {
138138

139139
func (s *inMemoryServer) Start(reader Reader, prefix string) (err error) {
140140
var handler http.Handler
141-
if handler, err = s.SetupHandler(reader, prefix); err != nil {
142-
return
143-
}
144-
145-
if s.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", s.port)); err != nil {
146-
return
141+
if handler, err = s.SetupHandler(reader, prefix); err == nil {
142+
if s.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", s.port)); err == nil {
143+
go func() {
144+
err = http.Serve(s.listener, handler)
145+
}()
146+
}
147147
}
148-
go func() {
149-
err = http.Serve(s.listener, handler)
150-
}()
151148
return
152149
}
153150

pkg/mock/types.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,9 @@ limitations under the License.
1616
package mock
1717

1818
type Object struct {
19-
Name string `yaml:"name" json:"name"`
20-
InitCount *int `yaml:"initCount" json:"initCount"`
21-
Sample string `yaml:"sample" json:"sample"`
22-
Fields []Field `yaml:"fields" json:"fields"`
23-
}
24-
25-
type Field struct {
26-
Name string `yaml:"name" json:"name"`
27-
Kind string `yaml:"kind" json:"kind"`
19+
Name string `yaml:"name" json:"name"`
20+
InitCount *int `yaml:"initCount" json:"initCount"`
21+
Sample string `yaml:"sample" json:"sample"`
2822
}
2923

3024
type Item struct {

0 commit comments

Comments
 (0)