Skip to content

Commit 5e81b65

Browse files
committed
fix(auth, executor): normalize Qwen base URL, adjust RefreshLead duration, and add tests
1 parent ad8e396 commit 5e81b65

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

internal/runtime/executor/qwen_executor.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,26 @@ func applyQwenHeaders(r *http.Request, token string, stream bool) {
632632
r.Header.Set("Accept", "application/json")
633633
}
634634

635+
func normaliseQwenBaseURL(resourceURL string) string {
636+
raw := strings.TrimSpace(resourceURL)
637+
if raw == "" {
638+
return ""
639+
}
640+
641+
normalized := raw
642+
lower := strings.ToLower(normalized)
643+
if !strings.HasPrefix(lower, "http://") && !strings.HasPrefix(lower, "https://") {
644+
normalized = "https://" + normalized
645+
}
646+
647+
normalized = strings.TrimRight(normalized, "/")
648+
if !strings.HasSuffix(strings.ToLower(normalized), "/v1") {
649+
normalized += "/v1"
650+
}
651+
652+
return normalized
653+
}
654+
635655
func qwenCreds(a *cliproxyauth.Auth) (token, baseURL string) {
636656
if a == nil {
637657
return "", ""
@@ -649,7 +669,7 @@ func qwenCreds(a *cliproxyauth.Auth) (token, baseURL string) {
649669
token = v
650670
}
651671
if v, ok := a.Metadata["resource_url"].(string); ok {
652-
baseURL = fmt.Sprintf("https://%s/v1", v)
672+
baseURL = normaliseQwenBaseURL(v)
653673
}
654674
}
655675
return

internal/runtime/executor/qwen_executor_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/router-for-me/CLIProxyAPI/v6/internal/thinking"
9+
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
910
"github.com/tidwall/gjson"
1011
)
1112

@@ -176,3 +177,35 @@ func TestWrapQwenError_Maps403QuotaTo429WithoutRetryAfter(t *testing.T) {
176177
t.Fatalf("wrapQwenError retryAfter = %v, want nil", *retryAfter)
177178
}
178179
}
180+
181+
func TestQwenCreds_NormalizesResourceURL(t *testing.T) {
182+
tests := []struct {
183+
name string
184+
resourceURL string
185+
wantBaseURL string
186+
}{
187+
{"host only", "portal.qwen.ai", "https://portal.qwen.ai/v1"},
188+
{"scheme no v1", "https://portal.qwen.ai", "https://portal.qwen.ai/v1"},
189+
{"scheme with v1", "https://portal.qwen.ai/v1", "https://portal.qwen.ai/v1"},
190+
{"scheme with v1 slash", "https://portal.qwen.ai/v1/", "https://portal.qwen.ai/v1"},
191+
}
192+
193+
for _, tt := range tests {
194+
t.Run(tt.name, func(t *testing.T) {
195+
auth := &cliproxyauth.Auth{
196+
Metadata: map[string]any{
197+
"access_token": "test-token",
198+
"resource_url": tt.resourceURL,
199+
},
200+
}
201+
202+
token, baseURL := qwenCreds(auth)
203+
if token != "test-token" {
204+
t.Fatalf("qwenCreds token = %q, want %q", token, "test-token")
205+
}
206+
if baseURL != tt.wantBaseURL {
207+
t.Fatalf("qwenCreds baseURL = %q, want %q", baseURL, tt.wantBaseURL)
208+
}
209+
})
210+
}
211+
}

sdk/auth/qwen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (a *QwenAuthenticator) Provider() string {
2727
}
2828

2929
func (a *QwenAuthenticator) RefreshLead() *time.Duration {
30-
return new(3 * time.Hour)
30+
return new(20 * time.Minute)
3131
}
3232

3333
func (a *QwenAuthenticator) Login(ctx context.Context, cfg *config.Config, opts *LoginOptions) (*coreauth.Auth, error) {

sdk/auth/qwen_refresh_lead_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package auth
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestQwenAuthenticator_RefreshLeadIsSane(t *testing.T) {
9+
lead := NewQwenAuthenticator().RefreshLead()
10+
if lead == nil {
11+
t.Fatal("RefreshLead() = nil, want non-nil")
12+
}
13+
if *lead <= 0 {
14+
t.Fatalf("RefreshLead() = %s, want > 0", *lead)
15+
}
16+
if *lead > 30*time.Minute {
17+
t.Fatalf("RefreshLead() = %s, want <= %s", *lead, 30*time.Minute)
18+
}
19+
}

0 commit comments

Comments
 (0)