Skip to content

Commit 2f409ca

Browse files
authored
feat: support proxy request body amend in mock (#638)
* chore: let the sql query input be empty * feat: support proxy request body amend in mock * test pass with the basic feature --------- Signed-off-by: Rick <[email protected]> Co-authored-by: rick <[email protected]>
1 parent 5372c55 commit 2f409ca

File tree

6 files changed

+80
-30
lines changed

6 files changed

+80
-30
lines changed

console/atest-ui/src/views/DataManager.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const loadingStores = ref(true)
1717
1818
const tablesTree = ref([])
1919
watch(store, (s) => {
20+
kind.value = ''
2021
stores.value.forEach((e: Store) => {
2122
if (e.name === s) {
2223
kind.value = e.kind.name
@@ -39,6 +40,7 @@ watch(kind, (k) => {
3940
switch (k) {
4041
case 'atest-store-orm':
4142
queryTip.value = 'Enter SQL query'
43+
executeQuery()
4244
break;
4345
case 'atest-store-etcd':
4446
case 'atest-store-redis':

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ items:
3737
proxies:
3838
- path: /api/v1/{part}
3939
target: http://atest.localhost:8080
40+
- path: /open-apis/bot/v2/hook/{token}
41+
target: https://open.feishu.cn/
42+
requestAmend:
43+
bodyPatch: |
44+
[{
45+
"op": "add",
46+
"path": "/msg_type",
47+
"value": "text"
48+
}, {
49+
"op": "add",
50+
"path": "/content",
51+
"value": {}
52+
}, {
53+
"op": "move",
54+
"from": "/text",
55+
"path": "/content/text"
56+
}]

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ require (
4343

4444
require golang.org/x/mod v0.22.0
4545

46+
require github.com/evanphx/json-patch v0.5.2 // indirect
47+
4648
require (
4749
github.com/Masterminds/goutils v1.1.1 // indirect
4850
github.com/Masterminds/semver/v3 v3.2.1 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK3
3131
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3232
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3333
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34+
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
35+
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
3436
github.com/expr-lang/expr v1.15.6 h1:dQFgzj5DBu3wnUz8+PGLZdPMpefAvxaCFTNM3iSjkGA=
3537
github.com/expr-lang/expr v1.15.6/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
3638
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
@@ -98,6 +100,7 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
98100
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
99101
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
100102
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
103+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
101104
github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls=
102105
github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k=
103106
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

pkg/mock/in_memory.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sync"
2929
"time"
3030

31+
jsonpatch "github.com/evanphx/json-patch"
3132
"github.com/swaggest/openapi-go/openapi3"
3233
"github.com/swaggest/rest/gorillamux"
3334

@@ -120,7 +121,26 @@ func (s *inMemoryServer) Load() (err error) {
120121
}
121122
memLogger.Info("redirect to", "target", api)
122123

123-
targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, req.Body)
124+
var requestBody []byte
125+
if requestBody, err = io.ReadAll(req.Body); err != nil {
126+
w.WriteHeader(http.StatusInternalServerError)
127+
}
128+
129+
if proxy.RequestAmend.BodyPatch != "" && len(requestBody) > 0 {
130+
var patch jsonpatch.Patch
131+
if patch, err = jsonpatch.DecodePatch([]byte(proxy.RequestAmend.BodyPatch)); err != nil {
132+
return
133+
}
134+
135+
fmt.Println("before patch:", string(requestBody))
136+
if requestBody, err = patch.Apply(requestBody); err != nil {
137+
fmt.Println(err)
138+
return
139+
}
140+
fmt.Println("after patch:", string(requestBody))
141+
}
142+
143+
targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, bytes.NewBuffer(requestBody))
124144
if err != nil {
125145
w.WriteHeader(http.StatusInternalServerError)
126146
memLogger.Error(err, "failed to create proxy request")
@@ -140,6 +160,7 @@ func (s *inMemoryServer) Load() (err error) {
140160
memLogger.Error(err, "failed to read response body")
141161
return
142162
}
163+
fmt.Println("received:", string(data))
143164

144165
for k, v := range resp.Header {
145166
w.Header().Add(k, v[0])

pkg/mock/types.go

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,59 @@ 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]string
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]string
2929
}
3030

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

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

4545
type Response struct {
46-
Encoder string `yaml:"encoder" json:"encoder"`
47-
Body string `yaml:"body" json:"body"`
48-
Header map[string]string `yaml:"header" json:"header"`
49-
StatusCode int `yaml:"statusCode" json:"statusCode"`
50-
BodyData []byte
46+
Encoder string `yaml:"encoder" json:"encoder"`
47+
Body string `yaml:"body" json:"body"`
48+
Header map[string]string `yaml:"header" json:"header"`
49+
StatusCode int `yaml:"statusCode" json:"statusCode"`
50+
BodyData []byte
5151
}
5252

5353
type Webhook struct {
54-
Name string `yaml:"name" json:"name"`
55-
Timer string `yaml:"timer" json:"timer"`
56-
Request RequestWithAuth `yaml:"request" json:"request"`
54+
Name string `yaml:"name" json:"name"`
55+
Timer string `yaml:"timer" json:"timer"`
56+
Request RequestWithAuth `yaml:"request" json:"request"`
5757
}
5858

5959
type Proxy struct {
60-
Path string `yaml:"path" json:"path"`
61-
Target string `yaml:"target" json:"target"`
60+
Path string `yaml:"path" json:"path"`
61+
Target string `yaml:"target" json:"target"`
62+
RequestAmend RequestAmend `yaml:"requestAmend" json:"requestAmend"`
63+
}
64+
65+
type RequestAmend struct {
66+
BodyPatch string `yaml:"bodyPatch" json:"bodyPatch"`
6267
}
6368

6469
type Server struct {
65-
Objects []Object `yaml:"objects" json:"objects"`
66-
Items []Item `yaml:"items" json:"items"`
67-
Proxies []Proxy `yaml:"proxies" json:"proxies"`
68-
Webhooks []Webhook `yaml:"webhooks" json:"webhooks"`
70+
Objects []Object `yaml:"objects" json:"objects"`
71+
Items []Item `yaml:"items" json:"items"`
72+
Proxies []Proxy `yaml:"proxies" json:"proxies"`
73+
Webhooks []Webhook `yaml:"webhooks" json:"webhooks"`
6974
}

0 commit comments

Comments
 (0)