- internal/gemini/ → internal/adapters/openai/ (renamed package to openai) - internal/pexels/ → internal/adapters/pexels/ - internal/config/ → internal/infra/config/ - internal/database/ → internal/infra/database/ - internal/locale/ → internal/infra/locale/ - internal/middleware/ → internal/infra/middleware/ - internal/server/ → internal/infra/server/ All import paths and call sites updated accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package middleware_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/food-ai/backend/internal/infra/middleware"
|
|
)
|
|
|
|
func TestRequestID_GeneratesNew(t *testing.T) {
|
|
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
id := middleware.RequestIDFromCtx(r.Context())
|
|
if id == "" {
|
|
t.Error("expected non-empty request ID in context")
|
|
}
|
|
}))
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
if rr.Header().Get("X-Request-ID") == "" {
|
|
t.Error("expected X-Request-ID in response header")
|
|
}
|
|
}
|
|
|
|
func TestRequestID_PreservesExisting(t *testing.T) {
|
|
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
id := middleware.RequestIDFromCtx(r.Context())
|
|
if id != "existing-id" {
|
|
t.Errorf("expected 'existing-id', got %q", id)
|
|
}
|
|
}))
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
req.Header.Set("X-Request-ID", "existing-id")
|
|
rr := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rr, req)
|
|
if rr.Header().Get("X-Request-ID") != "existing-id" {
|
|
t.Error("expected preserved X-Request-ID")
|
|
}
|
|
}
|
|
|
|
func TestRequestID_UniquePerRequest(t *testing.T) {
|
|
var ids []string
|
|
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ids = append(ids, middleware.RequestIDFromCtx(r.Context()))
|
|
}))
|
|
|
|
for i := 0; i < 3; i++ {
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
rr := httptest.NewRecorder()
|
|
handler.ServeHTTP(rr, req)
|
|
}
|
|
|
|
if ids[0] == ids[1] || ids[1] == ids[2] {
|
|
t.Error("expected unique IDs for each request")
|
|
}
|
|
}
|