From 622d1e6513196de5fc34a20f914608c7e07e1b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Thu, 7 Nov 2024 10:17:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E8=BF=9E=E5=A4=A9=E4=B8=8B=EF=BC=8Cv2?= =?UTF-8?q?=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/zltx_v2/internal/zltx_v2.go | 37 +++++++----------------- plugins/zltx_v2/internal/zltx_v2_test.go | 23 +++++++++++---- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/plugins/zltx_v2/internal/zltx_v2.go b/plugins/zltx_v2/internal/zltx_v2.go index 765b8f8..9ee177a 100644 --- a/plugins/zltx_v2/internal/zltx_v2.go +++ b/plugins/zltx_v2/internal/zltx_v2.go @@ -10,6 +10,7 @@ import ( "gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify" "io/ioutil" "net/http" + "strings" ) // 插件通信信息,若不对应则会报错panic @@ -65,37 +66,19 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest) func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { req := &http.Request{ - Method: "", - URL: nil, - Proto: "", - ProtoMajor: 0, - ProtoMinor: 0, - Header: nil, - Body: nil, - GetBody: nil, - ContentLength: 0, - TransferEncoding: nil, - Close: false, - Host: "", - Form: nil, - PostForm: nil, - MultipartForm: nil, - Trailer: nil, - RemoteAddr: "", - RequestURI: "", - TLS: nil, - Cancel: nil, - Response: nil, + Header: nil, + Body: nil, } - headers := make(map[string]string) - if err := json.Unmarshal(request.Headers, &headers); err != nil { + httpHeaders := make(http.Header) + if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil { return nil, fmt.Errorf("headers Unmarshal err [%v]", err) } - httpHeaders := make(http.Header, len(headers)) - for k, value := range headers { - httpHeaders.Add(k, value) + newHeaders := make(http.Header) + for key, values := range httpHeaders { + newKey := strings.Title(strings.ToLower(key)) + newHeaders[newKey] = values } - req.Header = httpHeaders + req.Header = newHeaders req.Body = ioutil.NopCloser(bytes.NewBuffer(request.Body)) c, err := transConfig(request.Config) diff --git a/plugins/zltx_v2/internal/zltx_v2_test.go b/plugins/zltx_v2/internal/zltx_v2_test.go index b9b8f6e..8831ed8 100644 --- a/plugins/zltx_v2/internal/zltx_v2_test.go +++ b/plugins/zltx_v2/internal/zltx_v2_test.go @@ -6,6 +6,7 @@ import ( "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" "github.com/stretchr/testify/assert" + "google.golang.org/grpc/metadata" "testing" ) @@ -78,7 +79,7 @@ func TestQuery(t *testing.T) { } func TestNotify(t *testing.T) { - jsonData := `{ + jsonData := []byte(`{ "tradeStatus": "SUCCESS", "orderNo": "C2081410801687044096", "tradeStateDesc": "成功", @@ -89,16 +90,26 @@ func TestNotify(t *testing.T) { "outTradeNo": "20240802212326527830", "rechargeAccount": "17384082748", "unitPrice":102.5 - }` - headers := `{"Authorization":"292e21f3683219369cf68dede0d45730","Content-Type":"application/json"}` + }`) + md := metadata.New(map[string]string{ + "Authorization": "292e21f3683219369cf68dede0d45730", + "Content-Type": "application/json", + }) + ctx := metadata.NewIncomingContext(context.Background(), md) + headerData, ok := metadata.FromIncomingContext(ctx) + if !ok { + t.Error("无法获取 Header 数据") + return + } + HeaderBytes, _ := json.Marshal(headerData) in := &proto.NotifyRequest{ Config: config(), Queries: nil, - Headers: []byte(headers), - Body: []byte(jsonData), + Headers: HeaderBytes, + Body: jsonData, } t.Run("TestNotify", func(t *testing.T) { - got, err := zltx.Notify(context.Background(), in) + got, err := zltx.Notify(ctx, in) if !assert.Nil(t, err) { t.Errorf("Notify() error = %v", err) return