From 3532ef05edaf48b967d801d36a089b789a5b863f Mon Sep 17 00:00:00 2001 From: rick Date: Tue, 4 Mar 2025 08:08:31 +0000 Subject: [PATCH] feat: support proxy request body amend in mock --- console/atest-ui/src/views/DataManager.vue | 4 +- pkg/mock/in_memory.go | 10 +++- pkg/mock/types.go | 63 ++++++++++++---------- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/console/atest-ui/src/views/DataManager.vue b/console/atest-ui/src/views/DataManager.vue index 90ab3ea..b8609ff 100644 --- a/console/atest-ui/src/views/DataManager.vue +++ b/console/atest-ui/src/views/DataManager.vue @@ -17,6 +17,7 @@ const loadingStores = ref(true) const tablesTree = ref([]) watch(store, (s) => { + kind.value = '' stores.value.forEach((e: Store) => { if (e.name === s) { kind.value = e.kind.name @@ -39,7 +40,8 @@ watch(kind, (k) => { queryTip.value = 'Enter SQL query' executeQuery() break; - case 'atest-store-etcd', 'atest-store-redis': + case 'atest-store-etcd': + case 'atest-store-redis': sqlQuery.value = '' queryTip.value = 'Enter key' break; diff --git a/pkg/mock/in_memory.go b/pkg/mock/in_memory.go index aad1847..c07c727 100644 --- a/pkg/mock/in_memory.go +++ b/pkg/mock/in_memory.go @@ -120,7 +120,15 @@ func (s *inMemoryServer) Load() (err error) { } memLogger.Info("redirect to", "target", api) - targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, req.Body) + var requestBody []byte + if requestBody, err = io.ReadAll(req.Body); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + + if proxy.RequestAmend.BodyPatch != "" { + } + + targetReq, err := http.NewRequestWithContext(req.Context(), req.Method, api, bytes.NewBuffer(requestBody)) if err != nil { w.WriteHeader(http.StatusInternalServerError) memLogger.Error(err, "failed to create proxy request") diff --git a/pkg/mock/types.go b/pkg/mock/types.go index fb00c11..54ef297 100644 --- a/pkg/mock/types.go +++ b/pkg/mock/types.go @@ -16,54 +16,59 @@ limitations under the License. package mock type Object struct { - Name string `yaml:"name" json:"name"` - InitCount *int `yaml:"initCount" json:"initCount"` - Sample string `yaml:"sample" json:"sample"` + Name string `yaml:"name" json:"name"` + InitCount *int `yaml:"initCount" json:"initCount"` + Sample string `yaml:"sample" json:"sample"` } type Item struct { - Name string `yaml:"name" json:"name"` - Request Request `yaml:"request" json:"request"` - Response Response `yaml:"response" json:"response"` - Param map[string]string + Name string `yaml:"name" json:"name"` + Request Request `yaml:"request" json:"request"` + Response Response `yaml:"response" json:"response"` + Param map[string]string } type Request struct { - Path string `yaml:"path" json:"path"` - Method string `yaml:"method" json:"method"` - Header map[string]string `yaml:"header" json:"header"` - Body string `yaml:"body" json:"body"` + Path string `yaml:"path" json:"path"` + Method string `yaml:"method" json:"method"` + Header map[string]string `yaml:"header" json:"header"` + Body string `yaml:"body" json:"body"` } type RequestWithAuth struct { - Request `yaml:",inline"` - BearerAPI string `yaml:"bearerAPI" json:"bearerAPI"` - Username string `yaml:"username" json:"username"` - Password string `yaml:"password" json:"password"` + Request `yaml:",inline"` + BearerAPI string `yaml:"bearerAPI" json:"bearerAPI"` + Username string `yaml:"username" json:"username"` + Password string `yaml:"password" json:"password"` } type Response struct { - Encoder string `yaml:"encoder" json:"encoder"` - Body string `yaml:"body" json:"body"` - Header map[string]string `yaml:"header" json:"header"` - StatusCode int `yaml:"statusCode" json:"statusCode"` - BodyData []byte + Encoder string `yaml:"encoder" json:"encoder"` + Body string `yaml:"body" json:"body"` + Header map[string]string `yaml:"header" json:"header"` + StatusCode int `yaml:"statusCode" json:"statusCode"` + BodyData []byte } type Webhook struct { - Name string `yaml:"name" json:"name"` - Timer string `yaml:"timer" json:"timer"` - Request RequestWithAuth `yaml:"request" json:"request"` + Name string `yaml:"name" json:"name"` + Timer string `yaml:"timer" json:"timer"` + Request RequestWithAuth `yaml:"request" json:"request"` } type Proxy struct { - Path string `yaml:"path" json:"path"` - Target string `yaml:"target" json:"target"` + Path string `yaml:"path" json:"path"` + Target string `yaml:"target" json:"target"` + RequestAmend RequestAmend `yaml:"requestAmend" json:"requestAmend"` +} + +type RequestAmend struct { + BodyPatch string `yaml:"bodyPatch" json:"bodyPatch"` } type Server struct { - Objects []Object `yaml:"objects" json:"objects"` - Items []Item `yaml:"items" json:"items"` - Proxies []Proxy `yaml:"proxies" json:"proxies"` - Webhooks []Webhook `yaml:"webhooks" json:"webhooks"` + Objects []Object `yaml:"objects" json:"objects"` + Items []Item `yaml:"items" json:"items"` + Proxies []Proxy `yaml:"proxies" json:"proxies"` + Webhooks []Webhook `yaml:"webhooks" json:"webhooks"` }