添加文件: xy_sh/internal/test/example_test.go
This commit is contained in:
parent
91d52b392f
commit
851e5e3894
|
|
@ -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("下单接口测试通过")
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue