fix/status-review-sync-20260409 #1

Merged
long merged 65 commits from fix/status-review-sync-20260409 into main 2026-04-18 15:05:51 +00:00
Showing only changes of commit 5929d774f0 - Show all commits

View File

@@ -1,6 +1,7 @@
package middleware
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
@@ -137,3 +138,81 @@ func TestNoStoreSensitiveResponses_DoesNotAttachHeadersToNonAuthRoutes(t *testin
t.Fatalf("did not expect cache-control header, got %q", got)
}
}
// ---------- TraceID middleware ----------
func TestTraceID_GeneratesAndAttachesTraceID(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/users", nil)
TraceID()(c)
traceID := c.GetString("trace_id")
if traceID == "" {
t.Fatal("expected trace_id to be set")
}
if len(traceID) < 8 {
t.Fatalf("trace_id should be reasonably long, got %q", traceID)
}
if got := recorder.Header().Get("X-Trace-ID"); got != traceID {
t.Fatalf("expected X-Trace-ID header to match trace_id, got %q", got)
}
}
func TestTraceID_ExtractsExistingTraceID(t *testing.T) {
gin.SetMode(gin.TestMode)
existingTraceID := "existing-trace-id-12345"
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/users", nil)
c.Request.Header.Set("X-Trace-ID", existingTraceID)
TraceID()(c)
traceID := c.GetString("trace_id")
if traceID != existingTraceID {
t.Fatalf("expected trace_id to be extracted from header, got %q", traceID)
}
}
// ---------- Error handling middleware ----------
func TestErrorHandler_HandlesErrors(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/users", nil)
c.Error(errors.New("test error"))
ErrorHandler()(c)
if recorder.Code != http.StatusInternalServerError {
t.Fatalf("expected status 500, got %d", recorder.Code)
}
}
func TestRecover_HandlesPanic(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, router := gin.CreateTestContext(recorder)
c.Request = httptest.NewRequest(http.MethodGet, "/panic", nil)
router.Use(Recover())
router.GET("/panic", func(c *gin.Context) {
panic("test panic")
})
router.ServeHTTP(recorder, c.Request)
if recorder.Code != http.StatusInternalServerError {
t.Fatalf("expected status 500 after panic, got %d", recorder.Code)
}
}