test(gateway): realign mux and error response assertions

This commit is contained in:
Your Name
2026-04-17 16:24:05 +08:00
parent 2e0f6e29aa
commit a31ea09045
2 changed files with 17 additions and 4 deletions

View File

@@ -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)

View File

@@ -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) {