Files
CLIProxyAPI/internal/runtime/executor/helps/utls_client_test.go
Luis Pater 35ab084fc3 refactor(runtime): enhance NewUtlsHTTPClient with context-based RoundTripper
- Updated `NewUtlsHTTPClient` to support context-aware RoundTrippers for protected hosts (e.g., Cloudflare bypass).
- Replaced `anthropicHosts` with `utlsProtectedHosts` to generalize host handling logic.
- Added unit test to validate context-based RoundTripper behavior.
- Replaced `NewProxyAwareHTTPClient` with `NewUtlsHTTPClient` in relevant executors for improved TLS fingerprinting.

Closes: #3680
2026-06-03 06:58:26 +08:00

46 lines
1.2 KiB
Go

package helps
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
type utlsClientRoundTripFunc func(*http.Request) (*http.Response, error)
func (f utlsClientRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
func TestNewUtlsHTTPClientUsesContextRoundTripperForProtectedHost(t *testing.T) {
t.Parallel()
called := false
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", utlsClientRoundTripFunc(func(req *http.Request) (*http.Response, error) {
called = true
if req.URL.Hostname() != "chatgpt.com" {
t.Fatalf("hostname = %q, want chatgpt.com", req.URL.Hostname())
}
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("{}")),
Request: req,
}, nil
}))
client := NewUtlsHTTPClient(ctx, nil, nil, 0)
resp, err := client.Get("https://chatgpt.com/backend-api/codex/responses")
if err != nil {
t.Fatalf("client.Get returned error: %v", err)
}
if errClose := resp.Body.Close(); errClose != nil {
t.Fatalf("response body close returned error: %v", errClose)
}
if !called {
t.Fatal("expected context RoundTripper to handle protected host request")
}
}