From 8ccda2e9708eeaf682af94cc97793ff5a6a567ce Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Sat, 1 Sep 2018 00:41:29 +0300 Subject: [PATCH] minor improvements --- v1/client.go | 21 ++++++++++++--------- v1/client_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/v1/client.go b/v1/client.go index d3da606..c3501e5 100644 --- a/v1/client.go +++ b/v1/client.go @@ -662,20 +662,23 @@ func (c *MgClient) CommandDelete(request string) (map[string]interface{}, int, e // WsMeta let you receive url & headers to open web socket connection func (c *MgClient) WsMeta(events []string) (string, http.Header, error) { - url := fmt.Sprintf("%s%s%s%s", c.URL, prefix, "/ws?events=", strings.Join(events[:], ",")) - headers := http.Header{} - headers.Add("x-bot-token", c.Token) - - if url == "" { - err := errors.New("empty WS URL") - return url, headers, err - } + var url string if len(events) < 1 { err := errors.New("events list must not be empty") - return url, headers, err + return url, nil, err } + url = fmt.Sprintf("%s%s%s%s", c.URL, prefix, "/ws?events=", strings.Join(events[:], ",")) + + if url == "" { + err := errors.New("empty WS URL") + return url, nil, err + } + + headers := http.Header{} + headers.Add("x-bot-token", c.Token) + return url, headers, nil } diff --git a/v1/client_test.go b/v1/client_test.go index 7f3ef80..5f7d01c 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -1,10 +1,12 @@ package v1 import ( + "fmt" "log" "net/http" "os" "strconv" + "strings" "testing" "github.com/joho/godotenv" @@ -314,3 +316,19 @@ func TestMgClient_CommandEditDelete(t *testing.T) { assert.NoError(t, err) t.Logf("%v", d) } + +func TestMgClient_WsMeta(t *testing.T) { + c := client() + events := []string{"user_updated", "user_join_chat"} + url, headers, err := c.WsMeta(events) + + if err != nil { + t.Errorf("%v", err) + } + + resUrl := fmt.Sprintf("%s%s%s%s", c.URL, prefix, "/ws?events=", strings.Join(events[:], ",")) + resToken := c.Token + + assert.Equal(t, resUrl, url) + assert.Equal(t, resToken, headers["X-Bot-Token"][0]) +}