@@ -228,8 +228,20 @@ func TestGetTier_两者都为nil(t *testing.T) {
228228// NewClient
229229// ---------------------------------------------------------------------------
230230
231+ func mustNewClient (t * testing.T , proxyURL string ) * Client {
232+ t .Helper ()
233+ client , err := NewClient (proxyURL )
234+ if err != nil {
235+ t .Fatalf ("NewClient(%q) failed: %v" , proxyURL , err )
236+ }
237+ return client
238+ }
239+
231240func TestNewClient_无代理 (t * testing.T ) {
232- client := NewClient ("" )
241+ client , err := NewClient ("" )
242+ if err != nil {
243+ t .Fatalf ("NewClient 返回错误: %v" , err )
244+ }
233245 if client == nil {
234246 t .Fatal ("NewClient 返回 nil" )
235247 }
@@ -246,7 +258,10 @@ func TestNewClient_无代理(t *testing.T) {
246258}
247259
248260func TestNewClient_有代理 (t * testing.T ) {
249- client := NewClient ("http://proxy.example.com:8080" )
261+ client , err := NewClient ("http://proxy.example.com:8080" )
262+ if err != nil {
263+ t .Fatalf ("NewClient 返回错误: %v" , err )
264+ }
250265 if client == nil {
251266 t .Fatal ("NewClient 返回 nil" )
252267 }
@@ -256,7 +271,10 @@ func TestNewClient_有代理(t *testing.T) {
256271}
257272
258273func TestNewClient_空格代理 (t * testing.T ) {
259- client := NewClient (" " )
274+ client , err := NewClient (" " )
275+ if err != nil {
276+ t .Fatalf ("NewClient 返回错误: %v" , err )
277+ }
260278 if client == nil {
261279 t .Fatal ("NewClient 返回 nil" )
262280 }
@@ -267,15 +285,13 @@ func TestNewClient_空格代理(t *testing.T) {
267285}
268286
269287func TestNewClient_无效代理URL (t * testing.T ) {
270- // 无效 URL 时 url.Parse 不一定返回错误(Go 的 url.Parse 很宽容),
271- // 但 ://invalid 会导致解析错误
272- client := NewClient ("://invalid" )
273- if client == nil {
274- t .Fatal ("NewClient 返回 nil" )
288+ // 无效 URL 应返回 error
289+ _ , err := NewClient ("://invalid" )
290+ if err == nil {
291+ t .Fatal ("无效代理 URL 应返回错误" )
275292 }
276- // 无效 URL 解析失败时,Transport 应保持 nil
277- if client .httpClient .Transport != nil {
278- t .Error ("无效代理 URL 时 Transport 应为 nil" )
293+ if ! strings .Contains (err .Error (), "invalid proxy URL" ) {
294+ t .Errorf ("错误信息应包含 'invalid proxy URL': got %s" , err .Error ())
279295 }
280296}
281297
@@ -499,7 +515,7 @@ func TestClient_ExchangeCode_无ClientSecret(t *testing.T) {
499515 defaultClientSecret = ""
500516 t .Cleanup (func () { defaultClientSecret = old })
501517
502- client := NewClient ( "" )
518+ client := mustNewClient ( t , "" )
503519 _ , err := client .ExchangeCode (context .Background (), "code" , "verifier" )
504520 if err == nil {
505521 t .Fatal ("缺少 client_secret 时应返回错误" )
@@ -602,7 +618,7 @@ func TestClient_RefreshToken_无ClientSecret(t *testing.T) {
602618 defaultClientSecret = ""
603619 t .Cleanup (func () { defaultClientSecret = old })
604620
605- client := NewClient ( "" )
621+ client := mustNewClient ( t , "" )
606622 _ , err := client .RefreshToken (context .Background (), "refresh-tok" )
607623 if err == nil {
608624 t .Fatal ("缺少 client_secret 时应返回错误" )
@@ -1242,7 +1258,7 @@ func TestClient_LoadCodeAssist_Success_RealCall(t *testing.T) {
12421258
12431259 withMockBaseURLs (t , []string {server .URL })
12441260
1245- client := NewClient ( "" )
1261+ client := mustNewClient ( t , "" )
12461262 resp , rawResp , err := client .LoadCodeAssist (context .Background (), "test-token" )
12471263 if err != nil {
12481264 t .Fatalf ("LoadCodeAssist 失败: %v" , err )
@@ -1277,7 +1293,7 @@ func TestClient_LoadCodeAssist_HTTPError_RealCall(t *testing.T) {
12771293
12781294 withMockBaseURLs (t , []string {server .URL })
12791295
1280- client := NewClient ( "" )
1296+ client := mustNewClient ( t , "" )
12811297 _ , _ , err := client .LoadCodeAssist (context .Background (), "bad-token" )
12821298 if err == nil {
12831299 t .Fatal ("服务器返回 403 时应返回错误" )
@@ -1300,7 +1316,7 @@ func TestClient_LoadCodeAssist_InvalidJSON_RealCall(t *testing.T) {
13001316
13011317 withMockBaseURLs (t , []string {server .URL })
13021318
1303- client := NewClient ( "" )
1319+ client := mustNewClient ( t , "" )
13041320 _ , _ , err := client .LoadCodeAssist (context .Background (), "token" )
13051321 if err == nil {
13061322 t .Fatal ("无效 JSON 响应应返回错误" )
@@ -1333,7 +1349,7 @@ func TestClient_LoadCodeAssist_URLFallback_RealCall(t *testing.T) {
13331349
13341350 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
13351351
1336- client := NewClient ( "" )
1352+ client := mustNewClient ( t , "" )
13371353 resp , _ , err := client .LoadCodeAssist (context .Background (), "token" )
13381354 if err != nil {
13391355 t .Fatalf ("LoadCodeAssist 应在 fallback 后成功: %v" , err )
@@ -1361,7 +1377,7 @@ func TestClient_LoadCodeAssist_AllURLsFail_RealCall(t *testing.T) {
13611377
13621378 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
13631379
1364- client := NewClient ( "" )
1380+ client := mustNewClient ( t , "" )
13651381 _ , _ , err := client .LoadCodeAssist (context .Background (), "token" )
13661382 if err == nil {
13671383 t .Fatal ("所有 URL 都失败时应返回错误" )
@@ -1377,7 +1393,7 @@ func TestClient_LoadCodeAssist_ContextCanceled_RealCall(t *testing.T) {
13771393
13781394 withMockBaseURLs (t , []string {server .URL })
13791395
1380- client := NewClient ( "" )
1396+ client := mustNewClient ( t , "" )
13811397 ctx , cancel := context .WithCancel (context .Background ())
13821398 cancel ()
13831399
@@ -1441,7 +1457,7 @@ func TestClient_FetchAvailableModels_Success_RealCall(t *testing.T) {
14411457
14421458 withMockBaseURLs (t , []string {server .URL })
14431459
1444- client := NewClient ( "" )
1460+ client := mustNewClient ( t , "" )
14451461 resp , rawResp , err := client .FetchAvailableModels (context .Background (), "test-token" , "project-abc" )
14461462 if err != nil {
14471463 t .Fatalf ("FetchAvailableModels 失败: %v" , err )
@@ -1496,7 +1512,7 @@ func TestClient_FetchAvailableModels_HTTPError_RealCall(t *testing.T) {
14961512
14971513 withMockBaseURLs (t , []string {server .URL })
14981514
1499- client := NewClient ( "" )
1515+ client := mustNewClient ( t , "" )
15001516 _ , _ , err := client .FetchAvailableModels (context .Background (), "bad-token" , "proj" )
15011517 if err == nil {
15021518 t .Fatal ("服务器返回 403 时应返回错误" )
@@ -1516,7 +1532,7 @@ func TestClient_FetchAvailableModels_InvalidJSON_RealCall(t *testing.T) {
15161532
15171533 withMockBaseURLs (t , []string {server .URL })
15181534
1519- client := NewClient ( "" )
1535+ client := mustNewClient ( t , "" )
15201536 _ , _ , err := client .FetchAvailableModels (context .Background (), "token" , "proj" )
15211537 if err == nil {
15221538 t .Fatal ("无效 JSON 响应应返回错误" )
@@ -1546,7 +1562,7 @@ func TestClient_FetchAvailableModels_URLFallback_RealCall(t *testing.T) {
15461562
15471563 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
15481564
1549- client := NewClient ( "" )
1565+ client := mustNewClient ( t , "" )
15501566 resp , _ , err := client .FetchAvailableModels (context .Background (), "token" , "proj" )
15511567 if err != nil {
15521568 t .Fatalf ("FetchAvailableModels 应在 fallback 后成功: %v" , err )
@@ -1574,7 +1590,7 @@ func TestClient_FetchAvailableModels_AllURLsFail_RealCall(t *testing.T) {
15741590
15751591 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
15761592
1577- client := NewClient ( "" )
1593+ client := mustNewClient ( t , "" )
15781594 _ , _ , err := client .FetchAvailableModels (context .Background (), "token" , "proj" )
15791595 if err == nil {
15801596 t .Fatal ("所有 URL 都失败时应返回错误" )
@@ -1590,7 +1606,7 @@ func TestClient_FetchAvailableModels_ContextCanceled_RealCall(t *testing.T) {
15901606
15911607 withMockBaseURLs (t , []string {server .URL })
15921608
1593- client := NewClient ( "" )
1609+ client := mustNewClient ( t , "" )
15941610 ctx , cancel := context .WithCancel (context .Background ())
15951611 cancel ()
15961612
@@ -1610,7 +1626,7 @@ func TestClient_FetchAvailableModels_EmptyModels_RealCall(t *testing.T) {
16101626
16111627 withMockBaseURLs (t , []string {server .URL })
16121628
1613- client := NewClient ( "" )
1629+ client := mustNewClient ( t , "" )
16141630 resp , rawResp , err := client .FetchAvailableModels (context .Background (), "token" , "proj" )
16151631 if err != nil {
16161632 t .Fatalf ("FetchAvailableModels 失败: %v" , err )
@@ -1646,7 +1662,7 @@ func TestClient_LoadCodeAssist_408Fallback_RealCall(t *testing.T) {
16461662
16471663 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
16481664
1649- client := NewClient ( "" )
1665+ client := mustNewClient ( t , "" )
16501666 resp , _ , err := client .LoadCodeAssist (context .Background (), "token" )
16511667 if err != nil {
16521668 t .Fatalf ("LoadCodeAssist 应在 408 fallback 后成功: %v" , err )
@@ -1672,7 +1688,7 @@ func TestClient_FetchAvailableModels_404Fallback_RealCall(t *testing.T) {
16721688
16731689 withMockBaseURLs (t , []string {server1 .URL , server2 .URL })
16741690
1675- client := NewClient ( "" )
1691+ client := mustNewClient ( t , "" )
16761692 resp , _ , err := client .FetchAvailableModels (context .Background (), "token" , "proj" )
16771693 if err != nil {
16781694 t .Fatalf ("FetchAvailableModels 应在 404 fallback 后成功: %v" , err )
0 commit comments