package traefikllmgateway import ( "context" "io" "net/http" "net/http/httptest" "strings" "sync" "testing" "time" ) const logScrubMarker = "LOGSCRUBMARKER42" // holdingUpstream answers nothing: it signals held, then waits for the // client to give up. The body is drained so the server notices the close. func holdingUpstream() (chan struct{}, *httptest.Server) { held := make(chan struct{}) var once sync.Once return held, httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = io.Copy(io.Discard, r.Body) once.Do(func() { close(held) }) select { case <-r.Context().Done(): case <-time.After(10 * time.Second): } })) } func unifiedChatLog(t *testing.T, baseURL string, ctx context.Context) string { t.Helper() gw := newStreamBillingGateway(t, providerTypeOpenAI, baseURL, "gpt-test") body := map[string]any{"model": "gpt-test", "messages": []any{map[string]any{"role": "user", "content": "hi"}}} req := newUnifiedRequest(t, http.MethodPost, "/v1/chat/completions", "sk-alice", body).WithContext(ctx) return captureStderr(t, func() { gw.ServeHTTP(httptest.NewRecorder(), req) }) } func TestUnifiedRoute_ConnectionErrorLogOmitsBaseURLUserinfo(t *testing.T) { out := unifiedChatLog(t, "http://"+logScrubMarker+"@"+closedLoopbackAddr(t)+"/v1", context.Background()) if !strings.Contains(out, "upstream connection error") { t.Fatalf("precondition: no connection error logged: %q", out) } if strings.Contains(out, logScrubMarker) { t.Errorf("log carries the base URL credential: %q", out) } } func TestUnifiedRoute_BuildRequestErrorLogOmitsUnparseableBaseURL(t *testing.T) { out := unifiedChatLog(t, "http://user:"+logScrubMarker+"@"+closedLoopbackAddr(t)+"\n", context.Background()) if !strings.Contains(out, "ERROR") { t.Fatalf("precondition: nothing logged at error level: %q", out) } if strings.Contains(out, logScrubMarker) { t.Errorf("log carries the unparseable base URL credential: %q", out) } } func TestUnifiedRoute_ClientCancelLogOmitsBaseURLUserinfo(t *testing.T) { held, up := holdingUpstream() defer up.Close() ctx, cancel := context.WithCancel(context.Background()) go func() { <-held cancel() }() out := unifiedChatLog(t, strings.Replace(up.URL, "http://", "http://"+logScrubMarker+"@", 1)+"/v1", ctx) if !strings.Contains(out, "canceled") { t.Fatalf("precondition: no cancel logged: %q", out) } if strings.Contains(out, logScrubMarker) { t.Errorf("log carries the base URL credential: %q", out) } } func targetProxyLog(t *testing.T, ctx context.Context, targetURL string) string { t.Helper() h := newTargetRedirectGateway(t, targetURL, targetURL) req := httptest.NewRequest(http.MethodGet, "/mcp/alpha", nil).WithContext(ctx) req.Header.Set("Authorization", "Bearer sk-alice") return captureStderr(t, func() { h.ServeHTTP(httptest.NewRecorder(), req) }) } func TestTargetProxy_ClientCancelLogOmitsTargetQuery(t *testing.T) { held, up := holdingUpstream() defer up.Close() ctx, cancel := context.WithCancel(context.Background()) go func() { select { case <-held: case <-time.After(10 * time.Second): } cancel() }() out := targetProxyLog(t, ctx, up.URL+"/mcp?api-key="+logScrubMarker) if !strings.Contains(out, "client canceled request") { t.Fatalf("precondition: no cancel logged: %q", out) } if strings.Contains(out, logScrubMarker) { t.Errorf("log carries the target query credential: %q", out) } } func TestTargetProxy_ConnectionErrorLogStillScrubbed(t *testing.T) { out := targetProxyLog(t, context.Background(), "http://"+closedLoopbackAddr(t)+"/mcp?api-key="+logScrubMarker) if !strings.Contains(out, "upstream connection error") { t.Fatalf("precondition: no connection error logged: %q", out) } if strings.Contains(out, logScrubMarker) { t.Errorf("log carries the target query credential: %q", out) } }