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:
zenfun
2025-12-14 00:35:35 +08:00
parent 50da2ff668
commit d0011f3eb2
7 changed files with 279 additions and 0 deletions

View 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)
}
}