Files
miti99bot/internal/telegram/webhook_test.go
T
tiennm99 341a0b7a1d fix(telegram): clear webhook via Bot API GET to avoid empty-body POST
go-telegram's DeleteWebhook posts an empty multipart form (DropPendingUpdates
is omitempty); some networks answer that bodyless POST with an empty response,
failing to parse and leaving the webhook active so getUpdates 409s forever.
Replace it with a plain GET /deleteWebhook, which sends no body and parses the
JSON reliably. Keeps pending updates for a lossless cutover.
2026-06-28 22:41:49 +07:00

52 lines
1.6 KiB
Go

package telegram
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestDeleteWebhookAt_OK(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %s, want GET", r.Method)
}
if !strings.HasSuffix(r.URL.Path, "/deleteWebhook") {
t.Errorf("path = %s, want suffix /deleteWebhook", r.URL.Path)
}
_, _ = w.Write([]byte(`{"ok":true,"result":true}`))
}))
defer srv.Close()
if err := deleteWebhookAt(context.Background(), srv.URL, "token"); err != nil {
t.Fatalf("deleteWebhookAt: %v", err)
}
}
func TestDeleteWebhookAt_EmptyBody(t *testing.T) {
// Reproduces the failing environment: 200 with an empty body. Must surface
// as an error so the caller retries / warns rather than assuming success.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK) // no body
}))
defer srv.Close()
if err := deleteWebhookAt(context.Background(), srv.URL, "token"); err == nil {
t.Fatal("expected error on empty body, got nil")
}
}
func TestDeleteWebhookAt_APIRejected(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"ok":false,"description":"Unauthorized"}`))
}))
defer srv.Close()
err := deleteWebhookAt(context.Background(), srv.URL, "token")
if err == nil || !strings.Contains(err.Error(), "Unauthorized") {
t.Fatalf("want rejection error mentioning Unauthorized, got %v", err)
}
}