fix(telegram): honor proxy fallbacks for bot api

This commit is contained in:
ShukeBta
2026-06-07 13:58:24 +08:00
parent 1d88a09568
commit b35b36738e
5 changed files with 152 additions and 50 deletions
+35 -1
View File
@@ -2,8 +2,10 @@ package service
import (
"errors"
"net/http"
"strings"
"testing"
"time"
)
func TestTelegramMethodURLUsesCustomAPIBase(t *testing.T) {
@@ -90,13 +92,45 @@ func TestTelegramTargetChatIDsUsesLegacyPrivateChatID(t *testing.T) {
func TestTelegramProxyCandidatesDefaultLocalFallbacks(t *testing.T) {
got := telegramProxyCandidates(map[string]string{})
joined := strings.Join(got, ",")
for _, want := range []string{"127.0.0.1:10808", "172.17.0.1:7890"} {
for _, want := range []string{"host.docker.internal:20171", "socks5://172.17.0.1:20170", "127.0.0.1:10808", "172.17.0.1:7890"} {
if !strings.Contains(joined, want) {
t.Fatalf("default proxy candidates %q missing %q", joined, want)
}
}
}
func TestTelegramHTTPClientsPreferConfiguredProxy(t *testing.T) {
clients := telegramHTTPClients(time.Second, map[string]string{
"proxy_url": "http://proxy.example:7890",
})
if len(clients) == 0 {
t.Fatal("expected telegram clients")
}
if got := telegramClientProxyString(t, clients[0]); got != "http://proxy.example:7890" {
t.Fatalf("first client proxy = %q, want configured proxy", got)
}
}
func telegramClientProxyString(t *testing.T, client *http.Client) string {
t.Helper()
transport, ok := client.Transport.(*http.Transport)
if !ok || transport.Proxy == nil {
return ""
}
req, err := http.NewRequest(http.MethodGet, defaultTelegramAPIBaseURL, nil)
if err != nil {
t.Fatal(err)
}
proxyURL, err := transport.Proxy(req)
if err != nil {
t.Fatal(err)
}
if proxyURL == nil {
return ""
}
return proxyURL.String()
}
func TestTelegramCommandFiltering(t *testing.T) {
if telegramIsCommandText("今天看什么") {
t.Fatal("plain chat message should not be treated as command")