-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector_test.go
132 lines (107 loc) · 3.38 KB
/
connector_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package pts
import (
"bytes"
"net/http"
"testing"
)
func TestConnector(t *testing.T) {
t.Run("Join", func(t *testing.T) {
testStrKey := "foo"
testStrVal := "bar"
testBoolKey := "bool"
clientProperties := map[string]interface{}{
testStrKey: testStrVal,
testBoolKey: true,
}
clientSendFunc := func(message []byte) error {
return nil
}
connector := NewConnector(func(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error {
return nil
}, func(_ *Error) {})
var joinedClient *Client
connector.hook(&Hooks{
OnConnect: func(client *Client) {
joinedClient = client
},
})
connector.Join(clientSendFunc, clientProperties)
if joinedClient == nil {
t.Errorf("OnConnect hook was not called, want OnConnect to be called.")
} else if val, _ := joinedClient.Get(testStrKey); val != testStrVal {
t.Errorf("joinedClient.Get(\"foo\") returns %s, want %s.", val, testStrVal)
return
} else if val, _ := joinedClient.Get(testBoolKey); val != true {
boolStr := "false"
if val.(bool) {
boolStr = "true"
}
t.Errorf("joinedClient.Get(\"foo\") returns %s, want %s.", boolStr, "true")
return
}
})
t.Run("Message", func(t *testing.T) {
testMsg := []byte{1, 2, 3}
clientSendFunc := func(message []byte) error {
return nil
}
connector := NewConnector(func(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error {
return nil
}, func(_ *Error) {})
var joinedClient *Client
var receivedClient *Client
var receivedMessage []byte
connector.hook(&Hooks{
OnConnect: func(client *Client) {
joinedClient = client
},
OnMessage: func(client *Client, msg []byte) {
receivedClient = client
receivedMessage = msg
},
})
connector.Join(clientSendFunc, map[string]interface{}{})
if joinedClient == nil {
t.Errorf("OnConnect hook was not called, want OnConnect to be called.")
}
connector.Message(joinedClient.Id, testMsg)
if receivedClient == nil || receivedMessage == nil {
t.Errorf("OnMessage hook was not called, want OnMessage to be called with client and message.")
} else if joinedClient != receivedClient {
t.Errorf("OnMessage gets called with different client, want same client than on OnConnect call")
return
} else if bytes.Compare(receivedMessage, testMsg) != 0 {
t.Errorf("receivedMessage %v, want %v.", receivedMessage, testMsg)
return
}
})
t.Run("Leave", func(t *testing.T) {
clientSendFunc := func(message []byte) error {
return nil
}
connector := NewConnector(func(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error {
return nil
}, func(_ *Error) {})
var joinedClient *Client
var leftClient *Client
connector.hook(&Hooks{
OnConnect: func(client *Client) {
joinedClient = client
},
OnDisconnect: func(client *Client) {
leftClient = client
},
})
connector.Join(clientSendFunc, map[string]interface{}{})
if joinedClient == nil {
t.Errorf("OnConnect hook was not called, want OnConnect to be called.")
}
connector.Leave(joinedClient.Id)
if leftClient == nil {
t.Errorf("OnDisconnect hook was not called, want OnDisconnect to be called with client and message.")
} else if joinedClient != leftClient {
t.Errorf("OnDisconnect gets called with different client, want same client than on OnConnect call")
return
}
})
}