package traefikllmgateway import ( "context" "net/http" "net/http/httptest" "strings" "testing" ) const redirectSecret = "SECRETQUERYTOKEN" func newTargetRedirectGateway(t *testing.T, mcpURL, agentURL string) http.Handler { t.Helper() cfg := CreateConfig() cfg.Providers = map[string]*ProviderConfig{"openai": {Type: "openai", APIKey: "k"}} cfg.MCPServers = map[string]*TargetConfig{"alpha": {URL: mcpURL}} cfg.Agents = map[string]*AgentConfig{"ag": {URL: agentURL}} cfg.Groups = map[string]*GroupConfig{"default": {}} cfg.Users = &UsersConfig{Inline: []*UserConfig{{Name: "alice", Group: "default", APIKey: "sk-alice"}}} h, err := New(context.Background(), http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}), cfg, "llmgw") if err != nil { t.Fatalf("New: %v", err) } return h } func redirectingUpstream() *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.HasSuffix(r.URL.Path, "/tools") { _, _ = w.Write([]byte("ok")) return } http.Redirect(w, r, "http://elsewhere.invalid/x?"+r.URL.RawQuery, http.StatusTemporaryRedirect) //nolint:gosec // test upstream echoing the query })) } func doTarget(h http.Handler, path string) *httptest.ResponseRecorder { req := httptest.NewRequest(http.MethodGet, path, nil) req.Header.Set("Authorization", "Bearer sk-alice") rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec } func TestTargetProxy_RedirectWithTargetQueryNotRelayed(t *testing.T) { up := redirectingUpstream() defer up.Close() h := newTargetRedirectGateway(t, up.URL+"/mcp?token="+redirectSecret, up.URL+"/a2a?token="+redirectSecret) for _, path := range []string{"/mcp/alpha", "/a2a/ag"} { rec := doTarget(h, path) if rec.Code != http.StatusBadGateway { t.Errorf("%s: status = %d, want 502", path, rec.Code) } if loc := rec.Header().Get("Location"); loc != "" { t.Errorf("%s: Location relayed: %q", path, loc) } if strings.Contains(rec.Body.String(), redirectSecret) || strings.Contains(rec.Header().Get("Location"), redirectSecret) { t.Errorf("%s: secret reached the client", path) } } } func TestTargetProxy_RedirectWithoutTargetQueryStillRelayed(t *testing.T) { up := redirectingUpstream() defer up.Close() h := newTargetRedirectGateway(t, up.URL+"/mcp", up.URL+"/a2a") rec := doTarget(h, "/mcp/alpha") if rec.Code != http.StatusTemporaryRedirect || rec.Header().Get("Location") == "" { t.Errorf("status = %d, Location = %q, want 307 relayed", rec.Code, rec.Header().Get("Location")) } } func TestTargetProxy_ClientOnlyQueryRedirectStillRelayed(t *testing.T) { up := redirectingUpstream() defer up.Close() h := newTargetRedirectGateway(t, up.URL+"/mcp", up.URL+"/a2a") rec := doTarget(h, "/mcp/alpha?x=1") if rec.Code != http.StatusTemporaryRedirect || rec.Header().Get("Location") == "" { t.Errorf("status = %d, Location = %q, want 307 relayed", rec.Code, rec.Header().Get("Location")) } } func TestTargetProxy_NonRedirectUnchanged(t *testing.T) { up := redirectingUpstream() defer up.Close() h := newTargetRedirectGateway(t, up.URL+"/mcp?token="+redirectSecret, up.URL+"/a2a") rec := doTarget(h, "/mcp/alpha/tools") if rec.Code != http.StatusOK || rec.Body.String() != "ok" { t.Errorf("status = %d body = %q, want 200 ok", rec.Code, rec.Body.String()) } }