Skip to content

Commit e690113

Browse files
committed
feat: support echo the request and response in mock server
1 parent ce1f79f commit e690113

File tree

4 files changed

+58
-37
lines changed

4 files changed

+58
-37
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,11 @@ items:
234234
proxies:
235235
- path: /api/v1/{part}
236236
target: http://atest.localhost:8080
237+
echo: true
237238
```
238239

240+
当 echo 的值为 true 时,会把收到的请求以及响应打印出来,方便观察数据。
241+
239242
当我们发起如下的请求时,实际请求的地址为 `http://atest.localhost:8080/api/v1/projects`
240243

241244
```shell

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ proxies:
3939
target: http://atest.localhost:8080
4040
- path: /open-apis/bot/v2/hook/{token}
4141
target: https://open.feishu.cn/
42+
echo: true
4243
requestAmend:
4344
bodyPatch: |
4445
[{

pkg/mock/in_memory.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ func (s *inMemoryServer) httpProxy(proxy *Proxy) {
178178
fmt.Println("after patch:", string(requestBody))
179179
}
180180

181+
if proxy.Echo {
182+
fmt.Println("Original request header:")
183+
for k, v := range req.Header {
184+
fmt.Println(k, ":", v)
185+
}
186+
fmt.Println("Original request path:", req.URL)
187+
fmt.Println("Original request payload:")
188+
fmt.Println(string(requestBody))
189+
}
190+
181191
targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, bytes.NewBuffer(requestBody))
182192
if err != nil {
183193
w.WriteHeader(http.StatusInternalServerError)
@@ -206,6 +216,12 @@ func (s *inMemoryServer) httpProxy(proxy *Proxy) {
206216
for k, v := range resp.Header {
207217
w.Header().Add(k, v[0])
208218
}
219+
220+
if proxy.Echo {
221+
fmt.Println("Original response payload:")
222+
fmt.Println(string(data))
223+
}
224+
209225
w.Write(data)
210226
})
211227
}

pkg/mock/types.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,66 @@ 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"`
19+
Name string `yaml:"name" json:"name"`
20+
InitCount *int `yaml:"initCount" json:"initCount"`
21+
Sample string `yaml:"sample" json:"sample"`
2222
}
2323

2424
type Item struct {
25-
Name string `yaml:"name" json:"name"`
26-
Request Request `yaml:"request" json:"request"`
27-
Response Response `yaml:"response" json:"response"`
28-
Param map[string]interface{}
25+
Name string `yaml:"name" json:"name"`
26+
Request Request `yaml:"request" json:"request"`
27+
Response Response `yaml:"response" json:"response"`
28+
Param map[string]interface{}
2929
}
3030

3131
type Request struct {
32-
Protocol string `yaml:"protocol" json:"protocol"`
33-
Path string `yaml:"path" json:"path"`
34-
Method string `yaml:"method" json:"method"`
35-
Header map[string]string `yaml:"header" json:"header"`
36-
Body string `yaml:"body" json:"body"`
32+
Protocol string `yaml:"protocol" json:"protocol"`
33+
Path string `yaml:"path" json:"path"`
34+
Method string `yaml:"method" json:"method"`
35+
Header map[string]string `yaml:"header" json:"header"`
36+
Body string `yaml:"body" json:"body"`
3737
}
3838

3939
type RequestWithAuth struct {
40-
Request `yaml:",inline"`
41-
BearerAPI string `yaml:"bearerAPI" json:"bearerAPI"`
42-
Username string `yaml:"username" json:"username"`
43-
Password string `yaml:"password" json:"password"`
40+
Request `yaml:",inline"`
41+
BearerAPI string `yaml:"bearerAPI" json:"bearerAPI"`
42+
Username string `yaml:"username" json:"username"`
43+
Password string `yaml:"password" json:"password"`
4444
}
4545

4646
type Response struct {
47-
Encoder string `yaml:"encoder" json:"encoder"`
48-
Body string `yaml:"body" json:"body"`
49-
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile"`
50-
Header map[string]string `yaml:"header" json:"header"`
51-
StatusCode int `yaml:"statusCode" json:"statusCode"`
52-
BodyData []byte
47+
Encoder string `yaml:"encoder" json:"encoder"`
48+
Body string `yaml:"body" json:"body"`
49+
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile"`
50+
Header map[string]string `yaml:"header" json:"header"`
51+
StatusCode int `yaml:"statusCode" json:"statusCode"`
52+
BodyData []byte
5353
}
5454

5555
type Webhook struct {
56-
Name string `yaml:"name" json:"name"`
57-
Timer string `yaml:"timer" json:"timer"`
58-
Param map[string]string `yaml:"param" json:"param"`
59-
Request RequestWithAuth `yaml:"request" json:"request"`
56+
Name string `yaml:"name" json:"name"`
57+
Timer string `yaml:"timer" json:"timer"`
58+
Param map[string]string `yaml:"param" json:"param"`
59+
Request RequestWithAuth `yaml:"request" json:"request"`
6060
}
6161

6262
type Proxy struct {
63-
Prefix string `yaml:"prefix" json:"prefix"`
64-
Port int `yaml:"port" json:"port"`
65-
Path string `yaml:"path" json:"path"`
66-
Target string `yaml:"target" json:"target"`
67-
RequestAmend RequestAmend `yaml:"requestAmend" json:"requestAmend"`
68-
Protocol string `yaml:"protocol" json:"protocol"`
63+
Prefix string `yaml:"prefix" json:"prefix"`
64+
Port int `yaml:"port" json:"port"`
65+
Path string `yaml:"path" json:"path"`
66+
Target string `yaml:"target" json:"target"`
67+
RequestAmend RequestAmend `yaml:"requestAmend" json:"requestAmend"`
68+
Protocol string `yaml:"protocol" json:"protocol"`
69+
Echo bool `yaml:"echo" json:"echo"`
6970
}
7071

7172
type RequestAmend struct {
72-
BodyPatch string `yaml:"bodyPatch" json:"bodyPatch"`
73+
BodyPatch string `yaml:"bodyPatch" json:"bodyPatch"`
7374
}
7475

7576
type Server struct {
76-
Objects []Object `yaml:"objects" json:"objects"`
77-
Items []Item `yaml:"items" json:"items"`
78-
Proxies []Proxy `yaml:"proxies" json:"proxies"`
79-
Webhooks []Webhook `yaml:"webhooks" json:"webhooks"`
77+
Objects []Object `yaml:"objects" json:"objects"`
78+
Items []Item `yaml:"items" json:"items"`
79+
Proxies []Proxy `yaml:"proxies" json:"proxies"`
80+
Webhooks []Webhook `yaml:"webhooks" json:"webhooks"`
8081
}

0 commit comments

Comments
 (0)