@@ -6,12 +6,12 @@ import (
66 "io"
77 "net/http"
88 "net/http/httptest"
9- "net/url"
109 "strings"
1110 "testing"
1211
1312 "github.com/gin-gonic/gin"
1413 "github.com/router-for-me/CLIProxyAPI/v7/internal/config"
14+ "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps"
1515 coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
1616 sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config"
1717)
@@ -22,17 +22,25 @@ func (f apiCallRoundTripFunc) RoundTrip(req *http.Request) (*http.Response, erro
2222 return f (req )
2323}
2424
25- func stubChromeAPICallTransport (t * testing.T , proxy * string , trip apiCallRoundTripFunc ) {
25+ func stubAPICallFingerprintTransport (t * testing.T , proxy * string , trip apiCallRoundTripFunc ) {
2626 t .Helper ()
27- original := newChromeAPICallTransport
27+ original := newAPICallFingerprintTransport
2828 t .Cleanup (func () {
29- newChromeAPICallTransport = original
29+ newAPICallFingerprintTransport = original
3030 })
31- newChromeAPICallTransport = func (proxyURL string ) http.RoundTripper {
31+ newAPICallFingerprintTransport = func (proxyURL string , fallback http. RoundTripper ) http.RoundTripper {
3232 if proxy != nil {
3333 * proxy = proxyURL
3434 }
35- return trip
35+ return apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
36+ if helps .IsChatGPTUpstreamURL (req .URL ) {
37+ return trip (req )
38+ }
39+ if fallback == nil {
40+ return nil , io .EOF
41+ }
42+ return fallback .RoundTrip (req )
43+ })
3644 }
3745}
3846
@@ -338,35 +346,6 @@ func TestAuthByIndexDistinguishesSharedAPIKeysAcrossProviders(t *testing.T) {
338346 }
339347}
340348
341- func TestUsesChromeAPICallTLS (t * testing.T ) {
342- t .Parallel ()
343-
344- cases := []struct {
345- name string
346- raw string
347- want bool
348- }{
349- {name : "chatgpt backend-api" , raw : "https://chatgpt.com/backend-api/subscriptions" , want : true },
350- {name : "chatgpt mixed case host" , raw : "https://ChatGPT.com/backend-api/codex/responses" , want : true },
351- {name : "chatgpt http" , raw : "http://chatgpt.com/backend-api/subscriptions" , want : false },
352- {name : "lookalike host" , raw : "https://chatgpt.com.example/backend-api/subscriptions" , want : false },
353- {name : "other https" , raw : "https://api.example.com/v1/ping" , want : false },
354- }
355- for _ , tc := range cases {
356- tc := tc
357- t .Run (tc .name , func (t * testing.T ) {
358- t .Parallel ()
359- parsed , errParse := url .Parse (tc .raw )
360- if errParse != nil {
361- t .Fatalf ("parse url: %v" , errParse )
362- }
363- if got := usesChromeAPICallTLS (parsed ); got != tc .want {
364- t .Fatalf ("usesChromeAPICallTLS(%q) = %v, want %v" , tc .raw , got , tc .want )
365- }
366- })
367- }
368- }
369-
370349func TestApplyChatGPTAPICallHeaderDefaults (t * testing.T ) {
371350 t .Parallel ()
372351
@@ -445,7 +424,7 @@ func TestApplyChatGPTAPICallHeaderDefaults(t *testing.T) {
445424func TestAPICallChatGPTUsesChromeTransportAndPassesHeaders (t * testing.T ) {
446425 var gotProxy string
447426 var gotReq * http.Request
448- stubChromeAPICallTransport (t , & gotProxy , apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
427+ stubAPICallFingerprintTransport (t , & gotProxy , apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
449428 gotReq = req .Clone (req .Context ())
450429 return & http.Response {
451430 StatusCode : http .StatusOK ,
@@ -483,7 +462,7 @@ func TestAPICallChatGPTUsesChromeTransportAndPassesHeaders(t *testing.T) {
483462 t .Fatalf ("upstream body = %q, want subscription JSON" , response .Body )
484463 }
485464 if gotProxy != "http://request-proxy.example.com:8080" {
486- t .Fatalf ("chrome proxy = %q, want request proxy" , gotProxy )
465+ t .Fatalf ("fingerprint proxy = %q, want request proxy" , gotProxy )
487466 }
488467 if gotReq == nil {
489468 t .Fatal ("expected chrome transport to receive the upstream request" )
@@ -501,7 +480,7 @@ func TestAPICallChatGPTUsesChromeTransportAndPassesHeaders(t *testing.T) {
501480
502481func TestAPICallNonChatGPTDoesNotUseChromeTransport (t * testing.T ) {
503482 chromeCalled := false
504- stubChromeAPICallTransport (t , nil , apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
483+ stubAPICallFingerprintTransport (t , nil , apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
505484 chromeCalled = true
506485 return & http.Response {
507486 StatusCode : http .StatusTeapot ,
@@ -551,14 +530,15 @@ func TestAPICallNonChatGPTDoesNotUseChromeTransport(t *testing.T) {
551530 }
552531}
553532
554- func TestAPICallClientTransportPassesResolvedProxyToChrome (t * testing.T ) {
533+ func TestAPICallClientTransportPassesResolvedProxyToFingerprint (t * testing.T ) {
555534 var gotProxy string
556- original := newChromeAPICallTransport
535+ original := newAPICallFingerprintTransport
557536 t .Cleanup (func () {
558- newChromeAPICallTransport = original
537+ newAPICallFingerprintTransport = original
559538 })
560- newChromeAPICallTransport = func (proxyURL string ) http.RoundTripper {
539+ newAPICallFingerprintTransport = func (proxyURL string , fallback http. RoundTripper ) http.RoundTripper {
561540 gotProxy = proxyURL
541+ _ = fallback
562542 return apiCallRoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
563543 return & http.Response {StatusCode : http .StatusOK , Body : http .NoBody , Request : req }, nil
564544 })
@@ -572,24 +552,24 @@ func TestAPICallClientTransportPassesResolvedProxyToChrome(t *testing.T) {
572552 auth := & coreauth.Auth {ProxyURL : "http://credential-proxy.example.com:8080" }
573553 _ = h .apiCallClientTransport (auth , " http://request-proxy.example.com:8080 " )
574554 if gotProxy != "http://request-proxy.example.com:8080" {
575- t .Fatalf ("chrome proxy = %q, want request proxy" , gotProxy )
555+ t .Fatalf ("fingerprint proxy = %q, want request proxy" , gotProxy )
576556 }
577557
578558 gotProxy = ""
579559 _ = h .apiCallClientTransport (auth , "" )
580560 if gotProxy != "http://credential-proxy.example.com:8080" {
581- t .Fatalf ("chrome proxy = %q, want credential proxy" , gotProxy )
561+ t .Fatalf ("fingerprint proxy = %q, want credential proxy" , gotProxy )
582562 }
583563
584564 gotProxy = ""
585565 _ = h .apiCallClientTransport (& coreauth.Auth {ProxyURL : "bad-value" }, "" )
586566 if gotProxy != "http://global-proxy.example.com:8080" {
587- t .Fatalf ("chrome proxy = %q, want global proxy" , gotProxy )
567+ t .Fatalf ("fingerprint proxy = %q, want global proxy" , gotProxy )
588568 }
589569
590570 gotProxy = ""
591571 _ = h .apiCallClientTransport (nil , "" )
592572 if gotProxy != "http://global-proxy.example.com:8080" {
593- t .Fatalf ("chrome proxy = %q, want global proxy when auth is nil" , gotProxy )
573+ t .Fatalf ("fingerprint proxy = %q, want global proxy when auth is nil" , gotProxy )
594574 }
595575}
0 commit comments