Skip to content

Commit 7813673

Browse files
committed
test pass with the basic feature
1 parent 3532ef0 commit 7813673

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

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: 14 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

@@ -125,7 +126,18 @@ func (s *inMemoryServer) Load() (err error) {
125126
w.WriteHeader(http.StatusInternalServerError)
126127
}
127128

128-
if proxy.RequestAmend.BodyPatch != "" {
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))
129141
}
130142

131143
targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, bytes.NewBuffer(requestBody))
@@ -148,6 +160,7 @@ func (s *inMemoryServer) Load() (err error) {
148160
memLogger.Error(err, "failed to read response body")
149161
return
150162
}
163+
fmt.Println("received:", string(data))
151164

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

0 commit comments

Comments
 (0)