直连天下,v2插件

This commit is contained in:
李子铭 2024-11-07 10:17:16 +08:00
parent c6d95fc808
commit 622d1e6513
2 changed files with 27 additions and 33 deletions

View File

@ -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)

View File

@ -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