sub2api/backend/internal/service/gateway_attribution_test.go
win 9da079a5ee
Some checks failed
Security Scan / backend-security (push) Failing after 3s
Security Scan / frontend-security (push) Failing after 5s
CI / test (push) Failing after 3s
CI / frontend (push) Failing after 3s
CI / golangci-lint (push) Failing after 3s
CI / windsurf-platform (macos-latest) (push) Has been cancelled
CI / windsurf-platform (windows-latest) (push) Has been cancelled
x
2026-04-27 19:01:41 +08:00

82 lines
2.7 KiB
Go

package service
import (
"net/http"
"testing"
)
func TestApplyClaudeRuntimeOptionalHeaders(t *testing.T) {
t.Setenv("CLAUDE_CODE_CONTAINER_ID", "ctr-123")
t.Setenv("CLAUDE_CODE_REMOTE_SESSION_ID", "remote-456")
t.Setenv("CLAUDE_AGENT_SDK_CLIENT_APP", "desktop")
t.Setenv("CLAUDE_CODE_ADDITIONAL_PROTECTION", "true")
req, err := http.NewRequest(http.MethodPost, "https://api.anthropic.com/v1/messages", nil)
if err != nil {
t.Fatalf("NewRequest() error = %v", err)
}
applyClaudeRuntimeOptionalHeaders(req)
if got := getHeaderRaw(req.Header, "x-claude-remote-container-id"); got != "ctr-123" {
t.Fatalf("x-claude-remote-container-id = %q", got)
}
if got := getHeaderRaw(req.Header, "x-claude-remote-session-id"); got != "remote-456" {
t.Fatalf("x-claude-remote-session-id = %q", got)
}
if got := getHeaderRaw(req.Header, "x-client-app"); got != "desktop" {
t.Fatalf("x-client-app = %q", got)
}
if got := getHeaderRaw(req.Header, "x-anthropic-additional-protection"); got != "true" {
t.Fatalf("x-anthropic-additional-protection = %q", got)
}
}
func TestBuildAttributionBlock_UsesEntrypointAndWorkload(t *testing.T) {
t.Setenv("CLAUDE_CODE_ATTRIBUTION_HEADER", "")
got := buildAttributionBlock("2.1.104", "abc", attributionBlockOptions{
Entrypoint: "sdk-cli",
Workload: "cron",
})
want := "x-anthropic-billing-header: cc_version=2.1.104.abc; cc_entrypoint=sdk-cli; cch=00000; cc_workload=cron;"
if got != want {
t.Fatalf("buildAttributionBlock() = %q, want %q", got, want)
}
}
func TestBuildAttributionBlock_OmitsCCHForBedrockLikeProviders(t *testing.T) {
t.Setenv("CLAUDE_CODE_ATTRIBUTION_HEADER", "")
got := buildAttributionBlock("2.1.104", "abc", attributionBlockOptions{
Entrypoint: "cli",
OmitCCH: true,
})
want := "x-anthropic-billing-header: cc_version=2.1.104.abc; cc_entrypoint=cli;"
if got != want {
t.Fatalf("buildAttributionBlock() = %q, want %q", got, want)
}
}
func TestInjectAttributionBlock_DisabledByEnv(t *testing.T) {
t.Setenv("CLAUDE_CODE_ATTRIBUTION_HEADER", "false")
body := []byte(`{"messages":[{"role":"user","content":"hello"}]}`)
got := injectAttributionBlock(body, "2.1.104", attributionBlockOptions{})
if string(got) != string(body) {
t.Fatalf("injectAttributionBlock() should keep body unchanged when attribution disabled")
}
}
func TestShouldOmitAttributionCCH(t *testing.T) {
if !shouldOmitAttributionCCH(&Account{Type: AccountTypeBedrock}, "") {
t.Fatal("expected bedrock account to omit cch")
}
if !shouldOmitAttributionCCH(&Account{Extra: map[string]any{"provider": "mantle"}}, "") {
t.Fatal("expected mantle provider to omit cch")
}
if shouldOmitAttributionCCH(&Account{Type: AccountTypeOAuth}, "oauth") {
t.Fatal("expected oauth account to keep cch")
}
}