添加文件: server_tb_LinkedMall/internal/test/example_test.go

This commit is contained in:
renzhiyuan 2026-08-27 16:19:10 +08:00
parent 5dbf93956e
commit e578b7d058
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"server_tb_LinkedMall/internal/config"
"server_tb_LinkedMall/internal/entities"
"server_tb_LinkedMall/internal/router"
"server_tb_LinkedMall/internal/service"
)
// setupTestApp 初始化测试应用
func setupTestApp(t *testing.T) (*fiber.App, *service.LinkedMallService) {
t.Helper()
cfg := &config.Config{
ServerPort: "8080",
AccessKeyId: "test-ak",
AccessKeySecret: "test-sk",
RegionId: "cn-zhangjiakou",
Endpoint: "linkedmall.cn-zhangjiakou.aliyuncs.com",
BasePath: "/opensaas-s2b/opensaas-s2b-biz-trade/v2",
DefaultPurchaserId: "test-purchaser-id",
}
svc, err := service.NewLinkedMallService(cfg)
if err != nil {
t.Skipf("跳过测试无法初始化服务可能需要有效AK/SK: %v", err)
}
app := fiber.New()
router.SetupRouter(app, svc)
return app, svc
}
// performRequest 执行测试HTTP请求
func performRequest(app *fiber.App, method, path string, body interface{}) *http.Response {
var reqBody []byte
if body != nil {
reqBody, _ = json.Marshal(body)
}
req := httptest.NewRequest(method, path, bytes.NewReader(reqBody))
req.Header.Set("Content-Type", "application/json")
resp, _ := app.Test(req, -1)
return resp
}
// TestHealthCheck 健康检查测试
func TestHealthCheck(t *testing.T) {
app, _ := setupTestApp(t)
resp