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 // WsMeta let you receive url & headers to open web socket connection
func (c *MgClient) WsMeta(events []string) (string, http.Header, error) { 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[:], ",")) var url string
headers := http.Header{}
headers.Add("x-bot-token", c.Token)
if url == "" {
err := errors.New("empty WS URL")
return url, headers, err
}
if len(events) < 1 { if len(events) < 1 {
err := errors.New("events list must not be empty") 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 return url, headers, nil
} }

View File

@ -1,10 +1,12 @@
package v1 package v1
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"strings"
"testing" "testing"
"github.com/joho/godotenv" "github.com/joho/godotenv"
@ -314,3 +316,19 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
t.Logf("%v", d) 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])
}