From 1910b8f573cb1193ffc9caeaa171cddf4090b739 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Thu, 23 Jul 2026 17:07:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6:=20ymt=5Fv?= =?UTF-8?q?3/example=5Ftest.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ymt_v3/example_test.go | 138 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 ymt_v3/example_test.go diff --git a/ymt_v3/example_test.go b/ymt_v3/example_test.go new file mode 100644 index 0000000..7ffca0d --- /dev/null +++ b/ymt_v3/example_test.go @@ -0,0 +1,138 @@ +package ymt_v3 + +import ( + "context" + "fmt" + "testing" +) + +// 测试参数(请替换为实际值) +const ( + testAppID = "xxx" + testPrivateKey = `-----BEGIN PRIVATE KEY----- +xxx +-----END PRIVATE KEY-----` + testPublicKey = `-----BEGIN PUBLIC KEY----- +xxx +-----END PUBLIC KEY-----` + testKey = "xxxx" + testEncryptType = "aes" + testBaseURL = "https://gateway.dev.cdlsxd.cn" + testActivityNo = "xxxx" +) + +func TestClient_Order(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Order(context.Background(), &OrderRequest{ + OutBizNo: "order_001", + ActivityNo: testActivityNo, + Account: "18666666666", + NotifyURL: "https://notify.example.com/openapi", + }) + if err != nil { + t.Fatalf("Order failed: %v", err) + } + fmt.Printf("Order response: %+v\n", resp) +} + +func TestClient_Query(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Query(context.Background(), &QueryRequest{ + OutBizNo: "order_001", + }) + if err != nil { + t.Fatalf("Query failed: %v", err) + } + fmt.Printf("Query response: %+v\n", resp) +} + +func TestClient_Discard(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.Discard(context.Background(), &DiscardRequest{ + TradeNo: "7251449503000383488", + }) + if err != nil { + t.Fatalf("Discard failed: %v", err) + } + fmt.Printf("Discard response: %+v\n", resp) +} + +func TestClient_BatchOrder(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.BatchOrder(context.Background(), &BatchOrderRequest{ + OutBizNo: "batch_001", + ActivityNo: testActivityNo, + Number: 10, + NotifyURL: "https://notify.example.com/openapi", + }) + if err != nil { + t.Fatalf("BatchOrder failed: %v", err) + } + fmt.Printf("BatchOrder response: %+v\n", resp) +} + +func TestClient_BatchQuery(t *testing.T) { + client := NewClient(&Config{ + AppID: testAppID, + PrivateKey: testPrivateKey, + PublicKey: testPublicKey, + Key: testKey, + EncryptType: testEncryptType, + BaseURL: testBaseURL, + }) + + resp, err := client.BatchQuery(context.Background(), &BatchQueryRequest{ + TradeNo: "7251449503000383499", + }) + if err != nil { + t.Fatalf("BatchQuery failed: %v", err) + } + fmt.Printf("BatchQuery response: %+v\n", resp) +} + +func TestVerifyCallback(t *testing.T) { + // 模拟回调参数 + appID := "123456" + timestamp := "2026-06-22 15:30:00" + sign := "base64_encoded_signature" + data := `{"out_biz_no":"batch_001","trade_no":"7251449503000383499","status":"success","download_url":"https://oss.example.com/openapi_7251449503000383499.zip","zip_password":"123456"}` + + err := VerifyCallback(appID, timestamp, sign, data, testPublicKey, []byte(testKey), testEncryptType) + if err != nil { + t.Logf("VerifyCallback result: %v (expected if sign is invalid)", err) + } else { + t.Log("VerifyCallback success") + } +} \ No newline at end of file