diff --git a/docs/site/content/zh/latest/tasks/mock/simple.yaml b/docs/site/content/zh/latest/tasks/mock/simple.yaml index 04e585c..2d756f6 100644 --- a/docs/site/content/zh/latest/tasks/mock/simple.yaml +++ b/docs/site/content/zh/latest/tasks/mock/simple.yaml @@ -37,3 +37,20 @@ items: proxies: - path: /api/v1/{part} target: http://atest.localhost:8080 + - path: /open-apis/bot/v2/hook/{token} + target: https://open.feishu.cn/ + requestAmend: + bodyPatch: | + [{ + "op": "add", + "path": "/msg_type", + "value": "text" + }, { + "op": "add", + "path": "/content", + "value": {} + }, { + "op": "move", + "from": "/text", + "path": "/content/text" + }] diff --git a/go.mod b/go.mod index 5d6d5b1..871bf36 100644 --- a/go.mod +++ b/go.mod @@ -43,6 +43,8 @@ require ( require golang.org/x/mod v0.22.0 +require github.com/evanphx/json-patch v0.5.2 // indirect + require ( github.com/Masterminds/goutils v1.1.1 // indirect github.com/Masterminds/semver/v3 v3.2.1 // indirect diff --git a/go.sum b/go.sum index b1b023c..6999465 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,8 @@ github.com/cucumber/messages-go/v16 v16.0.1/go.mod h1:EJcyR5Mm5ZuDsKJnT2N9KRnBK3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k= +github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ= github.com/expr-lang/expr v1.15.6 h1:dQFgzj5DBu3wnUz8+PGLZdPMpefAvxaCFTNM3iSjkGA= github.com/expr-lang/expr v1.15.6/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ= 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 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So= github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls= github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= diff --git a/pkg/mock/in_memory.go b/pkg/mock/in_memory.go index c07c727..ba107be 100644 --- a/pkg/mock/in_memory.go +++ b/pkg/mock/in_memory.go @@ -28,6 +28,7 @@ import ( "sync" "time" + jsonpatch "github.com/evanphx/json-patch" "github.com/swaggest/openapi-go/openapi3" "github.com/swaggest/rest/gorillamux" @@ -125,7 +126,18 @@ func (s *inMemoryServer) Load() (err error) { w.WriteHeader(http.StatusInternalServerError) } - if proxy.RequestAmend.BodyPatch != "" { + if proxy.RequestAmend.BodyPatch != "" && len(requestBody) > 0 { + var patch jsonpatch.Patch + if patch, err = jsonpatch.DecodePatch([]byte(proxy.RequestAmend.BodyPatch)); err != nil { + return + } + + fmt.Println("before patch:", string(requestBody)) + if requestBody, err = patch.Apply(requestBody); err != nil { + fmt.Println(err) + return + } + fmt.Println("after patch:", string(requestBody)) } targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, bytes.NewBuffer(requestBody)) @@ -148,6 +160,7 @@ func (s *inMemoryServer) Load() (err error) { memLogger.Error(err, "failed to read response body") return } + fmt.Println("received:", string(data)) for k, v := range resp.Header { w.Header().Add(k, v[0])