Stop returning useless http.Handler from ListenForWebhook.

bot-api-6.1
Syfaro 2016-01-03 23:05:36 -06:00
parent 08d7c01637
commit 80e30d7c19
3 changed files with 5 additions and 23 deletions

View File

@ -85,7 +85,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
updates, _ := bot.ListenForWebhook("/" + bot.Token) updates := bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil) go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
for update := range updates { for update := range updates {

8
bot.go
View File

@ -452,10 +452,10 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (<-chan Update, error) {
} }
// ListenForWebhook registers a http handler for a webhook. // ListenForWebhook registers a http handler for a webhook.
func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler) { func (bot *BotAPI) ListenForWebhook(pattern string) <-chan Update {
updatesChan := make(chan Update, 100) updatesChan := make(chan Update, 100)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body) bytes, _ := ioutil.ReadAll(r.Body)
var update Update var update Update
@ -464,9 +464,7 @@ func (bot *BotAPI) ListenForWebhook(pattern string) (<-chan Update, http.Handler
updatesChan <- update updatesChan <- update
}) })
http.HandleFunc(pattern, handler) return updatesChan
return updatesChan, handler
} }
// AnswerInlineQuery sends a response to an inline query. // AnswerInlineQuery sends a response to an inline query.

View File

@ -5,9 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/http/httptest"
"os" "os"
"strings"
"testing" "testing"
) )
@ -351,20 +349,6 @@ func TestGetUserProfilePhotos(t *testing.T) {
} }
} }
func TestListenForWebhook(t *testing.T) {
bot, _ := getBot(t)
_, handler := bot.ListenForWebhook("/")
req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
}
func TestSetWebhookWithCert(t *testing.T) { func TestSetWebhookWithCert(t *testing.T) {
bot, _ := getBot(t) bot, _ := getBot(t)
@ -445,7 +429,7 @@ func ExampleNewWebhook() {
log.Fatal(err) log.Fatal(err)
} }
updates, _ := bot.ListenForWebhook("/" + bot.Token) updates := bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil) go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
for update := range updates { for update := range updates {