- Restructure handlers into file_commands/file_queries/file_handlers - Add gRPC auth client, JWT middleware, rate limiting, request ID - Add common utilities: logger, sanitizer, s3_errors - Add unit tests for config, mediator, auth, request_id, sanitize - Add proto definitions and generated code - Remove old web UI pages - Add .dockerignore and .env.example
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func init() {
|
|
gin.SetMode(gin.TestMode)
|
|
}
|
|
|
|
func TestRequestIDMiddleware_GeneratesID(t *testing.T) {
|
|
r := gin.New()
|
|
r.Use(RequestIDMiddleware())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
gotID := w.Header().Get("X-Request-ID")
|
|
if gotID == "" {
|
|
t.Error("expected X-Request-ID to be generated, got empty string")
|
|
}
|
|
if len(gotID) != 16 {
|
|
t.Errorf("expected 16-char hex ID, got %d chars: %q", len(gotID), gotID)
|
|
}
|
|
}
|
|
|
|
func TestRequestIDMiddleware_PreservesExisting(t *testing.T) {
|
|
r := gin.New()
|
|
r.Use(RequestIDMiddleware())
|
|
r.GET("/test", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest(http.MethodGet, "/test", nil)
|
|
req.Header.Set("X-Request-ID", "my-custom-id")
|
|
r.ServeHTTP(w, req)
|
|
|
|
gotID := w.Header().Get("X-Request-ID")
|
|
if gotID != "my-custom-id" {
|
|
t.Errorf("expected X-Request-ID to be preserved as %q, got %q", "my-custom-id", gotID)
|
|
}
|
|
}
|