From 851e5e38942804621e44415d871d5dfbced872a2 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Fri, 24 Jul 2026 18:13:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6:=20xy=5Fsh?= =?UTF-8?q?/internal/test/example=5Ftest.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xy_sh/internal/test/example_test.go | 99 +++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 xy_sh/internal/test/example_test.go diff --git a/xy_sh/internal/test/example_test.go b/xy_sh/internal/test/example_test.go new file mode 100644 index 0000000..a8c5cee --- /dev/null +++ b/xy_sh/internal/test/example_test.go @@ -0,0 +1,99 @@ +package test + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "testing" + + "xy_sh/pkg/crypto" +) + +// 测试配置(需与服务器配置一致) +var ( + testSM4Key = []byte("1234567890123456") + testSM3Salt = []byte("default_sm3_salt") + testBaseURL = "http://localhost:8080" +) + +// TestOrderCreate 模拟卡券/直充权益下单接口请求 +func TestOrderCreate(t *testing.T) { + // 1. 构造业务参数 + bizReq := map[string]interface{}{ + "actCode": "ACT001", + "goodsCode": "123456", + "actOrderNum": "00001", + "account": "19912345678", + "callbackUrl": "https://xxx/notice", + } + + // 2. 序列化并加密 + bizJSON, _ := json.Marshal(bizReq) + encryptedData, err := crypto.SM4CBCEncrypt(bizJSON, testSM4Key) + if err != nil { + t.Fatalf("SM4加密失败: %v", err) + } + + // 3. 生成签名 + timestamp := crypto.GenerateTimestampMillis() + sign := crypto.GenerateSign(timestamp, encryptedData, testSM3Salt) + + // 4. 构造请求体 + reqBody := map[string]string{ + "encryptedData": encryptedData, + } + reqBodyJSON, _ := json.Marshal(reqBody) + + // 5. 发送HTTP请求 + req, err := http.NewRequest("POST", testBaseURL+"/api/order/create", bytes.NewReader(reqBodyJSON)) + if err != nil { + t.Fatalf("创建请求失败: %v", err) + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("timestamp", timestamp) + req.Header.Set("sign", sign) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + t.Fatalf("发送请求失败: %v", err) + } + defer resp.Body.Close() + + // 6. 读取响应 + respBody, _ := io.ReadAll(resp.Body) + fmt.Printf("响应状态码: %d\n", resp.StatusCode) + fmt.Printf("响应体: %s\n", string(respBody)) + + // 7. 解析响应 + var apiResp struct { + Code int `json:"code"` + Msg string `json:"msg"` + Data json.RawMessage `json:"data"` + } + if err := json.Unmarshal(respBody, &apiResp); err != nil { + t.Fatalf("解析响应失败: %v", err) + } + + if apiResp.Code != 0 { + t.Fatalf("请求失败: code=%d, msg=%s", apiResp.Code, apiResp.Msg) + } + + // 8. 解密响应数据 + if len(apiResp.Data) > 0 { + // data 是字符串(加密后的数据) + var encryptedDataStr string + if err := json.Unmarshal(apiResp.Data, &encryptedDataStr); err != nil { + t.Fatalf("解析加密数据失败: %v", err) + } + plaintext, err := crypto.SM4CBCDecrypt(encryptedDataStr, testSM4Key) + if err != nil { + t.Fatalf("解密响应数据失败: %v", err) + } + fmt.Printf("解密后业务数据: %s\n", string(plaintext)) + } + + t.Log("下单接口测试通过") +} \ No newline at end of file