Handle error in NewWebhook and NewWebhookWithCert
parent
2f7211a708
commit
69a82708c4
36
bot_test.go
36
bot_test.go
|
@ -479,8 +479,13 @@ func TestSetWebhookWithCert(t *testing.T) {
|
||||||
|
|
||||||
bot.Request(RemoveWebhookConfig{})
|
bot.Request(RemoveWebhookConfig{})
|
||||||
|
|
||||||
wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
|
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
|
||||||
_, err := bot.Request(wh)
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
_, err = bot.Request(wh)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -501,8 +506,14 @@ func TestSetWebhookWithoutCert(t *testing.T) {
|
||||||
|
|
||||||
bot.Request(RemoveWebhookConfig{})
|
bot.Request(RemoveWebhookConfig{})
|
||||||
|
|
||||||
wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
|
wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
|
||||||
_, err := bot.Request(wh)
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = bot.Request(wh)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -589,7 +600,14 @@ func ExampleNewWebhook() {
|
||||||
|
|
||||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -622,7 +640,13 @@ func ExampleWebhookHandler() {
|
||||||
|
|
||||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
20
helpers.go
20
helpers.go
|
@ -441,25 +441,33 @@ func NewUpdate(offset int) UpdateConfig {
|
||||||
// NewWebhook creates a new webhook.
|
// NewWebhook creates a new webhook.
|
||||||
//
|
//
|
||||||
// link is the url parsable link you wish to get the updates.
|
// link is the url parsable link you wish to get the updates.
|
||||||
func NewWebhook(link string) WebhookConfig {
|
func NewWebhook(link string) (WebhookConfig, error) {
|
||||||
u, _ := url.Parse(link)
|
u, err := url.Parse(link)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return WebhookConfig{}, err
|
||||||
|
}
|
||||||
|
|
||||||
return WebhookConfig{
|
return WebhookConfig{
|
||||||
URL: u,
|
URL: u,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWebhookWithCert creates a new webhook with a certificate.
|
// NewWebhookWithCert creates a new webhook with a certificate.
|
||||||
//
|
//
|
||||||
// link is the url you wish to get webhooks,
|
// link is the url you wish to get webhooks,
|
||||||
// file contains a string to a file, FileReader, or FileBytes.
|
// file contains a string to a file, FileReader, or FileBytes.
|
||||||
func NewWebhookWithCert(link string, file interface{}) WebhookConfig {
|
func NewWebhookWithCert(link string, file interface{}) (WebhookConfig, error) {
|
||||||
u, _ := url.Parse(link)
|
u, err := url.Parse(link)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return WebhookConfig{}, err
|
||||||
|
}
|
||||||
|
|
||||||
return WebhookConfig{
|
return WebhookConfig{
|
||||||
URL: u,
|
URL: u,
|
||||||
Certificate: file,
|
Certificate: file,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInlineQueryResultArticle creates a new inline query article.
|
// NewInlineQueryResultArticle creates a new inline query article.
|
||||||
|
|
|
@ -4,6 +4,31 @@ import (
|
||||||
"testing"
|
"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) {
|
func TestNewInlineQueryResultArticle(t *testing.T) {
|
||||||
result := NewInlineQueryResultArticle("id", "title", "message")
|
result := NewInlineQueryResultArticle("id", "title", "message")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue