package unit import ( "net/http/httptest" "testing" "time" "github.com/smartystreets/goconvey/convey" "github.com/subconverter-go/internal/config" httpserver "github.com/subconverter-go/internal/http" ) // TestHTTPServerCreation 测试HTTP服务器创建 func TestHTTPServerCreation(t *testing.T) { convey.Convey("HTTP服务器创建测试", t, func() { convey.Convey("创建默认HTTP服务器", func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) convey.So(server, convey.ShouldNotBeNil) // 验证服务器配置 stats := server.GetStats() convey.So(stats, convey.ShouldNotBeNil) convey.So(stats["host"], convey.ShouldEqual, "0.0.0.0") convey.So(stats["port"], convey.ShouldEqual, 25500) // 验证运行时间 uptime := server.GetUptime() convey.So(uptime, convey.ShouldBeGreaterThan, 0) }) convey.Convey("创建带自定义配置的HTTP服务器", func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() // 修改服务器配置 newConfig := manager.GetConfig().Clone() newConfig.Server.Host = "127.0.0.1" newConfig.Server.Port = 8080 err = manager.UpdateConfig(newConfig) convey.So(err, convey.ShouldBeNil) server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) convey.So(server, convey.ShouldNotBeNil) stats := server.GetStats() convey.So(stats["host"], convey.ShouldEqual, "127.0.0.1") convey.So(stats["port"], convey.ShouldEqual, 8080) }) }) } // TestHTTPServerRoutes 测试HTTP服务器路由 func TestHTTPServerRoutes(t *testing.T) { convey.Convey("HTTP服务器路由测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) defer server.Stop() app := server.GetApp() convey.Convey("健康检查路由", func() { req := httptest.NewRequest("GET", "/health", nil) resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) // 验证响应内容 // 这里简化测试,实际项目中应该解析JSON convey.So(resp.Header.Get("Content-Type"), convey.ShouldContain, "application/json") }) convey.Convey("版本信息路由", func() { req := httptest.NewRequest("GET", "/version", nil) resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) convey.So(resp.Header.Get("Content-Type"), convey.ShouldContain, "application/json") }) convey.Convey("根路由", func() { req := httptest.NewRequest("GET", "/", nil) resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) convey.So(resp.Header.Get("Content-Type"), convey.ShouldContain, "application/json") }) convey.Convey("统计信息路由", func() { req := httptest.NewRequest("GET", "/api/stats", nil) resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) convey.So(resp.Header.Get("Content-Type"), convey.ShouldContain, "application/json") }) convey.Convey("未实现的路由", func() { req := httptest.NewRequest("GET", "/api/sub", nil) resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 501) // Not Implemented }) }) } // TestHTTPServerMiddleware 测试HTTP服务器中间件 func TestHTTPServerMiddleware(t *testing.T) { convey.Convey("HTTP服务器中间件测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) defer server.Stop() app := server.GetApp() convey.Convey("CORS中间件", func() { req := httptest.NewRequest("GET", "/health", nil) req.Header.Set("Origin", "http://localhost:3000") resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) // 验证CORS头 allowOrigin := resp.Header.Get("Access-Control-Allow-Origin") convey.So(allowOrigin, convey.ShouldNotBeEmpty) }) convey.Convey("OPTIONS预检请求", func() { req := httptest.NewRequest("OPTIONS", "/health", nil) req.Header.Set("Origin", "http://localhost:3000") req.Header.Set("Access-Control-Request-Method", "GET") resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) }) }) } // TestHTTPServerWithRateLimit 测试带速率限制的HTTP服务器 func TestHTTPServerWithRateLimit(t *testing.T) { convey.Convey("带速率限制的HTTP服务器测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() // 启用速率限制 newConfig := manager.GetConfig().Clone() newConfig.Security.RateLimit = 2 // 每分钟2个请求 err = manager.UpdateConfig(newConfig) convey.So(err, convey.ShouldBeNil) server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) defer server.Stop() app := server.GetApp() convey.Convey("速率限制正常工作", func() { // 发送第一个请求 req1 := httptest.NewRequest("GET", "/health", nil) resp1, err := app.Test(req1, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp1.StatusCode, convey.ShouldEqual, 200) // 发送第二个请求 req2 := httptest.NewRequest("GET", "/health", nil) resp2, err := app.Test(req2, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp2.StatusCode, convey.ShouldEqual, 200) // 发送第三个请求(应该被限制) req3 := httptest.NewRequest("GET", "/health", nil) resp3, err := app.Test(req3, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp3.StatusCode, convey.ShouldEqual, 429) // Too Many Requests }) }) } // TestHTTPServerStats 测试HTTP服务器统计信息 func TestHTTPServerStats(t *testing.T) { convey.Convey("HTTP服务器统计信息测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) defer server.Stop() convey.Convey("获取统计信息", func() { stats := server.GetStats() convey.So(stats, convey.ShouldNotBeNil) // 验证统计信息字段 convey.So(stats["uptime"], convey.ShouldNotBeEmpty) convey.So(stats["start_time"], convey.ShouldNotBeEmpty) convey.So(stats["host"], convey.ShouldEqual, "0.0.0.0") convey.So(stats["port"], convey.ShouldEqual, 25500) convey.So(stats["max_request_size"], convey.ShouldEqual, int64(10485760)) // 10MB convey.So(stats["read_timeout"], convey.ShouldEqual, 30) convey.So(stats["write_timeout"], convey.ShouldEqual, 30) }) convey.Convey("运行时间递增", func() { uptime1 := server.GetUptime() time.Sleep(10 * time.Millisecond) uptime2 := server.GetUptime() convey.So(uptime2, convey.ShouldBeGreaterThan, uptime1) }) }) } // TestHTTPServerGracefulShutdown 测试HTTP服务器优雅关闭 func TestHTTPServerGracefulShutdown(t *testing.T) { convey.Convey("HTTP服务器优雅关闭测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) // 模拟服务器运行 go func() { time.Sleep(100 * time.Millisecond) err := server.Stop() convey.So(err, convey.ShouldBeNil) }() // 验证服务器能够正常关闭 time.Sleep(200 * time.Millisecond) }) } // TestHTTPServerWithCustomSecurityConfig 测试自定义安全配置 func TestHTTPServerWithCustomSecurityConfig(t *testing.T) { convey.Convey("自定义安全配置测试", t, func() { manager, err := config.NewConfigManager("") convey.So(err, convey.ShouldBeNil) defer manager.Close() // 设置自定义CORS配置 newConfig := manager.GetConfig().Clone() newConfig.Security.CorsOrigins = []string{"http://localhost:3000", "https://example.com"} newConfig.Security.RateLimit = 5 err = manager.UpdateConfig(newConfig) convey.So(err, convey.ShouldBeNil) server, err := httpserver.NewServer(manager) convey.So(err, convey.ShouldBeNil) defer server.Stop() app := server.GetApp() convey.Convey("自定义CORS生效", func() { req := httptest.NewRequest("GET", "/health", nil) req.Header.Set("Origin", "http://localhost:3000") resp, err := app.Test(req, -1) convey.So(err, convey.ShouldBeNil) convey.So(resp.StatusCode, convey.ShouldEqual, 200) }) }) }