Files
hfb_sys/backend/internal/router/router_test.go
T

69 lines
1.9 KiB
Go

package router
import (
"net/http"
"net/http/httptest"
"testing"
"hfb_sys/backend/internal/config"
"go.uber.org/zap"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func testConfig() config.Config {
return config.Config{AppEnv: "test", JWTSecret: "test-secret"}
}
func TestHealthRoutesSeparateLivenessAndReadiness(t *testing.T) {
engine := New(testConfig(), Dependencies{}, zap.NewNop())
for _, tt := range []struct {
path string
wantStatus int
}{
{path: "/health", wantStatus: http.StatusOK},
{path: "/health/ready", wantStatus: http.StatusServiceUnavailable},
{path: "/api/health", wantStatus: http.StatusServiceUnavailable},
} {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, tt.path, nil)
engine.ServeHTTP(recorder, request)
if recorder.Code != tt.wantStatus {
t.Fatalf("GET %s status = %d, want %d; body=%s", tt.path, recorder.Code, tt.wantStatus, recorder.Body.String())
}
}
}
func TestExtractedAdminRoutesRemainRegisteredWithoutDuplicates(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:router-routes?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
engine := New(testConfig(), Dependencies{DB: db}, zap.NewNop())
routes := engine.Routes()
registered := make(map[string]struct{}, len(routes))
for _, route := range routes {
key := route.Method + " " + route.Path
if _, exists := registered[key]; exists {
t.Fatalf("duplicate route: %s", key)
}
registered[key] = struct{}{}
}
for _, key := range []string{
"GET /api/admin/crash/products",
"GET /api/admin/mohong/products",
"POST /api/admin/chats/qrcodes/batch",
"PUT /api/admin/chat-support-groups/:id/members",
"PUT /api/admin/roles/:id/permissions",
"PUT /api/admin/admin-users/:id/password",
"POST /api/admin/announcements/:id/publish",
} {
if _, exists := registered[key]; !exists {
t.Errorf("missing route after extraction: %s", key)
}
}
}