test(config,app,server): 更新测试适配 servers 配置格式

- 所有测试用例使用 Servers[0] 替代 Server
- 断言检查 servers: 替代 server:
- 生成配置测试验证新格式输出

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-15 10:34:04 +08:00
parent 168d34d58d
commit d99011aab6
4 changed files with 164 additions and 164 deletions

View File

@ -196,7 +196,7 @@ func TestRun(t *testing.T) {
genConfig: true, genConfig: true,
outputPath: "", outputPath: "",
wantExitCode: 0, wantExitCode: 0,
wantContains: "server:", wantContains: "servers:",
}, },
{ {
name: "生成配置输出到文件", name: "生成配置输出到文件",
@ -245,7 +245,7 @@ func TestRun(t *testing.T) {
data, err := os.ReadFile(tt.outputPath) data, err := os.ReadFile(tt.outputPath)
if err != nil { if err != nil {
t.Errorf("读取生成的配置文件失败: %v", err) t.Errorf("读取生成的配置文件失败: %v", err)
} else if !strings.Contains(string(data), "server:") { } else if !strings.Contains(string(data), "servers:") {
t.Errorf("生成的配置文件应包含 'server:', 实际内容: %s", string(data)[:100]) t.Errorf("生成的配置文件应包含 'server:', 实际内容: %s", string(data)[:100])
} }
} }
@ -268,7 +268,7 @@ func TestGenerateConfig(t *testing.T) {
} }
// 验证输出包含基本配置结构 // 验证输出包含基本配置结构
expectedFields := []string{"server:", "listen:", "logging:", "performance:", "monitoring:"} expectedFields := []string{"servers:", "listen:", "logging:", "performance:", "monitoring:"}
for _, field := range expectedFields { for _, field := range expectedFields {
if !strings.Contains(stdout, field) { if !strings.Contains(stdout, field) {
t.Errorf("输出应包含 %q", field) t.Errorf("输出应包含 %q", field)
@ -302,7 +302,7 @@ func TestGenerateConfig(t *testing.T) {
} }
content := string(data) content := string(data)
expectedFields := []string{"server:", "listen:", "logging:", "performance:", "monitoring:"} expectedFields := []string{"servers:", "listen:", "logging:", "performance:", "monitoring:"}
for _, field := range expectedFields { for _, field := range expectedFields {
if !strings.Contains(content, field) { if !strings.Contains(content, field) {
t.Errorf("配置文件应包含 %q", field) t.Errorf("配置文件应包含 %q", field)
@ -361,9 +361,9 @@ func TestHandleSignal_SIGQUIT(t *testing.T) {
// 创建一个简单的 App // 创建一个简单的 App
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", // 使用随机端口 Listen: ":0", // 使用随机端口
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -382,9 +382,9 @@ func TestHandleSignal_SIGQUIT(t *testing.T) {
func TestHandleSignal_SIGTERM(t *testing.T) { func TestHandleSignal_SIGTERM(t *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.srv = server.New(app.cfg) app.srv = server.New(app.cfg)
@ -400,9 +400,9 @@ func TestHandleSignal_SIGTERM(t *testing.T) {
func TestHandleSignal_SIGINT(t *testing.T) { func TestHandleSignal_SIGINT(t *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.srv = server.New(app.cfg) app.srv = server.New(app.cfg)
@ -432,9 +432,9 @@ logging:
app := NewApp(cfgPath) app := NewApp(cfgPath)
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -449,9 +449,9 @@ logging:
func TestHandleSignal_SIGUSR1(t *testing.T) { func TestHandleSignal_SIGUSR1(t *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
Logging: config.LoggingConfig{ Logging: config.LoggingConfig{
Error: config.ErrorLogConfig{ Error: config.ErrorLogConfig{
Level: "info", Level: "info",
@ -471,9 +471,9 @@ func TestHandleSignal_SIGUSR1(t *testing.T) {
func TestHandleSignal_Unknown(t *testing.T) { func TestHandleSignal_Unknown(t *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -537,17 +537,17 @@ logging:
app := NewApp(cfgPath) app := NewApp(cfgPath)
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.reloadConfig() app.reloadConfig()
// 验证配置已更新 // 验证配置已更新
if app.cfg.Server.Listen != ":9090" { if app.cfg.Servers[0].Listen != ":9090" {
t.Errorf("Expected listen ':9090', got '%s'", app.cfg.Server.Listen) t.Errorf("Expected listen ':9090', got '%s'", app.cfg.Servers[0].Listen)
} }
} }
@ -555,9 +555,9 @@ logging:
func TestSetupSignalHandlers(_ *testing.T) { func TestSetupSignalHandlers(_ *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -571,9 +571,9 @@ func TestSetupSignalHandlers(_ *testing.T) {
func TestHandleSignal_SIGUSR2(t *testing.T) { func TestHandleSignal_SIGUSR2(t *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.srv = server.New(app.cfg) app.srv = server.New(app.cfg)
@ -591,9 +591,9 @@ func TestHandleSignal_SIGUSR2(t *testing.T) {
func TestGracefulUpgrade_NoListener(_ *testing.T) { func TestGracefulUpgrade_NoListener(_ *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.srv = server.New(app.cfg) app.srv = server.New(app.cfg)
@ -658,9 +658,9 @@ func TestAppFields(t *testing.T) {
func TestShutdownHTTP3_WithServer(_ *testing.T) { func TestShutdownHTTP3_WithServer(_ *testing.T) {
app := NewApp("") app := NewApp("")
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
HTTP3: config.HTTP3Config{ HTTP3: config.HTTP3Config{
Enabled: false, // 禁用,避免实际启动 Enabled: false, // 禁用,避免实际启动
}, },
@ -716,23 +716,23 @@ logging:
app := NewApp(cfgPath1) app := NewApp(cfgPath1)
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":7070", Listen: ":7070",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
// 第一次重载 // 第一次重载
app.reloadConfig() app.reloadConfig()
if app.cfg.Server.Listen != ":8080" { if app.cfg.Servers[0].Listen != ":8080" {
t.Errorf("After first reload: listen = %q, want :8080", app.cfg.Server.Listen) t.Errorf("After first reload: listen = %q, want :8080", app.cfg.Servers[0].Listen)
} }
// 更改配置路径并重载 // 更改配置路径并重载
app.cfgPath = cfgPath2 app.cfgPath = cfgPath2
app.reloadConfig() app.reloadConfig()
if app.cfg.Server.Listen != ":9090" { if app.cfg.Servers[0].Listen != ":9090" {
t.Errorf("After second reload: listen = %q, want :9090", app.cfg.Server.Listen) t.Errorf("After second reload: listen = %q, want :9090", app.cfg.Servers[0].Listen)
} }
} }
@ -769,9 +769,9 @@ logging:
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
app := NewApp(cfgPath) app := NewApp(cfgPath)
app.cfg = &config.Config{ app.cfg = &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
app.logger = logging.NewAppLogger(&config.LoggingConfig{}) app.logger = logging.NewAppLogger(&config.LoggingConfig{})
app.srv = server.New(app.cfg) app.srv = server.New(app.cfg)

View File

@ -47,14 +47,14 @@ monitoring:
t.Fatalf("Load() 失败: %v", err) t.Fatalf("Load() 失败: %v", err)
} }
if cfg.Server.Listen != ":8080" { if cfg.Servers[0].Listen != ":8080" {
t.Errorf("Server.Listen = %q, want %q", cfg.Server.Listen, ":8080") t.Errorf("Servers[0].Listen = %q, want %q", cfg.Servers[0].Listen, ":8080")
} }
if cfg.Server.Static[0].Root != "/var/www" { if cfg.Servers[0].Static[0].Root != "/var/www" {
t.Errorf("Server.Static.Root = %q, want %q", cfg.Server.Static[0].Root, "/var/www") t.Errorf("Servers[0].Static.Root = %q, want %q", cfg.Servers[0].Static[0].Root, "/var/www")
} }
if len(cfg.Server.Static[0].Index) != 1 || cfg.Server.Static[0].Index[0] != "index.html" { if len(cfg.Servers[0].Static[0].Index) != 1 || cfg.Servers[0].Static[0].Index[0] != "index.html" {
t.Errorf("Server.Static.Index = %v, want [index.html]", cfg.Server.Static[0].Index) t.Errorf("Servers[0].Static.Index = %v, want [index.html]", cfg.Servers[0].Static[0].Index)
} }
}) })
@ -148,11 +148,11 @@ server:
t.Fatalf("LoadFromString() 失败: %v", err) t.Fatalf("LoadFromString() 失败: %v", err)
} }
if cfg.Server.Listen != ":9090" { if cfg.Servers[0].Listen != ":9090" {
t.Errorf("Server.Listen = %q, want %q", cfg.Server.Listen, ":9090") t.Errorf("Servers[0].Listen = %q, want %q", cfg.Servers[0].Listen, ":9090")
} }
if cfg.Server.Static[0].Root != "/app/public" { if cfg.Servers[0].Static[0].Root != "/app/public" {
t.Errorf("Server.Static.Root = %q, want %q", cfg.Server.Static[0].Root, "/app/public") t.Errorf("Servers[0].Static.Root = %q, want %q", cfg.Servers[0].Static[0].Root, "/app/public")
} }
}) })
@ -192,14 +192,14 @@ logging:
func TestSave(t *testing.T) { func TestSave(t *testing.T) {
t.Run("正常保存", func(t *testing.T) { t.Run("正常保存", func(t *testing.T) {
cfg := &Config{ cfg := &Config{
Server: ServerConfig{ Servers: []ServerConfig{{
Listen: ":8080", Listen: ":8080",
Static: []StaticConfig{{ Static: []StaticConfig{{
Path: "/", Path: "/",
Root: "/var/www", Root: "/var/www",
Index: []string{"index.html"}, Index: []string{"index.html"},
}}, }},
}, }},
} }
tmpDir := t.TempDir() tmpDir := t.TempDir()
@ -215,19 +215,19 @@ func TestSave(t *testing.T) {
t.Fatalf("重新加载配置失败: %v", err) t.Fatalf("重新加载配置失败: %v", err)
} }
if loaded.Server.Listen != cfg.Server.Listen { if loaded.Servers[0].Listen != cfg.Servers[0].Listen {
t.Errorf("loaded.Server.Listen = %q, want %q", loaded.Server.Listen, cfg.Server.Listen) t.Errorf("loaded.Servers[0].Listen = %q, want %q", loaded.Servers[0].Listen, cfg.Servers[0].Listen)
} }
if loaded.Server.Static[0].Root != cfg.Server.Static[0].Root { if loaded.Servers[0].Static[0].Root != cfg.Servers[0].Static[0].Root {
t.Errorf("loaded.Server.Static[0].Root = %q, want %q", loaded.Server.Static[0].Root, cfg.Server.Static[0].Root) t.Errorf("loaded.Servers[0].Static[0].Root = %q, want %q", loaded.Servers[0].Static[0].Root, cfg.Servers[0].Static[0].Root)
} }
}) })
t.Run("无效路径", func(t *testing.T) { t.Run("无效路径", func(t *testing.T) {
cfg := &Config{ cfg := &Config{
Server: ServerConfig{ Servers: []ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
err := Save(cfg, "/nonexistent/directory/config.yaml") err := Save(cfg, "/nonexistent/directory/config.yaml")
@ -263,7 +263,7 @@ func TestSave(t *testing.T) {
t.Run("保存并加载完整配置", func(t *testing.T) { t.Run("保存并加载完整配置", func(t *testing.T) {
cfg := &Config{ cfg := &Config{
Server: ServerConfig{ Servers: []ServerConfig{{
Listen: ":8443", Listen: ":8443",
Name: "default", Name: "default",
Static: []StaticConfig{{ Static: []StaticConfig{{
@ -292,7 +292,7 @@ func TestSave(t *testing.T) {
Burst: 200, Burst: 200,
}, },
}, },
}, }},
Logging: LoggingConfig{ Logging: LoggingConfig{
Access: AccessLogConfig{ Access: AccessLogConfig{
Path: "/var/log/access.log", Path: "/var/log/access.log",
@ -325,14 +325,14 @@ func TestSave(t *testing.T) {
} }
// 验证关键字段 // 验证关键字段
if loaded.Server.Listen != cfg.Server.Listen { if loaded.Servers[0].Listen != cfg.Servers[0].Listen {
t.Errorf("loaded.Server.Listen = %q, want %q", loaded.Server.Listen, cfg.Server.Listen) t.Errorf("loaded.Servers[0].Listen = %q, want %q", loaded.Servers[0].Listen, cfg.Servers[0].Listen)
} }
if len(loaded.Server.Proxy) != 1 { if len(loaded.Servers[0].Proxy) != 1 {
t.Errorf("len(loaded.Server.Proxy) = %d, want 1", len(loaded.Server.Proxy)) t.Errorf("len(loaded.Servers[0].Proxy) = %d, want 1", len(loaded.Servers[0].Proxy))
} }
if loaded.Server.Proxy[0].LoadBalance != "round_robin" { if loaded.Servers[0].Proxy[0].LoadBalance != "round_robin" {
t.Errorf("loaded.Server.Proxy[0].LoadBalance = %q, want %q", loaded.Server.Proxy[0].LoadBalance, "round_robin") t.Errorf("loaded.Servers[0].Proxy[0].LoadBalance = %q, want %q", loaded.Servers[0].Proxy[0].LoadBalance, "round_robin")
} }
}) })
} }

View File

@ -19,44 +19,44 @@ func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig() cfg := DefaultConfig()
// 验证 Listen 默认值 // 验证 Listen 默认值
if cfg.Server.Listen != ":8080" { if cfg.Servers[0].Listen != ":8080" {
t.Errorf("Server.Listen 期望 :8080, 实际 %s", cfg.Server.Listen) t.Errorf("Server.Listen 期望 :8080, 实际 %s", cfg.Servers[0].Listen)
} }
// 验证 SSL 默认版本 // 验证 SSL 默认版本
if len(cfg.Server.SSL.Protocols) != 2 { if len(cfg.Servers[0].SSL.Protocols) != 2 {
t.Errorf("SSL.Protocols 期望 2 个版本,实际 %d", len(cfg.Server.SSL.Protocols)) t.Errorf("SSL.Protocols 期望 2 个版本,实际 %d", len(cfg.Servers[0].SSL.Protocols))
} }
expectedProtocols := []string{"TLSv1.2", "TLSv1.3"} expectedProtocols := []string{"TLSv1.2", "TLSv1.3"}
for i, proto := range cfg.Server.SSL.Protocols { for i, proto := range cfg.Servers[0].SSL.Protocols {
if proto != expectedProtocols[i] { if proto != expectedProtocols[i] {
t.Errorf("SSL.Protocols[%d] 期望 %s, 实际 %s", i, expectedProtocols[i], proto) t.Errorf("SSL.Protocols[%d] 期望 %s, 实际 %s", i, expectedProtocols[i], proto)
} }
} }
// 验证 HSTS 默认值 // 验证 HSTS 默认值
if cfg.Server.SSL.HSTS.MaxAge != 31536000 { if cfg.Servers[0].SSL.HSTS.MaxAge != 31536000 {
t.Errorf("HSTS.MaxAge 期望 31536000, 实际 %d", cfg.Server.SSL.HSTS.MaxAge) t.Errorf("HSTS.MaxAge 期望 31536000, 实际 %d", cfg.Servers[0].SSL.HSTS.MaxAge)
} }
if !cfg.Server.SSL.HSTS.IncludeSubDomains { if !cfg.Servers[0].SSL.HSTS.IncludeSubDomains {
t.Errorf("HSTS.IncludeSubDomains 期望 true, 实际 %v", cfg.Server.SSL.HSTS.IncludeSubDomains) t.Errorf("HSTS.IncludeSubDomains 期望 true, 实际 %v", cfg.Servers[0].SSL.HSTS.IncludeSubDomains)
} }
if cfg.Server.SSL.HSTS.Preload { if cfg.Servers[0].SSL.HSTS.Preload {
t.Errorf("HSTS.Preload 期望 false, 实际 %v", cfg.Server.SSL.HSTS.Preload) t.Errorf("HSTS.Preload 期望 false, 实际 %v", cfg.Servers[0].SSL.HSTS.Preload)
} }
// 验证压缩默认值 // 验证压缩默认值
if cfg.Server.Compression.Type != "gzip" { if cfg.Servers[0].Compression.Type != "gzip" {
t.Errorf("Compression.Type 期望 gzip, 实际 %s", cfg.Server.Compression.Type) t.Errorf("Compression.Type 期望 gzip, 实际 %s", cfg.Servers[0].Compression.Type)
} }
if cfg.Server.Compression.Level != 6 { if cfg.Servers[0].Compression.Level != 6 {
t.Errorf("Compression.Level 期望 6, 实际 %d", cfg.Server.Compression.Level) t.Errorf("Compression.Level 期望 6, 实际 %d", cfg.Servers[0].Compression.Level)
} }
if cfg.Server.Compression.MinSize != 1024 { if cfg.Servers[0].Compression.MinSize != 1024 {
t.Errorf("Compression.MinSize 期望 1024, 实际 %d", cfg.Server.Compression.MinSize) t.Errorf("Compression.MinSize 期望 1024, 实际 %d", cfg.Servers[0].Compression.MinSize)
} }
expectedTypes := []string{"text/html", "text/css", "text/javascript", "application/json", "application/javascript"} expectedTypes := []string{"text/html", "text/css", "text/javascript", "application/json", "application/javascript"}
for i, ct := range cfg.Server.Compression.Types { for i, ct := range cfg.Servers[0].Compression.Types {
if ct != expectedTypes[i] { if ct != expectedTypes[i] {
t.Errorf("Compression.Types[%d] 期望 %s, 实际 %s", i, expectedTypes[i], ct) t.Errorf("Compression.Types[%d] 期望 %s, 实际 %s", i, expectedTypes[i], ct)
} }
@ -165,22 +165,22 @@ func TestDefaultConfigSSLDefaults(t *testing.T) {
cfg := DefaultConfig() cfg := DefaultConfig()
// 验证 SessionTickets 默认值 // 验证 SessionTickets 默认值
if cfg.Server.SSL.SessionTickets.Enabled { if cfg.Servers[0].SSL.SessionTickets.Enabled {
t.Error("SessionTickets.Enabled 期望 false") t.Error("SessionTickets.Enabled 期望 false")
} }
if cfg.Server.SSL.SessionTickets.RetainKeys != 3 { if cfg.Servers[0].SSL.SessionTickets.RetainKeys != 3 {
t.Errorf("SessionTickets.RetainKeys 期望 3实际 %d", cfg.Server.SSL.SessionTickets.RetainKeys) t.Errorf("SessionTickets.RetainKeys 期望 3实际 %d", cfg.Servers[0].SSL.SessionTickets.RetainKeys)
} }
// 验证 ClientVerify 默认值 // 验证 ClientVerify 默认值
if cfg.Server.SSL.ClientVerify.Enabled { if cfg.Servers[0].SSL.ClientVerify.Enabled {
t.Error("ClientVerify.Enabled 期望 false") t.Error("ClientVerify.Enabled 期望 false")
} }
if cfg.Server.SSL.ClientVerify.Mode != "none" { if cfg.Servers[0].SSL.ClientVerify.Mode != "none" {
t.Errorf("ClientVerify.Mode 期望 none实际 %s", cfg.Server.SSL.ClientVerify.Mode) t.Errorf("ClientVerify.Mode 期望 none实际 %s", cfg.Servers[0].SSL.ClientVerify.Mode)
} }
if cfg.Server.SSL.ClientVerify.VerifyDepth != 1 { if cfg.Servers[0].SSL.ClientVerify.VerifyDepth != 1 {
t.Errorf("ClientVerify.VerifyDepth 期望 1实际 %d", cfg.Server.SSL.ClientVerify.VerifyDepth) t.Errorf("ClientVerify.VerifyDepth 期望 1实际 %d", cfg.Servers[0].SSL.ClientVerify.VerifyDepth)
} }
} }
@ -225,8 +225,8 @@ func TestGenerateConfigYAMLLoadable(t *testing.T) {
} }
// 验证关键字段匹配 // 验证关键字段匹配
if loadedCfg.Server.Listen != cfg.Server.Listen { if loadedCfg.Servers[0].Listen != cfg.Servers[0].Listen {
t.Errorf("Server.Listen 不匹配: 期望 %s, 实际 %s", cfg.Server.Listen, loadedCfg.Server.Listen) t.Errorf("Server.Listen 不匹配: 期望 %s, 实际 %s", cfg.Servers[0].Listen, loadedCfg.Servers[0].Listen)
} }
if loadedCfg.Resolver.Enabled != cfg.Resolver.Enabled { if loadedCfg.Resolver.Enabled != cfg.Resolver.Enabled {
t.Errorf("Resolver.Enabled 不匹配") t.Errorf("Resolver.Enabled 不匹配")

View File

@ -25,14 +25,14 @@ import (
// TestNew 测试服务器创建 // TestNew 测试服务器创建
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Static: []config.StaticConfig{{ Static: []config.StaticConfig{{
Path: "/", Path: "/",
Root: "./static", Root: "./static",
Index: []string{"index.html"}, Index: []string{"index.html"},
}}, }},
}, }},
} }
s := New(cfg) s := New(cfg)
@ -56,9 +56,9 @@ func TestNew(t *testing.T) {
// TestStopWithoutServer 测试无服务器时调用 Stop // TestStopWithoutServer 测试无服务器时调用 Stop
func TestStopWithoutServer(t *testing.T) { func TestStopWithoutServer(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -73,9 +73,9 @@ func TestStopWithoutServer(t *testing.T) {
// TestGracefulStop 测试 GracefulStop 调用 // TestGracefulStop 测试 GracefulStop 调用
func TestGracefulStop(t *testing.T) { func TestGracefulStop(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -90,9 +90,9 @@ func TestGracefulStop(t *testing.T) {
// TestStopAfterStop 测试多次调用 Stop // TestStopAfterStop 测试多次调用 Stop
func TestStopAfterStop(t *testing.T) { func TestStopAfterStop(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -109,9 +109,9 @@ func TestStopAfterStop(t *testing.T) {
// TestGracefulStopWithZeroTimeout 测试零超时的 GracefulStop // TestGracefulStopWithZeroTimeout 测试零超时的 GracefulStop
func TestGracefulStopWithZeroTimeout(t *testing.T) { func TestGracefulStopWithZeroTimeout(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -126,13 +126,13 @@ func TestGracefulStopWithZeroTimeout(t *testing.T) {
func TestBuildMiddlewareChain_AccessLog(t *testing.T) { func TestBuildMiddlewareChain_AccessLog(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -145,18 +145,18 @@ func TestBuildMiddlewareChain_AccessLog(t *testing.T) {
func TestBuildMiddlewareChain_AccessControl(t *testing.T) { func TestBuildMiddlewareChain_AccessControl(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Security: config.SecurityConfig{ Security: config.SecurityConfig{
Access: config.AccessConfig{ Access: config.AccessConfig{
Allow: []string{"127.0.0.1"}, Allow: []string{"127.0.0.1"},
}, },
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -169,7 +169,7 @@ func TestBuildMiddlewareChain_AccessControl(t *testing.T) {
func TestBuildMiddlewareChain_RateLimiter(t *testing.T) { func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Security: config.SecurityConfig{ Security: config.SecurityConfig{
RateLimit: config.RateLimitConfig{ RateLimit: config.RateLimitConfig{
@ -177,11 +177,11 @@ func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
Burst: 200, Burst: 200,
}, },
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -194,16 +194,16 @@ func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
func TestBuildMiddlewareChain_Rewrite(t *testing.T) { func TestBuildMiddlewareChain_Rewrite(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Rewrite: []config.RewriteRule{ Rewrite: []config.RewriteRule{
{Pattern: "/old/(.*)", Replacement: "/new/$1"}, {Pattern: "/old/(.*)", Replacement: "/new/$1"},
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -216,16 +216,16 @@ func TestBuildMiddlewareChain_Rewrite(t *testing.T) {
func TestBuildMiddlewareChain_Compression(t *testing.T) { func TestBuildMiddlewareChain_Compression(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Compression: config.CompressionConfig{ Compression: config.CompressionConfig{
Level: 6, Level: 6,
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -238,7 +238,7 @@ func TestBuildMiddlewareChain_Compression(t *testing.T) {
func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) { func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Security: config.SecurityConfig{ Security: config.SecurityConfig{
Headers: config.SecurityHeaders{ Headers: config.SecurityHeaders{
@ -246,11 +246,11 @@ func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
XContentTypeOptions: "nosniff", XContentTypeOptions: "nosniff",
}, },
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -263,7 +263,7 @@ func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) { func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Logging: config.LoggingConfig{}, Logging: config.LoggingConfig{},
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Security: config.SecurityConfig{ Security: config.SecurityConfig{
Access: config.AccessConfig{ Access: config.AccessConfig{
@ -283,11 +283,11 @@ func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
Compression: config.CompressionConfig{ Compression: config.CompressionConfig{
Level: 6, Level: 6,
}, },
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("buildMiddlewareChain failed: %v", err) t.Errorf("buildMiddlewareChain failed: %v", err)
} }
@ -299,9 +299,9 @@ func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
// TestTrackStats 测试请求统计追踪 // TestTrackStats 测试请求统计追踪
func TestTrackStats(t *testing.T) { func TestTrackStats(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -350,9 +350,9 @@ func TestTrackStats(t *testing.T) {
// TestTrackStats_MultipleRequests 测试多次请求统计 // TestTrackStats_MultipleRequests 测试多次请求统计
func TestTrackStats_MultipleRequests(t *testing.T) { func TestTrackStats_MultipleRequests(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -378,9 +378,9 @@ func TestTrackStats_MultipleRequests(t *testing.T) {
// TestGetListeners_Empty 测试空监听器列表 // TestGetListeners_Empty 测试空监听器列表
func TestGetListeners_Empty(t *testing.T) { func TestGetListeners_Empty(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -394,9 +394,9 @@ func TestGetListeners_Empty(t *testing.T) {
// TestSetListeners 测试设置监听器 // TestSetListeners 测试设置监听器
func TestSetListeners(t *testing.T) { func TestSetListeners(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -431,9 +431,9 @@ func TestSetListeners(t *testing.T) {
// TestGetTLSConfig_NotConfigured 测试未配置 TLS // TestGetTLSConfig_NotConfigured 测试未配置 TLS
func TestGetTLSConfig_NotConfigured(t *testing.T) { func TestGetTLSConfig_NotConfigured(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -453,9 +453,9 @@ func TestGetTLSConfig_NotConfigured(t *testing.T) {
// TestGetHandler 测试获取 handler // TestGetHandler 测试获取 handler
func TestGetHandler(t *testing.T) { func TestGetHandler(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -482,9 +482,9 @@ func TestGetHandler(t *testing.T) {
// TestServer_Connections 测试连接统计 // TestServer_Connections 测试连接统计
func TestServer_Connections(t *testing.T) { func TestServer_Connections(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -510,9 +510,9 @@ func TestServer_Connections(t *testing.T) {
// TestServer_Proxies 测试代理管理 // TestServer_Proxies 测试代理管理
func TestServer_Proxies(t *testing.T) { func TestServer_Proxies(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -526,9 +526,9 @@ func TestServer_Proxies(t *testing.T) {
// TestServer_Running 测试运行状态 // TestServer_Running 测试运行状态
func TestServer_Running(t *testing.T) { func TestServer_Running(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -542,9 +542,9 @@ func TestServer_Running(t *testing.T) {
// TestServer_StopWithNilFastServer 测试无 fastServer 时停止 // TestServer_StopWithNilFastServer 测试无 fastServer 时停止
func TestServer_StopWithNilFastServer(t *testing.T) { func TestServer_StopWithNilFastServer(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -559,9 +559,9 @@ func TestServer_StopWithNilFastServer(t *testing.T) {
// TestServer_GracefulStopWithNilFastServer 测试无 fastServer 时优雅停止 // TestServer_GracefulStopWithNilFastServer 测试无 fastServer 时优雅停止
func TestServer_GracefulStopWithNilFastServer(t *testing.T) { func TestServer_GracefulStopWithNilFastServer(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -576,9 +576,9 @@ func TestServer_GracefulStopWithNilFastServer(t *testing.T) {
// TestServer_GetProxyCacheStats 测试代理缓存统计 // TestServer_GetProxyCacheStats 测试代理缓存统计
func TestServer_GetProxyCacheStats(t *testing.T) { func TestServer_GetProxyCacheStats(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -596,14 +596,14 @@ func TestServer_GetProxyCacheStats(t *testing.T) {
// TestServer_BuildMiddlewareChain_EmptyConfig 测试空配置的中间件链 // TestServer_BuildMiddlewareChain_EmptyConfig 测试空配置的中间件链
func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) { func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
chain, err := s.buildMiddlewareChain(&cfg.Server) chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
@ -615,9 +615,9 @@ func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) {
// TestServer_TrackStats_EmptyBody 测试空响应体的统计 // TestServer_TrackStats_EmptyBody 测试空响应体的统计
func TestServer_TrackStats_EmptyBody(t *testing.T) { func TestServer_TrackStats_EmptyBody(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -646,9 +646,9 @@ func TestServer_TrackStats_EmptyBody(t *testing.T) {
// TestStart_Success 测试服务器配置初始化 // TestStart_Success 测试服务器配置初始化
func TestStart_Success(t *testing.T) { func TestStart_Success(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -669,14 +669,14 @@ func TestStart_WithStaticFiles(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
Static: []config.StaticConfig{{ Static: []config.StaticConfig{{
Path: "/static", Path: "/static",
Root: tempDir, Root: tempDir,
Index: []string{"index.html"}, Index: []string{"index.html"},
}}, }},
}, }},
} }
s := New(cfg) s := New(cfg)
@ -689,9 +689,9 @@ func TestStart_WithStaticFiles(t *testing.T) {
// TestStart_WithGoroutinePool 测试 GoroutinePool 配置 // TestStart_WithGoroutinePool 测试 GoroutinePool 配置
func TestStart_WithGoroutinePool(t *testing.T) { func TestStart_WithGoroutinePool(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
Performance: config.PerformanceConfig{ Performance: config.PerformanceConfig{
GoroutinePool: config.GoroutinePoolConfig{ GoroutinePool: config.GoroutinePoolConfig{
Enabled: true, Enabled: true,
@ -712,9 +712,9 @@ func TestStart_WithGoroutinePool(t *testing.T) {
// TestStart_WithFileCache 测试文件缓存配置 // TestStart_WithFileCache 测试文件缓存配置
func TestStart_WithFileCache(t *testing.T) { func TestStart_WithFileCache(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":8080", Listen: ":8080",
}, }},
Performance: config.PerformanceConfig{ Performance: config.PerformanceConfig{
FileCache: config.FileCacheConfig{ FileCache: config.FileCacheConfig{
MaxEntries: 1000, MaxEntries: 1000,
@ -737,9 +737,9 @@ func TestStop_Graceful(t *testing.T) {
} }
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -754,9 +754,9 @@ func TestStop_Graceful(t *testing.T) {
// TestGetTLSConfig_Nil 测试无 TLS 配置 // TestGetTLSConfig_Nil 测试无 TLS 配置
func TestGetTLSConfig_Nil(t *testing.T) { func TestGetTLSConfig_Nil(t *testing.T) {
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
s := New(cfg) s := New(cfg)
@ -776,9 +776,9 @@ func TestGetTLSConfig_NilServer(t *testing.T) {
// 防御性:如果 s 为 nil调用方法会 panic这是预期的行为 // 防御性:如果 s 为 nil调用方法会 panic这是预期的行为
// 这里我们只测试非 nil 但 tlsManager 为 nil 的情况 // 这里我们只测试非 nil 但 tlsManager 为 nil 的情况
cfg := &config.Config{ cfg := &config.Config{
Server: config.ServerConfig{ Servers: []config.ServerConfig{{
Listen: ":0", Listen: ":0",
}, }},
} }
s = New(cfg) s = New(cfg)