Skip to content

Commit 875de99

Browse files
committed
test pass with the mock proxy
1 parent 451e4e0 commit 875de99

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ curl http://localhost:6060/mock/projects/atest -X DELETE
7676
items:
7777
- name: prList
7878
request:
79-
path: /v1/repos/{repo}/prs
79+
path: /api/v1/repos/{repo}/prs
8080
response:
8181
header:
8282
server: mock
@@ -97,7 +97,7 @@ items:
9797
启动 Mock 服务后,我们就可以发起如下的请求:
9898

9999
```shell
100-
curl http://localhost:6060/mock/v1/repos/atest/prs -v
100+
curl http://localhost:6060/mock/api/v1/repos/atest/prs -v
101101
```
102102

103103
另外,为了满足复杂的场景,还可以对 Response Body 做特定的解码,目前支持:`base64`、`url`:
@@ -108,7 +108,7 @@ curl http://localhost:6060/mock/v1/repos/atest/prs -v
108108
items:
109109
- name: base64
110110
request:
111-
path: /v1/base64
111+
path: /api/v1/base64
112112
response:
113113
body: aGVsbG8=
114114
encoder: base64
@@ -117,7 +117,7 @@ items:
117117
上面 Body 的内容是经过 `base64` 编码的,这可以用于不希望直接明文显示,或者是图片的场景:
118118

119119
```shell
120-
curl http://localhost:6060/mock/v1/base64
120+
curl http://localhost:6060/mock/api/v1/base64
121121
```
122122

123123
如果你的 Body 内容可以通过另外一个 HTTP 请求(GET)获得,那么你可以这么写:
@@ -128,7 +128,7 @@ curl http://localhost:6060/mock/v1/base64
128128
items:
129129
- name: baidu
130130
request:
131-
path: /v1/baidu
131+
path: /api/v1/baidu
132132
response:
133133
body: https://baidu.com
134134
encoder: url

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
items:
44
- name: prList
55
request:
6-
path: /v1/repos/{repo}/prs
6+
path: /api/v1/repos/{repo}/prs
77
response:
88
header:
99
server: mock
@@ -20,7 +20,7 @@ items:
2020
}
2121
- name: base64
2222
request:
23-
path: /v1/base64
23+
path: /api/v1/base64
2424
response:
2525
body: aGVsbG8=
2626
encoder: base64
@@ -30,3 +30,6 @@ items:
3030
response:
3131
body: https://baidu.com
3232
encoder: url
33+
proxies:
34+
- path: /api/v1/{point}
35+
target: http://atest.localhost:8080

pkg/mock/in_memory.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,31 @@ func (s *inMemoryServer) Load() (err error) {
106106
for _, proxy := range server.Proxies {
107107
memLogger.Info("start to proxy", "target", proxy.Target)
108108
s.mux.HandleFunc(proxy.Path, func(w http.ResponseWriter, req *http.Request) {
109-
fmt.Println("catch all", req.URL.Path)
109+
api := fmt.Sprintf("%s/%s", proxy.Target, strings.TrimPrefix(req.URL.Path, s.prefix))
110+
memLogger.Info("redirect to", "target", api)
111+
112+
targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, req.Body)
113+
if err != nil {
114+
memLogger.Error(err, "failed to create proxy request")
115+
return
116+
}
117+
118+
resp, err := http.DefaultClient.Do(targetReq)
119+
if err != nil {
120+
memLogger.Error(err, "failed to do proxy request")
121+
return
122+
}
123+
124+
data, err := io.ReadAll(resp.Body)
125+
if err != nil {
126+
memLogger.Error(err, "failed to read response body")
127+
return
128+
}
129+
130+
for k, v := range resp.Header {
131+
w.Header().Add(k, v[0])
132+
}
133+
w.Write(data)
110134
})
111135
}
112136
return

0 commit comments

Comments
 (0)