46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package clientnotify
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildTGSendRequestTargetsClientChat(t *testing.T) {
|
|
req, err := buildTGSendRequest(context.Background(), "Ваш заказ готов", "bot-token", "555666", "http://bot-api:8081")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
wantURL := "http://bot-api:8081/botbot-token/sendMessage"
|
|
if req.URL.String() != wantURL {
|
|
t.Errorf("URL = %q, want %q", req.URL.String(), wantURL)
|
|
}
|
|
body, _ := io.ReadAll(req.Body)
|
|
if !strings.Contains(string(body), `"chat_id":"555666"`) {
|
|
t.Errorf("body missing chat_id: %s", body)
|
|
}
|
|
}
|
|
|
|
func TestParseTGSendResponseOK(t *testing.T) {
|
|
body := `{"ok":true,"result":{"message_id":42}}`
|
|
id, err := parseTGSendResponse(strings.NewReader(body))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if id != "42" {
|
|
t.Errorf("message id = %q, want 42", id)
|
|
}
|
|
}
|
|
|
|
func TestParseTGSendResponseError(t *testing.T) {
|
|
body := `{"ok":false,"description":"chat not found"}`
|
|
_, err := parseTGSendResponse(strings.NewReader(body))
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if !strings.Contains(err.Error(), "chat not found") {
|
|
t.Errorf("error = %v", err)
|
|
}
|
|
}
|