xy_sh-20260727101126/xy_sh/cmd/server/example_test.go

260 lines
6.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bytes"
"encoding/json"
"flag"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"xy_sh/internal/biz"
"xy_sh/internal/config"
"xy_sh/internal/data/impl"
"xy_sh/internal/entities"
"xy_sh/internal/router"
service2 "xy_sh/internal/service"
"xy_sh/pkg/crypto"
"xy_sh/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
const URL = "http://127.0.0.1:8085"
func init() {
crypto.InitCrypto("test_salt_12345", "test_key_16bytes")
}
func setupTestApp() *fiber.App {
app := fiber.New()
cfgPath := flag.String("config", "../../config/config_test.yaml", "Path to configuration file")
cfg, err := config.LoadConfig(*cfgPath)
db, _ := utils.NewGormDb(cfg)
orderImpl := impl.NewOrderImpl(db)
logImpl := impl.NewLogImpl(db)
ymt, err := ymtClient(cfg.Ymt)
if err != nil {
panic("初始化YMT客户端失败: " + err.Error())
}
bizs := biz.NewOrderStore(orderImpl, ymt, cfg)
service := service2.NewOrder(bizs, logImpl)
log.Infof("开始注册路由...") // 🔍 添加日志
router.SetupRoutes(app, service)
log.Infof("路由注册完成") // 🔍 添加日志
// 🔍 打印所有路由
for _, route := range app.GetRoutes() {
log.Infof("Route: %s %s", route.Method, route.Path)
}
return app
}
func makeEncryptedRequest(t *testing.T, app *fiber.App, path string, bizData interface{}) (*http.Response, []byte) {
t.Helper()
bizJSON, err := json.Marshal(bizData)
if err != nil {
t.Fatalf("业务数据序列化失败: %v", err)
}
encryptedData, err := crypto.SM4CBCEncrypt(bizJSON)
if err != nil {
t.Fatalf("加密失败: %v", err)
}
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
sign := crypto.GenerateSign(timestamp, encryptedData)
reqBody := entities.EncryptedRequest{
EncryptedData: encryptedData,
}
reqJSON, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(reqJSON))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("timestamp", timestamp)
req.Header.Set("sign", sign)
resp, err := app.Test(req, 50000)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return resp, body
}
func TestTestRoute(t *testing.T) {
app := setupTestApp()
req := httptest.NewRequest(http.MethodGet, "/test", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("请求失败: %v", err)
}
body, _ := io.ReadAll(resp.Body)
t.Logf("状态码: %d, 响应: %s", resp.StatusCode, string(body))
}
func TestCreateOrder(t *testing.T) {
app := setupTestApp()
bizData := entities.OrderRequest{
ActCode: "ACT007",
GoodsCode: "123456",
ActOrderNum: "TEST_ORDER_001",
Account: "19912345678",
CallbackUrl: "https://example.com/callback",
}
resp, body := makeEncryptedRequest(t, app, "/api/v1/order/create", bizData)
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码200实际: %d, 响应: %s", resp.StatusCode, string(body))
}
var commonResp entities.CommonResponse
if err := json.Unmarshal(body, &commonResp); err != nil {
t.Fatalf("响应解析失败: %v, 响应: %s", err, string(body))
}
if commonResp.Code != entities.CodeSuccess {
t.Errorf("期望code=0实际: %d, msg: %s", commonResp.Code, commonResp.Msg)
}
t.Logf("下单接口测试成功,响应: %s", string(body))
}
func TestQueryOrder(t *testing.T) {
app := setupTestApp()
queryBizData := entities.QueryOrderRequest{
//ActCode: "ACT007",
OrderNo: "936312532119846913",
}
resp, body := makeEncryptedRequest(t, app, "/api/v1/order/query", queryBizData)
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码200实际: %d, 响应: %s", resp.StatusCode, string(body))
}
var queryResp entities.CommonResponse
if err := json.Unmarshal(body, &queryResp); err != nil {
t.Fatalf("查询响应解析失败: %v", err)
}
if queryResp.Code != entities.CodeSuccess {
t.Errorf("查询失败code: %d, msg: %s", queryResp.Code, queryResp.Msg)
}
t.Logf("查询接口测试成功,响应: %s", string(body))
}
func TestWxRecharge(t *testing.T) {
app := setupTestApp()
bizData := entities.WxRechargeRequest{
ActCode: "FBG6vdYqGE4mGX7EH/woEg==",
OrderNo: "WX_TEST_ORDER_001",
GoodsCode: "0001",
ActOrderNum: "WX_ORDER_001",
AppId: "wx1234567890",
OpenId: "oABC1234567890",
}
resp, body := makeEncryptedRequest(t, app, "/api/v1/wx/recharge", bizData)
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码200实际: %d, 响应: %s", resp.StatusCode, string(body))
}
var commonResp entities.CommonResponse
if err := json.Unmarshal(body, &commonResp); err != nil {
t.Fatalf("响应解析失败: %v", err)
}
if commonResp.Code != entities.CodeSuccess {
t.Errorf("期望code=0实际: %d, msg: %s", commonResp.Code, commonResp.Msg)
}
t.Logf("微信立减金充值接口测试成功,响应: %s", string(body))
}
func TestCallback(t *testing.T) {
app := setupTestApp()
bizData := entities.CallbackRequest{
OrderNo: "HM202509291010001",
ActOrderNum: "XY2025092910100001",
Status: entities.StatusExpired,
Account: "19912345678",
}
resp, body := makeEncryptedRequest(t, app, "/api/v1/callback/notify", bizData)
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码200实际: %d, 响应: %s", resp.StatusCode, string(body))
}
if string(body) != "ok" {
t.Errorf("期望响应'ok',实际: %s", string(body))
}
t.Logf("回调通知接口测试成功")
}
func TestInvalidSign(t *testing.T) {
app := setupTestApp()
bizData := entities.OrderRequest{
ActCode: "ACT001",
GoodsCode: "123456",
ActOrderNum: "TEST_ORDER_003",
}
bizJSON, _ := json.Marshal(bizData)
encryptedData, _ := crypto.SM4CBCEncrypt(bizJSON)
timestamp := strconv.FormatInt(time.Now().UnixMilli(), 10)
wrongSign := "wrong_signature_12345"
reqBody := entities.EncryptedRequest{EncryptedData: encryptedData}
reqJSON, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/api/v1/order/create", bytes.NewReader(reqJSON))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("timestamp", timestamp)
req.Header.Set("sign", wrongSign)
resp, _ := app.Test(req)
if resp.StatusCode != http.StatusUnauthorized {
t.Errorf("期望状态码401签名验证失败实际: %d", resp.StatusCode)
}
t.Logf("签名验证失败测试成功")
}
func TestHealthCheck(t *testing.T) {
app := setupTestApp()
req := httptest.NewRequest(http.MethodGet, "/health", nil)
resp, err := app.Test(req)
if resp.StatusCode != http.StatusOK {
t.Errorf("期望状态码200实际: %d", resp.StatusCode)
}
t.Logf("健康检查接口测试成功,响应: %s", err)
}