Skip to content

Commit 53d566b

Browse files
authored
Merge pull request #375 from Eivel/develop
Handle error in NewWebhook and NewWebhookWithCert
2 parents 0a6e349 + 69a8270 commit 53d566b

File tree

3 files changed

+69
-12
lines changed

3 files changed

+69
-12
lines changed

bot_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,13 @@ func TestSetWebhookWithCert(t *testing.T) {
550550

551551
bot.Request(DeleteWebhookConfig{})
552552

553-
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
554-
_, err := bot.Request(wh)
553+
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
554+
555+
if err != nil {
556+
t.Error(err)
557+
}
558+
_, err = bot.Request(wh)
559+
555560
if err != nil {
556561
t.Error(err)
557562
}
@@ -572,8 +577,14 @@ func TestSetWebhookWithoutCert(t *testing.T) {
572577

573578
bot.Request(DeleteWebhookConfig{})
574579

575-
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
576-
_, err := bot.Request(wh)
580+
wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
581+
582+
if err != nil {
583+
t.Error(err)
584+
}
585+
586+
_, err = bot.Request(wh)
587+
577588
if err != nil {
578589
t.Error(err)
579590
}
@@ -704,7 +715,14 @@ func ExampleNewWebhook() {
704715

705716
log.Printf("Authorized on account %s", bot.Self.UserName)
706717

707-
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
718+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
719+
720+
if err != nil {
721+
panic(err)
722+
}
723+
724+
_, err = bot.Request(wh)
725+
708726
if err != nil {
709727
panic(err)
710728
}
@@ -737,7 +755,13 @@ func ExampleWebhookHandler() {
737755

738756
log.Printf("Authorized on account %s", bot.Self.UserName)
739757

740-
_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
758+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
759+
760+
if err != nil {
761+
panic(err)
762+
}
763+
764+
_, err = bot.Request(wh)
741765
if err != nil {
742766
panic(err)
743767
}

helpers.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,33 @@ func NewUpdate(offset int) UpdateConfig {
300300
// NewWebhook creates a new webhook.
301301
//
302302
// link is the url parsable link you wish to get the updates.
303-
func NewWebhook(link string) WebhookConfig {
304-
u, _ := url.Parse(link)
303+
func NewWebhook(link string) (WebhookConfig, error) {
304+
u, err := url.Parse(link)
305+
306+
if err != nil {
307+
return WebhookConfig{}, err
308+
}
305309

306310
return WebhookConfig{
307311
URL: u,
308-
}
312+
}, nil
309313
}
310314

311315
// NewWebhookWithCert creates a new webhook with a certificate.
312316
//
313317
// link is the url you wish to get webhooks,
314318
// file contains a string to a file, FileReader, or FileBytes.
315-
func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
316-
u, _ := url.Parse(link)
319+
func NewWebhookWithCert(link string, file interface{}) (WebhookConfig, error) {
320+
u, err := url.Parse(link)
321+
322+
if err != nil {
323+
return WebhookConfig{}, err
324+
}
317325

318326
return WebhookConfig{
319327
URL: u,
320328
Certificate: file,
321-
}
329+
}, nil
322330
}
323331

324332
// NewInlineQueryResultArticle creates a new inline query article.

helpers_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ import (
44
"testing"
55
)
66

7+
func TestNewWebhook(t *testing.T) {
8+
result, err := NewWebhook("https://example.com/token")
9+
10+
if err != nil ||
11+
result.URL.String() != "https://example.com/token" ||
12+
result.Certificate != interface{}(nil) ||
13+
result.MaxConnections != 0 ||
14+
len(result.AllowedUpdates) != 0 {
15+
t.Fail()
16+
}
17+
}
18+
19+
func TestNewWebhookWithCert(t *testing.T) {
20+
exampleFile := File{FileID: "123"}
21+
result, err := NewWebhookWithCert("https://example.com/token", exampleFile)
22+
23+
if err != nil ||
24+
result.URL.String() != "https://example.com/token" ||
25+
result.Certificate != exampleFile ||
26+
result.MaxConnections != 0 ||
27+
len(result.AllowedUpdates) != 0 {
28+
t.Fail()
29+
}
30+
}
31+
732
func TestNewInlineQueryResultArticle(t *testing.T) {
833
result := NewInlineQueryResultArticle("id", "title", "message")
934

0 commit comments

Comments
 (0)