Files
GovAI/server/internal/middleware/metrics_test.go
T
freedakgmail c949204662 feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。

T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
2026-06-17 17:52:47 +08:00

51 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestMetricsMiddleware_RecordsRequestWithRouteTemplate(t *testing.T) {
r := chi.NewRouter()
r.Use(Metrics)
r.Get("/things/{id}", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
// 用路由模板(而非具体路径)作为标签,避免高基数
before := testutil.ToFloat64(httpRequestsTotal.WithLabelValues("GET", "/things/{id}", "200"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, httptest.NewRequest("GET", "/things/42", nil))
if rec.Code != http.StatusOK {
t.Fatalf("状态码应为 200,实际 %d", rec.Code)
}
after := testutil.ToFloat64(httpRequestsTotal.WithLabelValues("GET", "/things/{id}", "200"))
if after != before+1 {
t.Fatalf("请求计数应 +1before=%v after=%v", before, after)
}
}
func TestMetricsMiddleware_RecordsErrorStatus(t *testing.T) {
r := chi.NewRouter()
r.Use(Metrics)
r.Get("/boom", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
before := testutil.ToFloat64(httpRequestsTotal.WithLabelValues("GET", "/boom", "500"))
rec := httptest.NewRecorder()
r.ServeHTTP(rec, httptest.NewRequest("GET", "/boom", nil))
after := testutil.ToFloat64(httpRequestsTotal.WithLabelValues("GET", "/boom", "500"))
if after != before+1 {
t.Fatalf("500 计数应 +1before=%v after=%v", before, after)
}
}