mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-22 09:10:30 +00:00
204 lines
6.7 KiB
Go
204 lines
6.7 KiB
Go
package executor
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
|
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
|
sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func TestBuildCodexWebsocketRequestBodyPreservesPreviousResponseID(t *testing.T) {
|
|
body := []byte(`{"model":"gpt-5-codex","previous_response_id":"resp-1","input":[{"type":"message","id":"msg-1"}]}`)
|
|
|
|
wsReqBody := buildCodexWebsocketRequestBody(body)
|
|
|
|
if got := gjson.GetBytes(wsReqBody, "type").String(); got != "response.create" {
|
|
t.Fatalf("type = %s, want response.create", got)
|
|
}
|
|
if got := gjson.GetBytes(wsReqBody, "previous_response_id").String(); got != "resp-1" {
|
|
t.Fatalf("previous_response_id = %s, want resp-1", got)
|
|
}
|
|
if gjson.GetBytes(wsReqBody, "input.0.id").String() != "msg-1" {
|
|
t.Fatalf("input item id mismatch")
|
|
}
|
|
if got := gjson.GetBytes(wsReqBody, "type").String(); got == "response.append" {
|
|
t.Fatalf("unexpected websocket request type: %s", got)
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexWebsocketHeadersDefaultsToCurrentResponsesBeta(t *testing.T) {
|
|
headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, nil, "", nil)
|
|
|
|
if got := headers.Get("OpenAI-Beta"); got != codexResponsesWebsocketBetaHeaderValue {
|
|
t.Fatalf("OpenAI-Beta = %s, want %s", got, codexResponsesWebsocketBetaHeaderValue)
|
|
}
|
|
if got := headers.Get("User-Agent"); got != codexUserAgent {
|
|
t.Fatalf("User-Agent = %s, want %s", got, codexUserAgent)
|
|
}
|
|
if got := headers.Get("x-codex-beta-features"); got != "" {
|
|
t.Fatalf("x-codex-beta-features = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexWebsocketHeadersUsesConfigDefaultsForOAuth(t *testing.T) {
|
|
cfg := &config.Config{
|
|
CodexHeaderDefaults: config.CodexHeaderDefaults{
|
|
UserAgent: "my-codex-client/1.0",
|
|
BetaFeatures: "feature-a,feature-b",
|
|
},
|
|
}
|
|
auth := &cliproxyauth.Auth{
|
|
Provider: "codex",
|
|
Metadata: map[string]any{"email": "user@example.com"},
|
|
}
|
|
|
|
headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, auth, "", cfg)
|
|
|
|
if got := headers.Get("User-Agent"); got != "my-codex-client/1.0" {
|
|
t.Fatalf("User-Agent = %s, want %s", got, "my-codex-client/1.0")
|
|
}
|
|
if got := headers.Get("x-codex-beta-features"); got != "feature-a,feature-b" {
|
|
t.Fatalf("x-codex-beta-features = %s, want %s", got, "feature-a,feature-b")
|
|
}
|
|
if got := headers.Get("OpenAI-Beta"); got != codexResponsesWebsocketBetaHeaderValue {
|
|
t.Fatalf("OpenAI-Beta = %s, want %s", got, codexResponsesWebsocketBetaHeaderValue)
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexWebsocketHeadersPrefersExistingHeadersOverClientAndConfig(t *testing.T) {
|
|
cfg := &config.Config{
|
|
CodexHeaderDefaults: config.CodexHeaderDefaults{
|
|
UserAgent: "config-ua",
|
|
BetaFeatures: "config-beta",
|
|
},
|
|
}
|
|
auth := &cliproxyauth.Auth{
|
|
Provider: "codex",
|
|
Metadata: map[string]any{"email": "user@example.com"},
|
|
}
|
|
ctx := contextWithGinHeaders(map[string]string{
|
|
"User-Agent": "client-ua",
|
|
"X-Codex-Beta-Features": "client-beta",
|
|
})
|
|
headers := http.Header{}
|
|
headers.Set("User-Agent", "existing-ua")
|
|
headers.Set("X-Codex-Beta-Features", "existing-beta")
|
|
|
|
got := applyCodexWebsocketHeaders(ctx, headers, auth, "", cfg)
|
|
|
|
if gotVal := got.Get("User-Agent"); gotVal != "existing-ua" {
|
|
t.Fatalf("User-Agent = %s, want %s", gotVal, "existing-ua")
|
|
}
|
|
if gotVal := got.Get("x-codex-beta-features"); gotVal != "existing-beta" {
|
|
t.Fatalf("x-codex-beta-features = %s, want %s", gotVal, "existing-beta")
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexWebsocketHeadersConfigUserAgentOverridesClientHeader(t *testing.T) {
|
|
cfg := &config.Config{
|
|
CodexHeaderDefaults: config.CodexHeaderDefaults{
|
|
UserAgent: "config-ua",
|
|
BetaFeatures: "config-beta",
|
|
},
|
|
}
|
|
auth := &cliproxyauth.Auth{
|
|
Provider: "codex",
|
|
Metadata: map[string]any{"email": "user@example.com"},
|
|
}
|
|
ctx := contextWithGinHeaders(map[string]string{
|
|
"User-Agent": "client-ua",
|
|
"X-Codex-Beta-Features": "client-beta",
|
|
})
|
|
|
|
headers := applyCodexWebsocketHeaders(ctx, http.Header{}, auth, "", cfg)
|
|
|
|
if got := headers.Get("User-Agent"); got != "config-ua" {
|
|
t.Fatalf("User-Agent = %s, want %s", got, "config-ua")
|
|
}
|
|
if got := headers.Get("x-codex-beta-features"); got != "client-beta" {
|
|
t.Fatalf("x-codex-beta-features = %s, want %s", got, "client-beta")
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexWebsocketHeadersIgnoresConfigForAPIKeyAuth(t *testing.T) {
|
|
cfg := &config.Config{
|
|
CodexHeaderDefaults: config.CodexHeaderDefaults{
|
|
UserAgent: "config-ua",
|
|
BetaFeatures: "config-beta",
|
|
},
|
|
}
|
|
auth := &cliproxyauth.Auth{
|
|
Provider: "codex",
|
|
Attributes: map[string]string{"api_key": "sk-test"},
|
|
}
|
|
|
|
headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, auth, "sk-test", cfg)
|
|
|
|
if got := headers.Get("User-Agent"); got != codexUserAgent {
|
|
t.Fatalf("User-Agent = %s, want %s", got, codexUserAgent)
|
|
}
|
|
if got := headers.Get("x-codex-beta-features"); got != "" {
|
|
t.Fatalf("x-codex-beta-features = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func TestApplyCodexHeadersUsesConfigUserAgentForOAuth(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodPost, "https://example.com/responses", nil)
|
|
if err != nil {
|
|
t.Fatalf("NewRequest() error = %v", err)
|
|
}
|
|
cfg := &config.Config{
|
|
CodexHeaderDefaults: config.CodexHeaderDefaults{
|
|
UserAgent: "config-ua",
|
|
BetaFeatures: "config-beta",
|
|
},
|
|
}
|
|
auth := &cliproxyauth.Auth{
|
|
Provider: "codex",
|
|
Metadata: map[string]any{"email": "user@example.com"},
|
|
}
|
|
req = req.WithContext(contextWithGinHeaders(map[string]string{
|
|
"User-Agent": "client-ua",
|
|
}))
|
|
|
|
applyCodexHeaders(req, auth, "oauth-token", true, cfg)
|
|
|
|
if got := req.Header.Get("User-Agent"); got != "config-ua" {
|
|
t.Fatalf("User-Agent = %s, want %s", got, "config-ua")
|
|
}
|
|
if got := req.Header.Get("x-codex-beta-features"); got != "" {
|
|
t.Fatalf("x-codex-beta-features = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
func contextWithGinHeaders(headers map[string]string) context.Context {
|
|
gin.SetMode(gin.TestMode)
|
|
recorder := httptest.NewRecorder()
|
|
ginCtx, _ := gin.CreateTestContext(recorder)
|
|
ginCtx.Request = httptest.NewRequest(http.MethodPost, "/", nil)
|
|
ginCtx.Request.Header = make(http.Header, len(headers))
|
|
for key, value := range headers {
|
|
ginCtx.Request.Header.Set(key, value)
|
|
}
|
|
return context.WithValue(context.Background(), "gin", ginCtx)
|
|
}
|
|
|
|
func TestNewProxyAwareWebsocketDialerDirectDisablesProxy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dialer := newProxyAwareWebsocketDialer(
|
|
&config.Config{SDKConfig: sdkconfig.SDKConfig{ProxyURL: "http://global-proxy.example.com:8080"}},
|
|
&cliproxyauth.Auth{ProxyURL: "direct"},
|
|
)
|
|
|
|
if dialer.Proxy != nil {
|
|
t.Fatal("expected websocket proxy function to be nil for direct mode")
|
|
}
|
|
}
|