-
Notifications
You must be signed in to change notification settings - Fork 19
/
evtwebsocket_test.go
108 lines (105 loc) · 2.03 KB
/
evtwebsocket_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
package evtwebsocket
import (
"testing"
"time"
)
func TestConn_Dial(t *testing.T) {
type args struct {
url string
subprotocol string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"ws-normal",
args{
"ws://echo.websocket.org",
"",
},
false,
},
{
"ws-tls",
args{
"wss://echo.websocket.org",
"",
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Conn{}
if err := c.Dial(tt.args.url, tt.args.subprotocol); (err != nil) != tt.wantErr {
t.Errorf("Conn.Dial() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestConn_Send(t *testing.T) {
type fields struct {
OnMessage func([]byte, *Conn)
OnError func(error)
OnConnected func(*Conn)
MatchMsg func([]byte, []byte) bool
}
type args struct {
url string
}
tests := []struct {
name string
fields fields
args args
}{
{
"regular-send",
fields{
OnConnected: func(con *Conn) {
m := Msg{
Body: []byte("Hello"),
Callback: func(msg []byte, con *Conn) {
if string(msg) != "Hello" {
t.Errorf("Callback() expected = 'Hello', got = '%s'", msg)
}
},
}
if err := con.Send(m); err != nil {
t.Errorf("Conn.Send() error = %v", err)
}
},
OnMessage: func(msg []byte, con *Conn) {
if string(msg) != "Hello" {
t.Errorf("OnMessage() expected = 'Hello', got = '%s'", msg)
}
},
MatchMsg: func(req, resp []byte) bool {
return string(req) == string(resp)
},
OnError: func(err error) {
t.Errorf("Error: %v", err)
},
},
args{
"ws://echo.websocket.org",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Conn{
OnMessage: tt.fields.OnMessage,
OnConnected: tt.fields.OnConnected,
MatchMsg: tt.fields.MatchMsg,
}
err := c.Dial(tt.args.url, "")
if err != nil {
t.Errorf("Conn.Dial() error = %v", err)
}
// Wait for response
time.Sleep(time.Second * 2)
})
}
}