mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 03:56:38 +08:00
fix(telegram): honor proxy fallbacks for bot api
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user