From cb1f86298ef88c90c6a00b494154f1d7a700092a Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 8 Jun 2026 18:01:21 +0800 Subject: [PATCH] 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 --- internal/config/validate.go | 6 +++ internal/config/validate_test.go | 74 +++++++++++++++++++++++++++ internal/loadbalance/balancer_test.go | 2 + 3 files changed, 82 insertions(+) diff --git a/internal/config/validate.go b/internal/config/validate.go index 2d8c4ac..6d591e8 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -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) + } + } } // 验证故障转移配置 diff --git a/internal/config/validate_test.go b/internal/config/validate_test.go index 8cdf157..7569d20 100644 --- a/internal/config/validate_test.go +++ b/internal/config/validate_test.go @@ -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 { diff --git a/internal/loadbalance/balancer_test.go b/internal/loadbalance/balancer_test.go index c6d1222..92496fb 100644 --- a/internal/loadbalance/balancer_test.go +++ b/internal/loadbalance/balancer_test.go @@ -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},