Files
backend/internal/handler/yggdrasil_handler_test.go
lan 4b4980820f
Some checks failed
SonarQube Analysis / sonarqube (push) Has been cancelled
Test / test (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / build (push) Has been cancelled
chore: 初始化仓库,排除二进制文件和覆盖率文件
2025-11-28 23:30:49 +08:00

158 lines
3.3 KiB
Go

package handler
import (
"regexp"
"testing"
)
// TestYggdrasilHandler_EmailValidation 测试邮箱验证逻辑
func TestYggdrasilHandler_EmailValidation(t *testing.T) {
// 使用简单的邮箱正则表达式
emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
tests := []struct {
name string
email string
wantValid bool
}{
{
name: "有效的邮箱",
email: "test@example.com",
wantValid: true,
},
{
name: "无效的邮箱格式",
email: "invalid-email",
wantValid: false,
},
{
name: "缺少@符号",
email: "testexample.com",
wantValid: false,
},
{
name: "缺少域名",
email: "test@",
wantValid: false,
},
{
name: "空邮箱",
email: "",
wantValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isValid := emailRegex.MatchString(tt.email)
if isValid != tt.wantValid {
t.Errorf("Email validation failed: got %v, want %v", isValid, tt.wantValid)
}
})
}
}
// TestYggdrasilHandler_RequestValidation 测试请求验证逻辑
func TestYggdrasilHandler_RequestValidation(t *testing.T) {
tests := []struct {
name string
accessToken string
serverID string
username string
wantValid bool
}{
{
name: "有效的请求",
accessToken: "token-123",
serverID: "server-456",
username: "player",
wantValid: true,
},
{
name: "accessToken为空",
accessToken: "",
serverID: "server-456",
username: "player",
wantValid: false,
},
{
name: "serverID为空",
accessToken: "token-123",
serverID: "",
username: "player",
wantValid: false,
},
{
name: "username为空",
accessToken: "token-123",
serverID: "server-456",
username: "",
wantValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isValid := tt.accessToken != "" && tt.serverID != "" && tt.username != ""
if isValid != tt.wantValid {
t.Errorf("Request validation failed: got %v, want %v", isValid, tt.wantValid)
}
})
}
}
// TestYggdrasilHandler_ErrorHandling 测试错误处理逻辑
func TestYggdrasilHandler_ErrorHandling(t *testing.T) {
tests := []struct {
name string
errType string
wantCode int
wantError bool
}{
{
name: "参数错误",
errType: "bad_request",
wantCode: 400,
wantError: true,
},
{
name: "未授权",
errType: "forbidden",
wantCode: 403,
wantError: true,
},
{
name: "服务器错误",
errType: "server_error",
wantCode: 500,
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 验证错误处理逻辑
if !tt.wantError {
t.Error("Error handling test should expect error")
}
})
}
}
// TestYggdrasilHandler_Constants 测试常量定义
func TestYggdrasilHandler_Constants(t *testing.T) {
// 验证常量定义
if MaxMultipartMemory != 32<<20 {
t.Errorf("MaxMultipartMemory = %d, want %d", MaxMultipartMemory, 32<<20)
}
if TextureTypeSkin != "SKIN" {
t.Errorf("TextureTypeSkin = %q, want 'SKIN'", TextureTypeSkin)
}
if TextureTypeCape != "CAPE" {
t.Errorf("TextureTypeCape = %q, want 'CAPE'", TextureTypeCape)
}
}