chore: 初始化仓库,排除二进制文件和覆盖率文件
This commit is contained in:
133
internal/handler/captcha_handler_test.go
Normal file
133
internal/handler/captcha_handler_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestCaptchaHandler_RequestValidation 测试验证码请求验证逻辑
|
||||
func TestCaptchaHandler_RequestValidation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
captchaID string
|
||||
dx int
|
||||
wantValid bool
|
||||
}{
|
||||
{
|
||||
name: "有效的请求参数",
|
||||
captchaID: "captcha-123",
|
||||
dx: 100,
|
||||
wantValid: true,
|
||||
},
|
||||
{
|
||||
name: "captchaID为空",
|
||||
captchaID: "",
|
||||
dx: 100,
|
||||
wantValid: false,
|
||||
},
|
||||
{
|
||||
name: "dx为0(可能有效)",
|
||||
captchaID: "captcha-123",
|
||||
dx: 0,
|
||||
wantValid: true, // dx为0也可能是有效的(用户没有滑动)
|
||||
},
|
||||
{
|
||||
name: "dx为负数(可能无效)",
|
||||
captchaID: "captcha-123",
|
||||
dx: -10,
|
||||
wantValid: true, // 负数也可能是有效的,取决于业务逻辑
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
isValid := tt.captchaID != ""
|
||||
if isValid != tt.wantValid {
|
||||
t.Errorf("Request validation failed: got %v, want %v", isValid, tt.wantValid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCaptchaHandler_ResponseFormat 测试响应格式逻辑
|
||||
func TestCaptchaHandler_ResponseFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
valid bool
|
||||
wantCode int
|
||||
wantStatus string
|
||||
}{
|
||||
{
|
||||
name: "验证成功",
|
||||
valid: true,
|
||||
wantCode: 200,
|
||||
wantStatus: "验证成功",
|
||||
},
|
||||
{
|
||||
name: "验证失败",
|
||||
valid: false,
|
||||
wantCode: 400,
|
||||
wantStatus: "验证失败,请重试",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 验证响应格式逻辑
|
||||
var code int
|
||||
var status string
|
||||
if tt.valid {
|
||||
code = 200
|
||||
status = "验证成功"
|
||||
} else {
|
||||
code = 400
|
||||
status = "验证失败,请重试"
|
||||
}
|
||||
|
||||
if code != tt.wantCode {
|
||||
t.Errorf("Response code = %d, want %d", code, tt.wantCode)
|
||||
}
|
||||
if status != tt.wantStatus {
|
||||
t.Errorf("Response status = %q, want %q", status, tt.wantStatus)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCaptchaHandler_ErrorHandling 测试错误处理逻辑
|
||||
func TestCaptchaHandler_ErrorHandling(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
hasError bool
|
||||
wantCode int
|
||||
wantError bool
|
||||
}{
|
||||
{
|
||||
name: "生成验证码失败",
|
||||
hasError: true,
|
||||
wantCode: 500,
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "验证验证码失败",
|
||||
hasError: true,
|
||||
wantCode: 500,
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "参数错误",
|
||||
hasError: true,
|
||||
wantCode: 400,
|
||||
wantError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 验证错误处理逻辑
|
||||
if tt.hasError && !tt.wantError {
|
||||
t.Error("Error handling logic failed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user