forked from bandabh/phxgoclient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel_test.go
89 lines (73 loc) · 1.84 KB
/
channel_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
package phxgoclient
import "testing"
func setupPhxGo() PheonixGoSocket {
socket := NewPheonixWebsocket("localhost:4000", "/socket", "ws", false)
socket.Listen()
socket.OpenChannel("room:lobby")
socket.JoinChannel("room:lobby", nil)
return socket
}
func TestChannel_Push(t *testing.T) {
socket := setupPhxGo()
channel, _ := socket.GetChannel("room:lobby")
if !channel.CanPush() {
t.Errorf("channel should be able to push")
}
channel.Push("ping", nil)
readEvent := channel.Read()
if readEvent.Payload.Status != "" {
if readEvent.Payload.Status != "ok" {
t.Errorf("failed to push to channel")
}
}
}
func TestChannel_Read(t *testing.T) {
socket := setupPhxGo()
channel, _ := socket.GetChannel("room:lobby")
if !channel.CanPush() {
t.Errorf("channel should be able to push")
}
channel.Push("ping", nil)
readEvent := channel.Read()
if readEvent.Event == "" {
t.Errorf("failed to read!")
}
}
//func TestChannel_Observe(t *testing.T) {
// socket := NewPheonixWebsocket("localhost:4000", "/socket", "ws", false)
// socket.Listen()
//
// socket.OpenChannel("room:lobby")
//
// channel, _ := socket.GetChannel("room:lobby")
// channel.Register(ReplyEvent, func(response MessageResponse) (data interface{}, err error) {
//
// if response.Payload.Status != "ok" {
// t.Errorf("failed to connect properly")
// }
//
// return response, nil
// })
// socket.JoinChannel("room:lobby", nil)
//
// _, err := channel.Observe()
//
// if err != nil {
// t.Errorf(err.Error())
// }
//
//}
func TestChannel_IsOpen(t *testing.T) {
socket := setupPhxGo()
channel, _ := socket.GetChannel("room:lobby")
if !channel.IsOpen() {
t.Errorf("channel should be open")
}
}
func TestChannel_CanPush(t *testing.T) {
socket := setupPhxGo()
channel, _ := socket.GetChannel("room:lobby")
if !channel.CanPush() {
t.Errorf("channel should be able to push")
}
}