Skip to content

Commit d8c0de4

Browse files
authored
feat: support echo the request and response in mock server (#820)
* feat: support echo the request and response in mock server * feat: webhook support read body from file * add doc of the webhooks --------- Co-authored-by: rick <[email protected]>
1 parent 5789fae commit d8c0de4

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

console/atest-desktop/forge.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ module.exports = {
5151
ui: {
5252
"enabled": true,
5353
"chooseDirectory": true
54+
},
55+
beforeCreate: (msiCreator) => {
56+
// Add installation directory to system PATH
57+
msiCreator.wixTemplate = msiCreator.wixTemplate.replace(
58+
'</Product>',
59+
` <Property Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" />
60+
<CustomAction Id="AddToPath" Property="PATH" Value="[INSTALLDIR]" Execute="immediate" />
61+
<CustomAction Id="RemoveFromPath" Property="PATH" Value="[INSTALLDIR]" Execute="immediate" />
62+
63+
<InstallExecuteSequence>
64+
<Custom Action="AddToPath" After="InstallFiles">NOT Installed</Custom>
65+
<Custom Action="RemoveFromPath" Before="RemoveFiles">REMOVE="ALL"</Custom>
66+
</InstallExecuteSequence>
67+
68+
<Component Id="PathComponent" Guid="*" Directory="INSTALLDIR">
69+
<Environment Id="PATH" Name="PATH" Value="[INSTALLDIR]" Permanent="no" Part="last" Action="set" System="yes" />
70+
</Component>
71+
72+
</Product>`
73+
);
5474
}
5575
}
5676
}

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

Lines changed: 15 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
@@ -293,4 +296,16 @@ proxies:
293296
* HTTP
294297
* Syslog
295298

299+
```yaml
300+
webhooks:
301+
- timer: 3s
302+
name: shakeHands
303+
request:
304+
method: POST
305+
path: http://192.168.1.123:8080/api/v1
306+
header:
307+
Content-Type: application/json
308+
bodyFromFile: demo.json
309+
```
310+
296311
> 更多 URL 中通配符的用法,请参考 https://github.com/gorilla/mux

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: 25 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
}
@@ -580,6 +596,15 @@ func runWebhook(ctx context.Context, objCtx interface{}, wh *Webhook) (err error
580596
}
581597
}
582598

599+
if wh.Request.BodyFromFile != "" {
600+
if data, readErr := os.ReadFile(wh.Request.BodyFromFile); readErr != nil {
601+
memLogger.Error(readErr, "failed to read file", "file", wh.Request.BodyFromFile)
602+
return
603+
} else {
604+
wh.Request.Body = string(data)
605+
}
606+
}
607+
583608
var payload io.Reader
584609
payload, err = render.RenderAsReader("mock webhook server payload", wh.Request.Body, wh)
585610
if err != nil {

pkg/mock/types.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ type Item struct {
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"`
37+
BodyFromFile string `yaml:"bodyFromFile" json:"bodyFromFile"`
3738
}
3839

3940
type RequestWithAuth struct {
@@ -66,6 +67,7 @@ type Proxy struct {
6667
Target string `yaml:"target" json:"target"`
6768
RequestAmend RequestAmend `yaml:"requestAmend" json:"requestAmend"`
6869
Protocol string `yaml:"protocol" json:"protocol"`
70+
Echo bool `yaml:"echo" json:"echo"`
6971
}
7072

7173
type RequestAmend struct {

0 commit comments

Comments
 (0)