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.
This commit is contained in:
yetone
2026-11-13 19:36:33 +08:00
parent d628a1c803
commit e5994a0159
3 changed files with 18 additions and 0 deletions
+3
View File
@@ -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)
}
+3
View File
@@ -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)
}
+12
View File
@@ -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
}