all repos — telegram-bot-api @ 53d566ba56fb4b9925de3aeb74726e870f8d2e1d

Golang bindings for the Telegram Bot API

Merge pull request #375 from Eivel/develop

Handle error in NewWebhook and NewWebhookWithCert
Syfaro syfaro@huefox.com
Wed, 10 Mar 2021 22:00:07 -0500
commit

53d566ba56fb4b9925de3aeb74726e870f8d2e1d

parent

0a6e349f52da30c8ccda45fb2b9ec625c8ef63b7

3 files changed, 69 insertions(+), 12 deletions(-)

jump to
M bot_test.gobot_test.go

@@ -550,8 +550,13 @@ time.Sleep(time.Second * 2)

bot.Request(DeleteWebhookConfig{}) - wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem") - _, err := bot.Request(wh) + wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem") + + if err != nil { + t.Error(err) + } + _, err = bot.Request(wh) + if err != nil { t.Error(err) }

@@ -572,8 +577,14 @@ time.Sleep(time.Second * 2)

bot.Request(DeleteWebhookConfig{}) - wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token) - _, err := bot.Request(wh) + wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token) + + if err != nil { + t.Error(err) + } + + _, err = bot.Request(wh) + if err != nil { t.Error(err) }

@@ -704,7 +715,14 @@ bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName) - _, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")) + wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem") + + if err != nil { + panic(err) + } + + _, err = bot.Request(wh) + if err != nil { panic(err) }

@@ -737,7 +755,13 @@ bot.Debug = true

log.Printf("Authorized on account %s", bot.Self.UserName) - _, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")) + wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem") + + if err != nil { + panic(err) + } + + _, err = bot.Request(wh) if err != nil { panic(err) }
M helpers.gohelpers.go

@@ -300,25 +300,33 @@

// NewWebhook creates a new webhook. // // link is the url parsable link you wish to get the updates. -func NewWebhook(link string) WebhookConfig { - u, _ := url.Parse(link) +func NewWebhook(link string) (WebhookConfig, error) { + u, err := url.Parse(link) + + if err != nil { + return WebhookConfig{}, err + } return WebhookConfig{ URL: u, - } + }, nil } // NewWebhookWithCert creates a new webhook with a certificate. // // link is the url you wish to get webhooks, // file contains a string to a file, FileReader, or FileBytes. -func NewWebhookWithCert(link string, file interface{}) WebhookConfig { - u, _ := url.Parse(link) +func NewWebhookWithCert(link string, file interface{}) (WebhookConfig, error) { + u, err := url.Parse(link) + + if err != nil { + return WebhookConfig{}, err + } return WebhookConfig{ URL: u, Certificate: file, - } + }, nil } // NewInlineQueryResultArticle creates a new inline query article.
M helpers_test.gohelpers_test.go

@@ -4,6 +4,31 @@ import (

"testing" ) +func TestNewWebhook(t *testing.T) { + result, err := NewWebhook("https://example.com/token") + + if err != nil || + result.URL.String() != "https://example.com/token" || + result.Certificate != interface{}(nil) || + result.MaxConnections != 0 || + len(result.AllowedUpdates) != 0 { + t.Fail() + } +} + +func TestNewWebhookWithCert(t *testing.T) { + exampleFile := File{FileID: "123"} + result, err := NewWebhookWithCert("https://example.com/token", exampleFile) + + if err != nil || + result.URL.String() != "https://example.com/token" || + result.Certificate != exampleFile || + result.MaxConnections != 0 || + len(result.AllowedUpdates) != 0 { + t.Fail() + } +} + func TestNewInlineQueryResultArticle(t *testing.T) { result := NewInlineQueryResultArticle("id", "title", "message")