fix: add missing test coverage for Task 4 config integration

- Add validation tests for least_time and sticky configs
- Add algorithm tests for least_time and sticky
- Add SameSite validation in validateProxy
This commit is contained in:
xfy 2026-06-08 18:01:21 +08:00
parent 88a2c1fc1b
commit cb1f86298e
3 changed files with 82 additions and 0 deletions

View File

@ -519,6 +519,12 @@ func validateProxy(p *ProxyConfig) error {
if p.Sticky.FallbackAlgo != "" && !loadbalance.IsValidAlgorithm(p.Sticky.FallbackAlgo) {
return fmt.Errorf("无效的 sticky fallback_balance: %s", p.Sticky.FallbackAlgo)
}
if p.Sticky.SameSite != "" {
validSameSites := []string{"Lax", "Strict", "None"}
if !slices.Contains(validSameSites, p.Sticky.SameSite) {
return fmt.Errorf("无效的 sticky same_site: %s有效值: Lax, Strict, None", p.Sticky.SameSite)
}
}
}
// 验证故障转移配置

View File

@ -170,6 +170,80 @@ func TestValidateProxy(t *testing.T) {
wantErr: true,
errMsg: "无效的负载均衡算法",
},
{
name: "有效 least_time 配置 metric=header",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "least_time",
LeastTime: LeastTimeConfig{Metric: "header"},
},
wantErr: false,
},
{
name: "有效 least_time 配置 metric=last_byte",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "least_time",
LeastTime: LeastTimeConfig{Metric: "last_byte"},
},
wantErr: false,
},
{
name: "无效 least_time metric",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "least_time",
LeastTime: LeastTimeConfig{Metric: "invalid"},
},
wantErr: true,
errMsg: "无效的 least_time metric",
},
{
name: "有效 sticky 配置",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "sticky",
Sticky: StickyConfig{Enabled: true, FallbackAlgo: "round_robin"},
},
wantErr: false,
},
{
name: "无效 sticky enabled=false",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "sticky",
Sticky: StickyConfig{Enabled: false},
},
wantErr: true,
errMsg: "sticky.enabled 必须为 true",
},
{
name: "无效 sticky fallback_balance",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "sticky",
Sticky: StickyConfig{Enabled: true, FallbackAlgo: "invalid"},
},
wantErr: true,
errMsg: "无效的 sticky fallback_balance",
},
{
name: "无效 sticky same_site",
config: ProxyConfig{
Path: "/api",
Targets: []ProxyTarget{{URL: "http://backend:8080"}},
LoadBalance: "sticky",
Sticky: StickyConfig{Enabled: true, SameSite: "Invalid"},
},
wantErr: true,
errMsg: "无效的 sticky same_site",
},
}
for _, tt := range tests {

View File

@ -940,6 +940,8 @@ func TestIsValidAlgorithm(t *testing.T) {
{"ip_hash", "ip_hash", true},
{"consistent_hash", "consistent_hash", true},
{"random", "random", true},
{"least_time", "least_time", true},
{"sticky", "sticky", true},
{"invalid", "invalid", false},
{"empty", "", true}, // 空字符串有效(使用默认值)
{"unknown", "unknown-algorithm", false},