Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSyncBillingHeaderVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
userAgent string
|
||||
wantSub string // substring expected in result
|
||||
unchanged bool // expect body to remain the same
|
||||
}{
|
||||
{
|
||||
name: "replaces cc_version preserving message-derived suffix",
|
||||
body: `{"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.81.df2; cc_entrypoint=cli; cch=00000;"},{"type":"text","text":"You are Claude Code.","cache_control":{"type":"ephemeral"}}],"messages":[]}`,
|
||||
userAgent: "claude-cli/2.1.22 (external, cli)",
|
||||
wantSub: "cc_version=2.1.22.df2",
|
||||
},
|
||||
{
|
||||
name: "no billing header in system",
|
||||
body: `{"system":[{"type":"text","text":"You are Claude Code."}],"messages":[]}`,
|
||||
userAgent: "claude-cli/2.1.22",
|
||||
unchanged: true,
|
||||
},
|
||||
{
|
||||
name: "no system field",
|
||||
body: `{"messages":[]}`,
|
||||
userAgent: "claude-cli/2.1.22",
|
||||
unchanged: true,
|
||||
},
|
||||
{
|
||||
name: "user-agent without version",
|
||||
body: `{"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.81; cc_entrypoint=cli; cch=00000;"}],"messages":[]}`,
|
||||
userAgent: "Mozilla/5.0",
|
||||
unchanged: true,
|
||||
},
|
||||
{
|
||||
name: "empty user-agent",
|
||||
body: `{"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.81; cc_entrypoint=cli; cch=00000;"}],"messages":[]}`,
|
||||
userAgent: "",
|
||||
unchanged: true,
|
||||
},
|
||||
{
|
||||
name: "version already matches",
|
||||
body: `{"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.22; cc_entrypoint=cli; cch=00000;"}],"messages":[]}`,
|
||||
userAgent: "claude-cli/2.1.22",
|
||||
unchanged: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := syncBillingHeaderVersion([]byte(tt.body), tt.userAgent)
|
||||
if tt.unchanged {
|
||||
assert.Equal(t, tt.body, string(result), "body should remain unchanged")
|
||||
} else {
|
||||
assert.Contains(t, string(result), tt.wantSub)
|
||||
// Ensure old semver is gone
|
||||
assert.NotContains(t, string(result), "cc_version=2.1.81")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user