125 lines
3.1 KiB
Go
125 lines
3.1 KiB
Go
package internal
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||
"github.com/stretchr/testify/assert"
|
||
"net/http"
|
||
"testing"
|
||
)
|
||
|
||
var srv = &ZLTXV1Service{}
|
||
|
||
func config() []byte {
|
||
c := &Config{
|
||
AppId: "1",
|
||
AppKey: "1e2bf7a04b8b1e6be5dc78d04e8639c9",
|
||
BaseUri: "http://test.openapi.1688sup.cn",
|
||
NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify",
|
||
MerchantId: 23369, // 23329
|
||
}
|
||
//c := &Config{
|
||
// AppId: "101",
|
||
// AppKey: "95E7EC7D4A394FF8D11788E5E436DE99",
|
||
// BaseUri: "https://openapi.1688sup.com",
|
||
// NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify",
|
||
// MerchantId: 25715,
|
||
//}
|
||
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) {
|
||
//d := []byte(`{"jdSmsCode":"123456"}`)
|
||
//s := base64.StdEncoding.EncodeToString(d)
|
||
//t.Logf("%s\n", s)
|
||
|
||
request := &proto.OrderRequest{
|
||
Config: config(),
|
||
Order: &proto.OrderRequest_Order{
|
||
OrderNo: "202501131732090710010002",
|
||
Account: "18666173766",
|
||
Quantity: 1,
|
||
//Extra: []byte(`{"jdCode":"123456"}`), // 京东e卡官方,拓展参数
|
||
Extra: []byte(`{"jdSmsCode":"123456"}`), // 京东e卡专票,拓展参数
|
||
},
|
||
Product: &proto.OrderRequest_Product{
|
||
ProductNo: "360", // 101 2255 360
|
||
Extra: []byte(`{}`),
|
||
},
|
||
}
|
||
t.Logf("%s\n", request.String())
|
||
return
|
||
t.Run("TestOrder", func(t *testing.T) {
|
||
got, err := srv.Order(context.Background(), request)
|
||
if err != nil {
|
||
t.Errorf("Order() error = %v", err)
|
||
return
|
||
}
|
||
t.Logf("%+v\n", got)
|
||
assert.Equal(t, int(proto.Status_ING), int(got.Result.Status))
|
||
})
|
||
}
|
||
|
||
func TestQuery(t *testing.T) {
|
||
c := config()
|
||
request := &proto.QueryRequest{
|
||
Config: c,
|
||
Order: &proto.QueryRequest_Order{
|
||
OrderNo: "test_plugin_zltx_v1_direct_1",
|
||
TradeNo: "",
|
||
Account: "",
|
||
Extra: nil,
|
||
},
|
||
}
|
||
t.Logf("%+v\n", c)
|
||
t.Run("TestQuery", func(t *testing.T) {
|
||
got, err := srv.Query(context.Background(), request)
|
||
if err != nil {
|
||
t.Errorf("Query() error = %v", err)
|
||
return
|
||
}
|
||
t.Logf("%+v\n", got)
|
||
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
|
||
})
|
||
}
|
||
|
||
func TestNotify(t *testing.T) {
|
||
in := &proto.NotifyRequest{
|
||
Config: config(),
|
||
Queries: nil,
|
||
Headers: []byte(`{"Authorization":["MD5 appid=1,sign=A419BC9E70FA82CC07030A041A4DEEC1"]}`),
|
||
Body: []byte(`{"merchantId":23369,"outTradeNo":"202501101143088400010016","tradeNo":"732200443106705409","status":"01","cardCode":"R7UVRxu5Zi8SockCksuZ3w=="}`),
|
||
}
|
||
t.Run("TestNotify", func(t *testing.T) {
|
||
got, err := srv.Notify(context.Background(), in)
|
||
if !assert.Nil(t, err) {
|
||
t.Errorf("Notify() error = %v", err)
|
||
return
|
||
}
|
||
|
||
responseHeaders := make(http.Header)
|
||
err = json.Unmarshal([]byte(got.Headers), &responseHeaders)
|
||
if err != nil {
|
||
t.Error(err)
|
||
return
|
||
}
|
||
for s, strings := range responseHeaders {
|
||
t.Logf("%s: %+v", s, strings)
|
||
}
|
||
|
||
fmt.Printf("%+v\n", got)
|
||
assert.Equal(t, int(proto.Status_SUCCESS), int(got.Result.Status))
|
||
})
|
||
}
|