插件调试

This commit is contained in:
李子铭 2025-01-07 10:59:07 +08:00
parent 7428fb1892
commit 507e120fc3
2 changed files with 18 additions and 41 deletions

View File

@ -10,7 +10,6 @@ import (
"gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify" "gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify"
"io" "io"
"net/http" "net/http"
"strings"
) )
// 插件通信信息,若不对应则会报错panic // 插件通信信息,若不对应则会报错panic
@ -28,14 +27,17 @@ func (p *ZLTXV2Service) Order(ctx context.Context, request *proto.OrderRequest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
client, err := c.client() client, err := c.client()
if err != nil { if err != nil {
return nil, err return nil, err
} }
response, errResp, err := api.OrderCreate(ctx, client, c.AppId, c.orderReq(request)) response, errResp, err := api.OrderCreate(ctx, client, c.AppId, c.orderReq(request))
if err != nil { if err != nil {
return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err)) return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err))
} }
if errResp != nil { if errResp != nil {
return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)) return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message))
} }
@ -48,6 +50,7 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest)
if err != nil { if err != nil {
return nil, err return nil, err
} }
client, err := c.client() client, err := c.client()
if err != nil { if err != nil {
return nil, err return nil, err
@ -57,6 +60,7 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest)
if err != nil { if err != nil {
return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err)) return nil, proto.ErrorRequestFail(fmt.Sprintf("请求异常[%v]", err))
} }
if errResp != nil { if errResp != nil {
return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message)) return nil, proto.ErrorRequestFail(fmt.Sprintf("请求错误[code:%d]-[reason:%s]-[msg:%s]", errResp.Code, errResp.Reason, errResp.Message))
} }
@ -65,26 +69,21 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest)
} }
func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
req := &http.Request{
Header: nil,
Body: nil,
}
httpHeaders := make(http.Header) httpHeaders := make(http.Header)
if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil { if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil {
return nil, proto.ErrorParamFail(fmt.Sprintf("headers Unmarshal err [%v]", err)) return nil, proto.ErrorParamFail(fmt.Sprintf("headers Unmarshal err [%v]", err))
} }
newHeaders := make(http.Header)
for key, values := range httpHeaders { req := &http.Request{
newKey := strings.ToTitle(strings.ToLower(key)) Header: httpHeaders,
newHeaders[newKey] = values Body: io.NopCloser(bytes.NewBuffer(request.Body)),
} }
req.Header = newHeaders
req.Body = io.NopCloser(bytes.NewBuffer(request.Body))
c, err := transConfig(request.Config) c, err := transConfig(request.Config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
n, err := notify.NewNotify(c.AppId, c.AppKey).ParseAndVerify(req) n, err := notify.NewNotify(c.AppId, c.AppKey).ParseAndVerify(req)
if err != nil { if err != nil {
return nil, proto.ErrorSignFail(err.Error()) return nil, proto.ErrorSignFail(err.Error())

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto" "gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"google.golang.org/grpc/metadata"
"net/http" "net/http"
"testing" "testing"
) )
@ -36,7 +35,7 @@ func TestOrder(t *testing.T) {
request := &proto.OrderRequest{ request := &proto.OrderRequest{
Config: config(), Config: config(),
Order: &proto.OrderRequest_Order{ Order: &proto.OrderRequest_Order{
OrderNo: "test_zltx_v2_6", OrderNo: "test_zltx_v2_7",
Account: "18666173766", Account: "18666173766",
Quantity: 1, Quantity: 1,
Extra: nil, Extra: nil,
@ -62,8 +61,8 @@ func TestQuery(t *testing.T) {
request := &proto.QueryRequest{ request := &proto.QueryRequest{
Config: config(), Config: config(),
Order: &proto.QueryRequest_Order{ Order: &proto.QueryRequest_Order{
OrderNo: "test_zltx_v2_3", OrderNo: "test_zltx_v2_7",
TradeNo: "", TradeNo: "", // 731095414466813953
Account: "", Account: "",
Extra: nil, Extra: nil,
}, },
@ -80,39 +79,18 @@ func TestQuery(t *testing.T) {
} }
func TestNotify(t *testing.T) { func TestNotify(t *testing.T) {
jsonData := []byte(`{ bodyBytes := []byte(`{"tradeStatus":"SUCCESS","orderNo":"731095414466813953","tradeStateDesc":"成功","mchId":23329,"outTradeNo":"test_zltx_v2_7","rechargeAccount":"18666173766","unitPrice":0.1,"attach":""}`)
"tradeStatus": "SUCCESS", headerBytes := []byte(`{"Accept-Encoding":["gzip"],"Authorization":["39859e5d71a1e7a9f69a6d00cfe79a7a"],"Connection":["close"],"Content-Length":["186"],"Content-Type":["application/json"],"User-Agent":["GuzzleHttp/6.5.5 curl/7.69.1 PHP/7.2.34"],"X-Envoy-Attempt-Count":["1"],"X-Envoy-External-Address":["120.79.35.82"],"X-Forwarded-Client-Cert":["By=spiffe://cluster.local/ns/gopro/sa/default;Hash=7e17458f89c270f74ca6039bfd8c091de2924d2f95f3e415f9c6f95e67932798;Subject=\"\";URI=spiffe://cluster.local/ns/istio-system/sa/istio-ingressgateway-go"],"X-Forwarded-For":["120.79.35.82, 112.124.3.46"],"X-Forwarded-Proto":["https"],"X-Real-Ip":["112.124.3.46"],"X-Remoteaddr":["172.21.0.1"],"X-Request-Id":["69a03321-e633-466b-808e-7e822232cf12"]}`)
"orderNo": "C2081410801687044096",
"tradeStateDesc": "成功",
"cards": [
{"no":"*","pwd":"*","deadline":"2024-07-03 23:59:59","cardType":1}
],
"mchId": 23329,
"outTradeNo": "20240802212326527830",
"rechargeAccount": "17384082748",
"unitPrice":102.5
}`)
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{ in := &proto.NotifyRequest{
Config: config(), Config: config(),
Queries: nil, Queries: nil,
Headers: HeaderBytes, Headers: headerBytes,
Body: jsonData, Body: bodyBytes,
} }
t.Run("TestNotify", func(t *testing.T) { t.Run("TestNotify", func(t *testing.T) {
got, err := zltx.Notify(ctx, in) got, err := zltx.Notify(context.Background(), in)
if !assert.Nil(t, err) { if !assert.Nil(t, err) {
t.Errorf("Notify() error = %v", err) t.Errorf("Notify() error = %v", err)
return return