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:
parent
168d34d58d
commit
d99011aab6
@ -196,7 +196,7 @@ func TestRun(t *testing.T) {
|
||||
genConfig: true,
|
||||
outputPath: "",
|
||||
wantExitCode: 0,
|
||||
wantContains: "server:",
|
||||
wantContains: "servers:",
|
||||
},
|
||||
{
|
||||
name: "生成配置输出到文件",
|
||||
@ -245,7 +245,7 @@ func TestRun(t *testing.T) {
|
||||
data, err := os.ReadFile(tt.outputPath)
|
||||
if err != nil {
|
||||
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])
|
||||
}
|
||||
}
|
||||
@ -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 {
|
||||
if !strings.Contains(stdout, field) {
|
||||
t.Errorf("输出应包含 %q", field)
|
||||
@ -302,7 +302,7 @@ func TestGenerateConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
content := string(data)
|
||||
expectedFields := []string{"server:", "listen:", "logging:", "performance:", "monitoring:"}
|
||||
expectedFields := []string{"servers:", "listen:", "logging:", "performance:", "monitoring:"}
|
||||
for _, field := range expectedFields {
|
||||
if !strings.Contains(content, field) {
|
||||
t.Errorf("配置文件应包含 %q", field)
|
||||
@ -361,9 +361,9 @@ func TestHandleSignal_SIGQUIT(t *testing.T) {
|
||||
// 创建一个简单的 App
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0", // 使用随机端口
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
@ -382,9 +382,9 @@ func TestHandleSignal_SIGQUIT(t *testing.T) {
|
||||
func TestHandleSignal_SIGTERM(t *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
app.srv = server.New(app.cfg)
|
||||
@ -400,9 +400,9 @@ func TestHandleSignal_SIGTERM(t *testing.T) {
|
||||
func TestHandleSignal_SIGINT(t *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
app.srv = server.New(app.cfg)
|
||||
@ -432,9 +432,9 @@ logging:
|
||||
|
||||
app := NewApp(cfgPath)
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
@ -449,9 +449,9 @@ logging:
|
||||
func TestHandleSignal_SIGUSR1(t *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
Logging: config.LoggingConfig{
|
||||
Error: config.ErrorLogConfig{
|
||||
Level: "info",
|
||||
@ -471,9 +471,9 @@ func TestHandleSignal_SIGUSR1(t *testing.T) {
|
||||
func TestHandleSignal_Unknown(t *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
@ -537,17 +537,17 @@ logging:
|
||||
|
||||
app := NewApp(cfgPath)
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
app.reloadConfig()
|
||||
|
||||
// 验证配置已更新
|
||||
if app.cfg.Server.Listen != ":9090" {
|
||||
t.Errorf("Expected listen ':9090', got '%s'", app.cfg.Server.Listen)
|
||||
if app.cfg.Servers[0].Listen != ":9090" {
|
||||
t.Errorf("Expected listen ':9090', got '%s'", app.cfg.Servers[0].Listen)
|
||||
}
|
||||
}
|
||||
|
||||
@ -555,9 +555,9 @@ logging:
|
||||
func TestSetupSignalHandlers(_ *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
@ -571,9 +571,9 @@ func TestSetupSignalHandlers(_ *testing.T) {
|
||||
func TestHandleSignal_SIGUSR2(t *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
app.srv = server.New(app.cfg)
|
||||
@ -591,9 +591,9 @@ func TestHandleSignal_SIGUSR2(t *testing.T) {
|
||||
func TestGracefulUpgrade_NoListener(_ *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
app.srv = server.New(app.cfg)
|
||||
@ -658,9 +658,9 @@ func TestAppFields(t *testing.T) {
|
||||
func TestShutdownHTTP3_WithServer(_ *testing.T) {
|
||||
app := NewApp("")
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
HTTP3: config.HTTP3Config{
|
||||
Enabled: false, // 禁用,避免实际启动
|
||||
},
|
||||
@ -716,23 +716,23 @@ logging:
|
||||
|
||||
app := NewApp(cfgPath1)
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":7070",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
|
||||
// 第一次重载
|
||||
app.reloadConfig()
|
||||
if app.cfg.Server.Listen != ":8080" {
|
||||
t.Errorf("After first reload: listen = %q, want :8080", app.cfg.Server.Listen)
|
||||
if app.cfg.Servers[0].Listen != ":8080" {
|
||||
t.Errorf("After first reload: listen = %q, want :8080", app.cfg.Servers[0].Listen)
|
||||
}
|
||||
|
||||
// 更改配置路径并重载
|
||||
app.cfgPath = cfgPath2
|
||||
app.reloadConfig()
|
||||
if app.cfg.Server.Listen != ":9090" {
|
||||
t.Errorf("After second reload: listen = %q, want :9090", app.cfg.Server.Listen)
|
||||
if app.cfg.Servers[0].Listen != ":9090" {
|
||||
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) {
|
||||
app := NewApp(cfgPath)
|
||||
app.cfg = &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
|
||||
app.srv = server.New(app.cfg)
|
||||
|
||||
@ -47,14 +47,14 @@ monitoring:
|
||||
t.Fatalf("Load() 失败: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Server.Listen != ":8080" {
|
||||
t.Errorf("Server.Listen = %q, want %q", cfg.Server.Listen, ":8080")
|
||||
if cfg.Servers[0].Listen != ":8080" {
|
||||
t.Errorf("Servers[0].Listen = %q, want %q", cfg.Servers[0].Listen, ":8080")
|
||||
}
|
||||
if cfg.Server.Static[0].Root != "/var/www" {
|
||||
t.Errorf("Server.Static.Root = %q, want %q", cfg.Server.Static[0].Root, "/var/www")
|
||||
if cfg.Servers[0].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" {
|
||||
t.Errorf("Server.Static.Index = %v, want [index.html]", cfg.Server.Static[0].Index)
|
||||
if len(cfg.Servers[0].Static[0].Index) != 1 || cfg.Servers[0].Static[0].Index[0] != "index.html" {
|
||||
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)
|
||||
}
|
||||
|
||||
if cfg.Server.Listen != ":9090" {
|
||||
t.Errorf("Server.Listen = %q, want %q", cfg.Server.Listen, ":9090")
|
||||
if cfg.Servers[0].Listen != ":9090" {
|
||||
t.Errorf("Servers[0].Listen = %q, want %q", cfg.Servers[0].Listen, ":9090")
|
||||
}
|
||||
if cfg.Server.Static[0].Root != "/app/public" {
|
||||
t.Errorf("Server.Static.Root = %q, want %q", cfg.Server.Static[0].Root, "/app/public")
|
||||
if cfg.Servers[0].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) {
|
||||
t.Run("正常保存", func(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{
|
||||
Servers: []ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Static: []StaticConfig{{
|
||||
Path: "/",
|
||||
Root: "/var/www",
|
||||
Index: []string{"index.html"},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
@ -215,19 +215,19 @@ func TestSave(t *testing.T) {
|
||||
t.Fatalf("重新加载配置失败: %v", err)
|
||||
}
|
||||
|
||||
if loaded.Server.Listen != cfg.Server.Listen {
|
||||
t.Errorf("loaded.Server.Listen = %q, want %q", loaded.Server.Listen, cfg.Server.Listen)
|
||||
if loaded.Servers[0].Listen != cfg.Servers[0].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 {
|
||||
t.Errorf("loaded.Server.Static[0].Root = %q, want %q", 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.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) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{
|
||||
Servers: []ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
err := Save(cfg, "/nonexistent/directory/config.yaml")
|
||||
@ -263,7 +263,7 @@ func TestSave(t *testing.T) {
|
||||
|
||||
t.Run("保存并加载完整配置", func(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{
|
||||
Servers: []ServerConfig{{
|
||||
Listen: ":8443",
|
||||
Name: "default",
|
||||
Static: []StaticConfig{{
|
||||
@ -292,7 +292,7 @@ func TestSave(t *testing.T) {
|
||||
Burst: 200,
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
Logging: LoggingConfig{
|
||||
Access: AccessLogConfig{
|
||||
Path: "/var/log/access.log",
|
||||
@ -325,14 +325,14 @@ func TestSave(t *testing.T) {
|
||||
}
|
||||
|
||||
// 验证关键字段
|
||||
if loaded.Server.Listen != cfg.Server.Listen {
|
||||
t.Errorf("loaded.Server.Listen = %q, want %q", loaded.Server.Listen, cfg.Server.Listen)
|
||||
if loaded.Servers[0].Listen != cfg.Servers[0].Listen {
|
||||
t.Errorf("loaded.Servers[0].Listen = %q, want %q", loaded.Servers[0].Listen, cfg.Servers[0].Listen)
|
||||
}
|
||||
if len(loaded.Server.Proxy) != 1 {
|
||||
t.Errorf("len(loaded.Server.Proxy) = %d, want 1", len(loaded.Server.Proxy))
|
||||
if len(loaded.Servers[0].Proxy) != 1 {
|
||||
t.Errorf("len(loaded.Servers[0].Proxy) = %d, want 1", len(loaded.Servers[0].Proxy))
|
||||
}
|
||||
if loaded.Server.Proxy[0].LoadBalance != "round_robin" {
|
||||
t.Errorf("loaded.Server.Proxy[0].LoadBalance = %q, want %q", loaded.Server.Proxy[0].LoadBalance, "round_robin")
|
||||
if loaded.Servers[0].Proxy[0].LoadBalance != "round_robin" {
|
||||
t.Errorf("loaded.Servers[0].Proxy[0].LoadBalance = %q, want %q", loaded.Servers[0].Proxy[0].LoadBalance, "round_robin")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -19,44 +19,44 @@ func TestDefaultConfig(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
// 验证 Listen 默认值
|
||||
if cfg.Server.Listen != ":8080" {
|
||||
t.Errorf("Server.Listen 期望 :8080, 实际 %s", cfg.Server.Listen)
|
||||
if cfg.Servers[0].Listen != ":8080" {
|
||||
t.Errorf("Server.Listen 期望 :8080, 实际 %s", cfg.Servers[0].Listen)
|
||||
}
|
||||
|
||||
// 验证 SSL 默认版本
|
||||
if len(cfg.Server.SSL.Protocols) != 2 {
|
||||
t.Errorf("SSL.Protocols 期望 2 个版本,实际 %d", len(cfg.Server.SSL.Protocols))
|
||||
if len(cfg.Servers[0].SSL.Protocols) != 2 {
|
||||
t.Errorf("SSL.Protocols 期望 2 个版本,实际 %d", len(cfg.Servers[0].SSL.Protocols))
|
||||
}
|
||||
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] {
|
||||
t.Errorf("SSL.Protocols[%d] 期望 %s, 实际 %s", i, expectedProtocols[i], proto)
|
||||
}
|
||||
}
|
||||
|
||||
// 验证 HSTS 默认值
|
||||
if cfg.Server.SSL.HSTS.MaxAge != 31536000 {
|
||||
t.Errorf("HSTS.MaxAge 期望 31536000, 实际 %d", cfg.Server.SSL.HSTS.MaxAge)
|
||||
if cfg.Servers[0].SSL.HSTS.MaxAge != 31536000 {
|
||||
t.Errorf("HSTS.MaxAge 期望 31536000, 实际 %d", cfg.Servers[0].SSL.HSTS.MaxAge)
|
||||
}
|
||||
if !cfg.Server.SSL.HSTS.IncludeSubDomains {
|
||||
t.Errorf("HSTS.IncludeSubDomains 期望 true, 实际 %v", cfg.Server.SSL.HSTS.IncludeSubDomains)
|
||||
if !cfg.Servers[0].SSL.HSTS.IncludeSubDomains {
|
||||
t.Errorf("HSTS.IncludeSubDomains 期望 true, 实际 %v", cfg.Servers[0].SSL.HSTS.IncludeSubDomains)
|
||||
}
|
||||
if cfg.Server.SSL.HSTS.Preload {
|
||||
t.Errorf("HSTS.Preload 期望 false, 实际 %v", cfg.Server.SSL.HSTS.Preload)
|
||||
if cfg.Servers[0].SSL.HSTS.Preload {
|
||||
t.Errorf("HSTS.Preload 期望 false, 实际 %v", cfg.Servers[0].SSL.HSTS.Preload)
|
||||
}
|
||||
|
||||
// 验证压缩默认值
|
||||
if cfg.Server.Compression.Type != "gzip" {
|
||||
t.Errorf("Compression.Type 期望 gzip, 实际 %s", cfg.Server.Compression.Type)
|
||||
if cfg.Servers[0].Compression.Type != "gzip" {
|
||||
t.Errorf("Compression.Type 期望 gzip, 实际 %s", cfg.Servers[0].Compression.Type)
|
||||
}
|
||||
if cfg.Server.Compression.Level != 6 {
|
||||
t.Errorf("Compression.Level 期望 6, 实际 %d", cfg.Server.Compression.Level)
|
||||
if cfg.Servers[0].Compression.Level != 6 {
|
||||
t.Errorf("Compression.Level 期望 6, 实际 %d", cfg.Servers[0].Compression.Level)
|
||||
}
|
||||
if cfg.Server.Compression.MinSize != 1024 {
|
||||
t.Errorf("Compression.MinSize 期望 1024, 实际 %d", cfg.Server.Compression.MinSize)
|
||||
if cfg.Servers[0].Compression.MinSize != 1024 {
|
||||
t.Errorf("Compression.MinSize 期望 1024, 实际 %d", cfg.Servers[0].Compression.MinSize)
|
||||
}
|
||||
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] {
|
||||
t.Errorf("Compression.Types[%d] 期望 %s, 实际 %s", i, expectedTypes[i], ct)
|
||||
}
|
||||
@ -165,22 +165,22 @@ func TestDefaultConfigSSLDefaults(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
// 验证 SessionTickets 默认值
|
||||
if cfg.Server.SSL.SessionTickets.Enabled {
|
||||
if cfg.Servers[0].SSL.SessionTickets.Enabled {
|
||||
t.Error("SessionTickets.Enabled 期望 false")
|
||||
}
|
||||
if cfg.Server.SSL.SessionTickets.RetainKeys != 3 {
|
||||
t.Errorf("SessionTickets.RetainKeys 期望 3,实际 %d", cfg.Server.SSL.SessionTickets.RetainKeys)
|
||||
if cfg.Servers[0].SSL.SessionTickets.RetainKeys != 3 {
|
||||
t.Errorf("SessionTickets.RetainKeys 期望 3,实际 %d", cfg.Servers[0].SSL.SessionTickets.RetainKeys)
|
||||
}
|
||||
|
||||
// 验证 ClientVerify 默认值
|
||||
if cfg.Server.SSL.ClientVerify.Enabled {
|
||||
if cfg.Servers[0].SSL.ClientVerify.Enabled {
|
||||
t.Error("ClientVerify.Enabled 期望 false")
|
||||
}
|
||||
if cfg.Server.SSL.ClientVerify.Mode != "none" {
|
||||
t.Errorf("ClientVerify.Mode 期望 none,实际 %s", cfg.Server.SSL.ClientVerify.Mode)
|
||||
if cfg.Servers[0].SSL.ClientVerify.Mode != "none" {
|
||||
t.Errorf("ClientVerify.Mode 期望 none,实际 %s", cfg.Servers[0].SSL.ClientVerify.Mode)
|
||||
}
|
||||
if cfg.Server.SSL.ClientVerify.VerifyDepth != 1 {
|
||||
t.Errorf("ClientVerify.VerifyDepth 期望 1,实际 %d", cfg.Server.SSL.ClientVerify.VerifyDepth)
|
||||
if cfg.Servers[0].SSL.ClientVerify.VerifyDepth != 1 {
|
||||
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 {
|
||||
t.Errorf("Server.Listen 不匹配: 期望 %s, 实际 %s", cfg.Server.Listen, loadedCfg.Server.Listen)
|
||||
if loadedCfg.Servers[0].Listen != cfg.Servers[0].Listen {
|
||||
t.Errorf("Server.Listen 不匹配: 期望 %s, 实际 %s", cfg.Servers[0].Listen, loadedCfg.Servers[0].Listen)
|
||||
}
|
||||
if loadedCfg.Resolver.Enabled != cfg.Resolver.Enabled {
|
||||
t.Errorf("Resolver.Enabled 不匹配")
|
||||
|
||||
@ -25,14 +25,14 @@ import (
|
||||
// TestNew 测试服务器创建
|
||||
func TestNew(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Static: []config.StaticConfig{{
|
||||
Path: "/",
|
||||
Root: "./static",
|
||||
Index: []string{"index.html"},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -56,9 +56,9 @@ func TestNew(t *testing.T) {
|
||||
// TestStopWithoutServer 测试无服务器时调用 Stop
|
||||
func TestStopWithoutServer(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -73,9 +73,9 @@ func TestStopWithoutServer(t *testing.T) {
|
||||
// TestGracefulStop 测试 GracefulStop 调用
|
||||
func TestGracefulStop(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -90,9 +90,9 @@ func TestGracefulStop(t *testing.T) {
|
||||
// TestStopAfterStop 测试多次调用 Stop
|
||||
func TestStopAfterStop(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -109,9 +109,9 @@ func TestStopAfterStop(t *testing.T) {
|
||||
// TestGracefulStopWithZeroTimeout 测试零超时的 GracefulStop
|
||||
func TestGracefulStopWithZeroTimeout(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -126,13 +126,13 @@ func TestGracefulStopWithZeroTimeout(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_AccessLog(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -145,18 +145,18 @@ func TestBuildMiddlewareChain_AccessLog(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_AccessControl(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Security: config.SecurityConfig{
|
||||
Access: config.AccessConfig{
|
||||
Allow: []string{"127.0.0.1"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -169,7 +169,7 @@ func TestBuildMiddlewareChain_AccessControl(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Security: config.SecurityConfig{
|
||||
RateLimit: config.RateLimitConfig{
|
||||
@ -177,11 +177,11 @@ func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
|
||||
Burst: 200,
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -194,16 +194,16 @@ func TestBuildMiddlewareChain_RateLimiter(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_Rewrite(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Rewrite: []config.RewriteRule{
|
||||
{Pattern: "/old/(.*)", Replacement: "/new/$1"},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -216,16 +216,16 @@ func TestBuildMiddlewareChain_Rewrite(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_Compression(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Compression: config.CompressionConfig{
|
||||
Level: 6,
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -238,7 +238,7 @@ func TestBuildMiddlewareChain_Compression(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Security: config.SecurityConfig{
|
||||
Headers: config.SecurityHeaders{
|
||||
@ -246,11 +246,11 @@ func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
|
||||
XContentTypeOptions: "nosniff",
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -263,7 +263,7 @@ func TestBuildMiddlewareChain_SecurityHeaders(t *testing.T) {
|
||||
func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Logging: config.LoggingConfig{},
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Security: config.SecurityConfig{
|
||||
Access: config.AccessConfig{
|
||||
@ -283,11 +283,11 @@ func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
|
||||
Compression: config.CompressionConfig{
|
||||
Level: 6,
|
||||
},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("buildMiddlewareChain failed: %v", err)
|
||||
}
|
||||
@ -299,9 +299,9 @@ func TestBuildMiddlewareChain_AllMiddlewares(t *testing.T) {
|
||||
// TestTrackStats 测试请求统计追踪
|
||||
func TestTrackStats(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -350,9 +350,9 @@ func TestTrackStats(t *testing.T) {
|
||||
// TestTrackStats_MultipleRequests 测试多次请求统计
|
||||
func TestTrackStats_MultipleRequests(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -378,9 +378,9 @@ func TestTrackStats_MultipleRequests(t *testing.T) {
|
||||
// TestGetListeners_Empty 测试空监听器列表
|
||||
func TestGetListeners_Empty(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -394,9 +394,9 @@ func TestGetListeners_Empty(t *testing.T) {
|
||||
// TestSetListeners 测试设置监听器
|
||||
func TestSetListeners(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -431,9 +431,9 @@ func TestSetListeners(t *testing.T) {
|
||||
// TestGetTLSConfig_NotConfigured 测试未配置 TLS
|
||||
func TestGetTLSConfig_NotConfigured(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -453,9 +453,9 @@ func TestGetTLSConfig_NotConfigured(t *testing.T) {
|
||||
// TestGetHandler 测试获取 handler
|
||||
func TestGetHandler(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -482,9 +482,9 @@ func TestGetHandler(t *testing.T) {
|
||||
// TestServer_Connections 测试连接统计
|
||||
func TestServer_Connections(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -510,9 +510,9 @@ func TestServer_Connections(t *testing.T) {
|
||||
// TestServer_Proxies 测试代理管理
|
||||
func TestServer_Proxies(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -526,9 +526,9 @@ func TestServer_Proxies(t *testing.T) {
|
||||
// TestServer_Running 测试运行状态
|
||||
func TestServer_Running(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -542,9 +542,9 @@ func TestServer_Running(t *testing.T) {
|
||||
// TestServer_StopWithNilFastServer 测试无 fastServer 时停止
|
||||
func TestServer_StopWithNilFastServer(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -559,9 +559,9 @@ func TestServer_StopWithNilFastServer(t *testing.T) {
|
||||
// TestServer_GracefulStopWithNilFastServer 测试无 fastServer 时优雅停止
|
||||
func TestServer_GracefulStopWithNilFastServer(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -576,9 +576,9 @@ func TestServer_GracefulStopWithNilFastServer(t *testing.T) {
|
||||
// TestServer_GetProxyCacheStats 测试代理缓存统计
|
||||
func TestServer_GetProxyCacheStats(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -596,14 +596,14 @@ func TestServer_GetProxyCacheStats(t *testing.T) {
|
||||
// TestServer_BuildMiddlewareChain_EmptyConfig 测试空配置的中间件链
|
||||
func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Server)
|
||||
chain, err := s.buildMiddlewareChain(&cfg.Servers[0])
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
@ -615,9 +615,9 @@ func TestServer_BuildMiddlewareChain_EmptyConfig(t *testing.T) {
|
||||
// TestServer_TrackStats_EmptyBody 测试空响应体的统计
|
||||
func TestServer_TrackStats_EmptyBody(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -646,9 +646,9 @@ func TestServer_TrackStats_EmptyBody(t *testing.T) {
|
||||
// TestStart_Success 测试服务器配置初始化
|
||||
func TestStart_Success(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -669,14 +669,14 @@ func TestStart_WithStaticFiles(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
Static: []config.StaticConfig{{
|
||||
Path: "/static",
|
||||
Root: tempDir,
|
||||
Index: []string{"index.html"},
|
||||
}},
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -689,9 +689,9 @@ func TestStart_WithStaticFiles(t *testing.T) {
|
||||
// TestStart_WithGoroutinePool 测试 GoroutinePool 配置
|
||||
func TestStart_WithGoroutinePool(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
Performance: config.PerformanceConfig{
|
||||
GoroutinePool: config.GoroutinePoolConfig{
|
||||
Enabled: true,
|
||||
@ -712,9 +712,9 @@ func TestStart_WithGoroutinePool(t *testing.T) {
|
||||
// TestStart_WithFileCache 测试文件缓存配置
|
||||
func TestStart_WithFileCache(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":8080",
|
||||
},
|
||||
}},
|
||||
Performance: config.PerformanceConfig{
|
||||
FileCache: config.FileCacheConfig{
|
||||
MaxEntries: 1000,
|
||||
@ -737,9 +737,9 @@ func TestStop_Graceful(t *testing.T) {
|
||||
}
|
||||
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -754,9 +754,9 @@ func TestStop_Graceful(t *testing.T) {
|
||||
// TestGetTLSConfig_Nil 测试无 TLS 配置
|
||||
func TestGetTLSConfig_Nil(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
s := New(cfg)
|
||||
@ -776,9 +776,9 @@ func TestGetTLSConfig_NilServer(t *testing.T) {
|
||||
// 防御性:如果 s 为 nil,调用方法会 panic,这是预期的行为
|
||||
// 这里我们只测试非 nil 但 tlsManager 为 nil 的情况
|
||||
cfg := &config.Config{
|
||||
Server: config.ServerConfig{
|
||||
Servers: []config.ServerConfig{{
|
||||
Listen: ":0",
|
||||
},
|
||||
}},
|
||||
}
|
||||
s = New(cfg)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user