mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
test: add comprehensive unit tests for provider, middleware, and sync service
- Add TESTING.md documentation explaining unit test conventions and integration testing setup - Add miniredis and sqlite dependencies to go.mod for in-memory testing - Add provider_handler_test.go ensuring Vertex providers default google_location to "global" - Add request_id_test.go verifying request ID generation and header preservation - Add sync_test.go validating Redis snapshot writes and routing key generation - Update README.md with testing section referencing new documentation
This commit is contained in:
57
internal/middleware/request_id_test.go
Normal file
57
internal/middleware/request_id_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestRequestID_GeneratesAndEchoes(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
r := gin.New()
|
||||
r.Use(RequestID())
|
||||
r.GET("/x", func(c *gin.Context) {
|
||||
c.String(200, c.GetHeader("X-Request-ID"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != 200 {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
id := rr.Header().Get("X-Request-ID")
|
||||
if id == "" {
|
||||
t.Fatalf("expected X-Request-ID response header")
|
||||
}
|
||||
if rr.Body.String() != id {
|
||||
t.Fatalf("expected body to echo X-Request-ID, got %q want %q", rr.Body.String(), id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestID_PreservesIncomingHeader(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
r := gin.New()
|
||||
r.Use(RequestID())
|
||||
r.GET("/x", func(c *gin.Context) {
|
||||
c.String(200, c.GetHeader("X-Request-ID"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req.Header.Set("X-Request-ID", "req-abc")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != 200 {
|
||||
t.Fatalf("expected 200, got %d", rr.Code)
|
||||
}
|
||||
if got := rr.Header().Get("X-Request-ID"); got != "req-abc" {
|
||||
t.Fatalf("expected preserved X-Request-ID, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user