Merge pull request #6 from gwinn/master

minor improvements
This commit is contained in:
Alex Lushpai 2018-09-01 00:42:23 +03:00 committed by GitHub
commit d9bdaa949d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View File

@ -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
}

View File

@ -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])
}