package internal import ( "context" "encoding/json" "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" "github.com/stretchr/testify/assert" "google.golang.org/grpc/metadata" "testing" ) var zltx = &ZLTXV2Service{} func config() []byte { c := &Config{ AppId: "23329", AppKey: "8db16e8cc8363ed4eb4c14f9520bcc32", BaseUri: "http://211.137.105.198:17100", NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify", } marshal, _ := json.Marshal(c) return marshal } func TestConfig(t *testing.T) { t.Run("TestConfig", func(t *testing.T) { c := config() fmt.Printf("%s\n", string(c)) assert.NotEmpty(t, c) }) } func TestOrder(t *testing.T) { request := &proto.OrderRequest{ Config: config(), Order: &proto.OrderRequest_Order{ OrderNo: "test_zltx_v2_6", Account: "18666173766", Quantity: 1, Extra: nil, }, Product: &proto.OrderRequest_Product{ ProductNo: "106", Extra: []byte(`{}`), }, } t.Run("TestOrder", func(t *testing.T) { got, err := zltx.Order(context.Background(), request) if err != nil { t.Errorf("Order() error = %v", err) return } fmt.Printf("%+v\n", got) assert.Equal(t, int(proto.Status_ING), int(got.Result.Status)) }) } func TestQuery(t *testing.T) { request := &proto.QueryRequest{ Config: config(), Order: &proto.QueryRequest_Order{ OrderNo: "test_zltx_v2_3", TradeNo: "", Account: "", Extra: nil, }, } t.Run("TestQuery", func(t *testing.T) { got, err := zltx.Query(context.Background(), request) if err != nil { t.Errorf("Query() error = %v", err) return } fmt.Printf("%+v\n", got) assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status)) }) } func TestNotify(t *testing.T) { jsonData := []byte(`{ "tradeStatus": "SUCCESS", "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{ Config: config(), Queries: nil, Headers: HeaderBytes, Body: jsonData, } t.Run("TestNotify", func(t *testing.T) { got, err := zltx.Notify(ctx, in) if !assert.Nil(t, err) { t.Errorf("Notify() error = %v", err) return } fmt.Printf("%s \n", got.String()) assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status)) }) }