-
Notifications
You must be signed in to change notification settings - Fork 16
/
philote_test.go
58 lines (47 loc) · 1.4 KB
/
philote_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import(
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/websocket"
)
func TestPhilotesExchangingMessages(t *testing.T) {
h := NewHive()
if len(h.Philotes) != 0 {
t.Error("new Hive shouldn't have registered philotes")
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"read": []string{"test-channel"},
"write": []string{"test-channel"},
})
tokenString, err := token.SignedString(Config.jwtSecret); if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(http.HandlerFunc(h.ServeNewConnection))
header := map[string][]string{
"Authorization": []string{"Bearer " + tokenString},
}
u, _ := url.Parse(server.URL)
u.Scheme = "ws"
conn1, _, err := websocket.DefaultDialer.Dial(u.String(), header); if err != nil {
t.Error(err)
}
conn2, _, err := websocket.DefaultDialer.Dial(u.String(), header); if err != nil {
t.Error(err)
}
if len(h.Philotes) != 2 {
t.Error("Both philotes should be connected and registered")
}
originalMessage := &Message{Data: "yo!", Channel: "test-channel"}
go func() { time.Sleep(time.Second); conn1.WriteJSON(originalMessage) }()
receivedMessage := &Message{}
err = conn2.ReadJSON(receivedMessage); if err != nil {
t.Error(err)
}
if receivedMessage.Data != "yo!" {
t.Error("incorrect message data")
}
}