Files
warmbly/internal/api/middleware/request_id_test.go
Matthew Meszaros 3837cd3898 feat: bound auth login network waits
Add request and mail-delivery timeouts around auth flows so login requests cannot hang indefinitely when notification delivery stalls.

Allow the local admin dev origin through default CORS and update context-aware lint fixes so the repository lint gate passes.
2026-06-01 16:56:24 +02:00

57 lines
1.5 KiB
Go

package middleware
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestRequestIDMiddlewareUsesClientRequestID(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(RequestIDMiddleware())
r.GET("/x", func(c *gin.Context) {
c.String(http.StatusOK, c.GetString(RequestIDContextKey))
})
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/x", nil)
req.Header.Set(RequestIDHeader, "client-trace_123")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
}
if got := rec.Header().Get(RequestIDHeader); got != "client-trace_123" {
t.Fatalf("response request id = %q", got)
}
if got := rec.Body.String(); got != "client-trace_123" {
t.Fatalf("context request id = %q", got)
}
}
func TestRequestIDMiddlewareReplacesUnsafeRequestID(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(RequestIDMiddleware())
r.GET("/x", func(c *gin.Context) {
c.String(http.StatusOK, c.GetString(RequestIDContextKey))
})
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/x", nil)
req.Header.Set(RequestIDHeader, "bad/request/id")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
got := rec.Header().Get(RequestIDHeader)
if got == "" || got == "bad/request/id" {
t.Fatalf("response request id = %q", got)
}
if got != rec.Body.String() {
t.Fatalf("header request id %q does not match context %q", got, rec.Body.String())
}
}