242 lines
6.9 KiB
Go
242 lines
6.9 KiB
Go
|
|
package xingwei
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"encoding/json"
|
|||
|
|
"testing"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func TestXingweiService_CreateSign(t *testing.T) {
|
|||
|
|
// 创建测试配置 - 使用nil logger来避免日志问题
|
|||
|
|
service := NewXingweiService(
|
|||
|
|
"https://sjztyh.chengdaoji.cn/dataCenterManageApi/manage/interface/doc/api/handle",
|
|||
|
|
"test_api_id",
|
|||
|
|
"test_api_key",
|
|||
|
|
nil, // 使用nil logger
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 测试签名生成
|
|||
|
|
timestamp := int64(1743474772049)
|
|||
|
|
sign := service.createSign(timestamp)
|
|||
|
|
|
|||
|
|
// 验证签名不为空
|
|||
|
|
if sign == "" {
|
|||
|
|
t.Error("签名不能为空")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证签名长度(MD5应该是32位十六进制字符串)
|
|||
|
|
if len(sign) != 32 {
|
|||
|
|
t.Errorf("签名长度应该是32位,实际是%d位", len(sign))
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("生成的签名: %s", sign)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestXingweiService_CallAPI(t *testing.T) {
|
|||
|
|
// 创建测试配置 - 使用nil logger来避免日志问题
|
|||
|
|
service := NewXingweiService(
|
|||
|
|
"https://sjztyh.chengdaoji.cn/dataCenterManageApi/manage/interface/doc/api/handle",
|
|||
|
|
"test_api_id",
|
|||
|
|
"test_api_key",
|
|||
|
|
nil, // 使用nil logger
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 创建测试上下文
|
|||
|
|
ctx := context.Background()
|
|||
|
|
|
|||
|
|
// 测试参数
|
|||
|
|
projectID := "test_project_id"
|
|||
|
|
params := map[string]interface{}{
|
|||
|
|
"test_param": "test_value",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 注意:这个测试会实际发送HTTP请求,所以可能会失败
|
|||
|
|
// 在实际使用中,应该使用mock或者测试服务器
|
|||
|
|
resp, err := service.CallAPI(ctx, projectID, params)
|
|||
|
|
|
|||
|
|
// 由于这是真实的外部API调用,我们主要测试错误处理
|
|||
|
|
if err != nil {
|
|||
|
|
t.Logf("预期的错误(真实API调用): %v", err)
|
|||
|
|
} else {
|
|||
|
|
t.Logf("API调用成功,响应长度: %d", len(resp))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestXingweiService_GenerateRequestID(t *testing.T) {
|
|||
|
|
// 创建测试配置 - 使用nil logger来避免日志问题
|
|||
|
|
service := NewXingweiService(
|
|||
|
|
"https://sjztyh.chengdaoji.cn/dataCenterManageApi/manage/interface/doc/api/handle",
|
|||
|
|
"test_api_id",
|
|||
|
|
"test_api_key",
|
|||
|
|
nil, // 使用nil logger
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// 测试请求ID生成
|
|||
|
|
requestID1 := service.generateRequestID()
|
|||
|
|
requestID2 := service.generateRequestID()
|
|||
|
|
|
|||
|
|
// 验证请求ID不为空
|
|||
|
|
if requestID1 == "" || requestID2 == "" {
|
|||
|
|
t.Error("请求ID不能为空")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证请求ID应该以xingwei_开头
|
|||
|
|
if len(requestID1) < 8 || requestID1[:8] != "xingwei_" {
|
|||
|
|
t.Error("请求ID应该以xingwei_开头")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证两次生成的请求ID应该不同
|
|||
|
|
if requestID1 == requestID2 {
|
|||
|
|
t.Error("两次生成的请求ID应该不同")
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("请求ID1: %s", requestID1)
|
|||
|
|
t.Logf("请求ID2: %s", requestID2)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestGetXingweiErrorMessage(t *testing.T) {
|
|||
|
|
// 测试已知错误码(使用常量)
|
|||
|
|
testCases := []struct {
|
|||
|
|
code int
|
|||
|
|
expected string
|
|||
|
|
}{
|
|||
|
|
{CodeSuccess, "操作成功"},
|
|||
|
|
{CodeSystemError, "系统内部错误"},
|
|||
|
|
{CodeMerchantError, "商家相关报错(商家不存在、商家被禁用、商家余额不足)"},
|
|||
|
|
{CodeAccountExpired, "账户已过期"},
|
|||
|
|
{CodeIPWhitelistMissing, "未添加ip白名单"},
|
|||
|
|
{CodeUnauthorized, "未授权调用该接口"},
|
|||
|
|
{CodeProductIDError, "产品id错误"},
|
|||
|
|
{CodeInterfaceDisabled, "接口被停用"},
|
|||
|
|
{CodeQueryException, "接口查询异常,请联系技术人员"},
|
|||
|
|
{CodeNotFound, "未查询到结果"},
|
|||
|
|
{9999, "未知错误码: 9999"}, // 测试未知错误码
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for _, tc := range testCases {
|
|||
|
|
result := GetXingweiErrorMessage(tc.code)
|
|||
|
|
if result != tc.expected {
|
|||
|
|
t.Errorf("错误码 %d 的消息不正确,期望: %s, 实际: %s", tc.code, tc.expected, result)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestXingweiResponseParsing(t *testing.T) {
|
|||
|
|
// 测试响应结构解析
|
|||
|
|
testCases := []struct {
|
|||
|
|
name string
|
|||
|
|
response string
|
|||
|
|
expectedCode int
|
|||
|
|
}{
|
|||
|
|
{
|
|||
|
|
name: "成功响应",
|
|||
|
|
response: `{"msg": "操作成功", "code": 200, "data": {"result": "test"}}`,
|
|||
|
|
expectedCode: CodeSuccess,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
name: "商家错误",
|
|||
|
|
response: `{"msg": "商家相关报错", "code": 3001, "data": null}`,
|
|||
|
|
expectedCode: CodeMerchantError,
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
name: "未查询到结果",
|
|||
|
|
response: `{"msg": "未查询到结果", "code": 6000, "data": null}`,
|
|||
|
|
expectedCode: CodeNotFound,
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for _, tc := range testCases {
|
|||
|
|
t.Run(tc.name, func(t *testing.T) {
|
|||
|
|
var resp XingweiResponse
|
|||
|
|
err := json.Unmarshal([]byte(tc.response), &resp)
|
|||
|
|
if err != nil {
|
|||
|
|
t.Errorf("解析响应失败: %v", err)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if resp.Code != tc.expectedCode {
|
|||
|
|
t.Errorf("错误码不匹配,期望: %d, 实际: %d", tc.expectedCode, resp.Code)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试错误消息获取
|
|||
|
|
errorMsg := GetXingweiErrorMessage(resp.Code)
|
|||
|
|
if errorMsg == "" {
|
|||
|
|
t.Errorf("无法获取错误码 %d 的消息", resp.Code)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("响应: %+v, 错误消息: %s", resp, errorMsg)
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// TestXingweiErrorHandling 测试错误处理逻辑
|
|||
|
|
func TestXingweiErrorHandling(t *testing.T) {
|
|||
|
|
// 注意:这个测试主要验证常量定义和错误消息,不需要实际的服务实例
|
|||
|
|
|
|||
|
|
// 测试查空错误
|
|||
|
|
t.Run("NotFound错误", func(t *testing.T) {
|
|||
|
|
// 模拟返回查空响应
|
|||
|
|
response := `{"msg": "未查询到结果", "code": 6000, "data": null}`
|
|||
|
|
var xingweiResp XingweiResponse
|
|||
|
|
err := json.Unmarshal([]byte(response), &xingweiResp)
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("解析响应失败: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证状态码
|
|||
|
|
if xingweiResp.Code != CodeNotFound {
|
|||
|
|
t.Errorf("期望状态码 %d, 实际 %d", CodeNotFound, xingweiResp.Code)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证错误消息
|
|||
|
|
errorMsg := GetXingweiErrorMessage(xingweiResp.Code)
|
|||
|
|
if errorMsg != "未查询到结果" {
|
|||
|
|
t.Errorf("期望错误消息 '未查询到结果', 实际 '%s'", errorMsg)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("查空错误测试通过: 状态码=%d, 消息=%s", xingweiResp.Code, errorMsg)
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 测试系统错误
|
|||
|
|
t.Run("SystemError错误", func(t *testing.T) {
|
|||
|
|
response := `{"msg": "系统内部错误", "code": 500, "data": null}`
|
|||
|
|
var xingweiResp XingweiResponse
|
|||
|
|
err := json.Unmarshal([]byte(response), &xingweiResp)
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("解析响应失败: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if xingweiResp.Code != CodeSystemError {
|
|||
|
|
t.Errorf("期望状态码 %d, 实际 %d", CodeSystemError, xingweiResp.Code)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
errorMsg := GetXingweiErrorMessage(xingweiResp.Code)
|
|||
|
|
if errorMsg != "系统内部错误" {
|
|||
|
|
t.Errorf("期望错误消息 '系统内部错误', 实际 '%s'", errorMsg)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("系统错误测试通过: 状态码=%d, 消息=%s", xingweiResp.Code, errorMsg)
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 测试成功响应
|
|||
|
|
t.Run("Success响应", func(t *testing.T) {
|
|||
|
|
response := `{"msg": "操作成功", "code": 200, "data": {"result": "test"}}`
|
|||
|
|
var xingweiResp XingweiResponse
|
|||
|
|
err := json.Unmarshal([]byte(response), &xingweiResp)
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("解析响应失败: %v", err)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if xingweiResp.Code != CodeSuccess {
|
|||
|
|
t.Errorf("期望状态码 %d, 实际 %d", CodeSuccess, xingweiResp.Code)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
errorMsg := GetXingweiErrorMessage(xingweiResp.Code)
|
|||
|
|
if errorMsg != "操作成功" {
|
|||
|
|
t.Errorf("期望错误消息 '操作成功', 实际 '%s'", errorMsg)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
t.Logf("成功响应测试通过: 状态码=%d, 消息=%s", xingweiResp.Code, errorMsg)
|
|||
|
|
})
|
|||
|
|
}
|