From e5994a0159e88dbcfbb04f8b587b8b7fee3a9c19 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 19:36:33 +0800 Subject: [PATCH] gateway tests: a fake upstream judges only what the gateway sent it (#6535) TestTencentTokenHubRoutes went red once in a full internal/gateway run on the Linux box: "asked at /ws/herdr". Another program on the machine, a herdr client, reached the fake upstream's loopback port, and the test fails on any path its fake was asked at. TestQianfan's and Bedrock's "asked at" loops do the same. All three now judge only requests that carry the test's key (sentByTest). The gateway sends that key on every call, so a wrong path it asks at still fails. Run: - wsl-run go test ./internal/gateway -count=3 -run 'TestTencentTokenHubRoutes|Qianfan|Bedrock': ok. - GOOS=windows go vet ./internal/gateway: ok. gofmt -l: clean. - Broken by hand: a GET of srv.URL+"/ws/herdr" added before TokenHub's check is green with the filter, and red ("tokenhub_test.go:155: asked at /ws/herdr") with it taken out. Not changed: the shared fake that native_compaction_test.go's "asked at" reads keeps only the last request's path, so a stray request can still overwrite it. Noted on #6535. --- internal/gateway/bedrock_test.go | 3 +++ internal/gateway/qianfan_test.go | 3 +++ internal/gateway/tokenhub_test.go | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/internal/gateway/bedrock_test.go b/internal/gateway/bedrock_test.go index 9432d8587..7b4432b8c 100644 --- a/internal/gateway/bedrock_test.go +++ b/internal/gateway/bedrock_test.go @@ -134,6 +134,9 @@ func TestBedrockRoutes(t *testing.T) { up.mu.Lock() defer up.mu.Unlock() for _, c := range up.calls { + if !sentByTest(c.head, "ABSK-test") { + continue + } if c.path != "/anthropic/v1/messages" && c.path != "/openai/v1/chat/completions" { t.Errorf("asked at %s", c.path) } diff --git a/internal/gateway/qianfan_test.go b/internal/gateway/qianfan_test.go index 68f18aebc..4b093cd06 100644 --- a/internal/gateway/qianfan_test.go +++ b/internal/gateway/qianfan_test.go @@ -129,6 +129,9 @@ func TestQianfanTokenPlanRoutes(t *testing.T) { up.mu.Lock() defer up.mu.Unlock() for _, c := range up.calls { + if !sentByTest(c.head, "bce-v3-test") { + continue + } if !strings.HasPrefix(c.path, "/v2/tokenplan/personal/") && !strings.HasPrefix(c.path, "/anthropic/tokenplan/personal/") { t.Errorf("asked at %s", c.path) } diff --git a/internal/gateway/tokenhub_test.go b/internal/gateway/tokenhub_test.go index 6d7a0341e..2db8903aa 100644 --- a/internal/gateway/tokenhub_test.go +++ b/internal/gateway/tokenhub_test.go @@ -145,6 +145,9 @@ func TestTencentTokenHubRoutes(t *testing.T) { up.mu.Lock() defer up.mu.Unlock() for _, c := range up.calls { + if !sentByTest(c.head, "sk-tokenhub-test") { + continue + } if c.path != "/v1/chat/completions" && c.path != "/v1/responses" && c.path != "/v1/messages" { t.Errorf("asked at %s", c.path) } @@ -152,3 +155,12 @@ func TestTencentTokenHubRoutes(t *testing.T) { }) } } + +// sentByTest tells a request the gateway sent with the test's key from one +// any other program on the machine sent to the fake's loopback port: a herdr +// client's /ws/herdr reached TestTencentTokenHubRoutes' upstream on the +// Linux box and turned it red (#6535). The checks of every path asked judge +// only the former. +func sentByTest(h http.Header, key string) bool { + return h.Get("Authorization") == "Bearer "+key || h.Get("x-api-key") == key +}