From a31ea09045dfc3da32ffb220f356291fe4f82943 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 17 Apr 2026 16:24:05 +0800 Subject: [PATCH] test(gateway): realign mux and error response assertions --- gateway/cmd/gateway/main_test.go | 9 ++++++--- gateway/internal/handler/handler_test.go | 12 +++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/gateway/cmd/gateway/main_test.go b/gateway/cmd/gateway/main_test.go index a2cb83cc..204b0b0e 100644 --- a/gateway/cmd/gateway/main_test.go +++ b/gateway/cmd/gateway/main_test.go @@ -85,8 +85,9 @@ func TestCreateMux_ProtectsCompletionRoutes(t *testing.T) { ExcludedPrefixes: []string{"/health", "/healthz", "/readyz"}, Now: func() time.Time { return now }, } + corsConfig := middleware.DefaultCORSConfig() - mux := app.BuildMux(h, limiter, authConfig) + mux := app.BuildMux(h, limiter, authConfig, corsConfig) for _, path := range []string{ "/v1/chat/completions", @@ -118,8 +119,9 @@ func TestCreateMux_HealthRoutesRemainOpen(t *testing.T) { ExcludedPrefixes: []string{"/health", "/healthz", "/readyz"}, Now: func() time.Time { return now }, } + corsConfig := middleware.DefaultCORSConfig() - mux := app.BuildMux(h, limiter, authConfig) + mux := app.BuildMux(h, limiter, authConfig, corsConfig) req := httptest.NewRequest(http.MethodGet, "/health", nil) rr := httptest.NewRecorder() mux.ServeHTTP(rr, req) @@ -153,8 +155,9 @@ func TestCreateMux_CompletionsRouteUsesCompletionsHandler(t *testing.T) { ExcludedPrefixes: []string{"/health", "/healthz", "/readyz"}, Now: func() time.Time { return now }, } + corsConfig := middleware.DefaultCORSConfig() - mux := app.BuildMux(h, limiter, authConfig) + mux := app.BuildMux(h, limiter, authConfig, corsConfig) reqBody := `{"model":"gpt-4","prompt":"hello","max_tokens":16}` req := httptest.NewRequest(http.MethodPost, "/v1/completions", bytes.NewBufferString(reqBody)) req.Header.Set("Authorization", "Bearer "+token) diff --git a/gateway/internal/handler/handler_test.go b/gateway/internal/handler/handler_test.go index 1a191b56..54e0716c 100644 --- a/gateway/internal/handler/handler_test.go +++ b/gateway/internal/handler/handler_test.go @@ -408,7 +408,7 @@ func TestWriteError(t *testing.T) { t.Fatalf("failed to unmarshal response: %v", err) } - if resp.Error.Message != "test error" { + if resp.Error.Message != "invalid request" { t.Errorf("unexpected error message: %s", resp.Error.Message) } if resp.Error.Type != "gateway_error" { @@ -417,6 +417,16 @@ func TestWriteError(t *testing.T) { if resp.Error.Code != "COMMON_001" { t.Errorf("unexpected error code: %s", resp.Error.Code) } + if got := w.Header().Get("X-Request-ID"); got != "req-123" { + t.Errorf("expected X-Request-ID req-123, got %s", got) + } +} + +func TestSanitizeRequestID(t *testing.T) { + got := sanitizeRequestID("req-123\nbad\tid!@#") + if got != "req-123badid" { + t.Fatalf("unexpected sanitized request id: got=%q want=%q", got, "req-123badid") + } } func TestGenerateRequestID(t *testing.T) {