-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
142 lines (120 loc) · 2.54 KB
/
client_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
133
134
135
136
137
138
139
140
141
142
package socketify_test
import (
"fmt"
"github.com/aliforever/go-socketify"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewClient(t *testing.T) {
type args struct {
address string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Successful",
args: args{
address: "ws://127.0.0.1:8080/",
},
wantErr: false,
},
}
go runServer()
// time.Sleep(time.Second * 5)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := make(chan bool)
got, _ := socketify.NewClient(tt.args.address)
got.SetRawHandler(func(message []byte) {
fmt.Println("received", string(message), "and closing...")
got.Close(200, "close")
})
var receivedErr error
got.SetOnClose(func(err error) {
fmt.Println("received error", err)
receivedErr = err
ch <- true
})
<-ch
assert.ErrorContains(t, receivedErr, "close")
})
}
}
func TestNewClientWrite(t *testing.T) {
type args struct {
address string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Successful",
args: args{
address: "ws://127.0.0.1:8080/",
},
wantErr: false,
},
}
go runServer()
// time.Sleep(time.Second * 5)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := socketify.NewClient(tt.args.address)
if err != nil {
panic(err)
}
var wait = make(chan bool)
var receivedErr error
got.SetRawHandler(func(message []byte) {
if string(message) == "Hello" {
fmt.Println("received", string(message), "and closing...")
got.Close(200, "bye")
} else {
fmt.Println("received", string(message))
}
})
got.SetOnClose(func(err error) {
receivedErr = err
wait <- true
})
go got.WriteText("Hello")
fmt.Println("waiting")
<-wait
assert.ErrorContains(t, receivedErr, "bye")
})
}
}
func runServer() {
s := socketify.NewServer(socketify.ServerOptions().SetAddress(":8080").SetEndpoint("/"))
go func() {
err := s.Listen()
if err != nil {
panic(err)
}
}()
for upgradeRequest := range s.UpgradeRequests() {
client, err := upgradeRequest.Upgrade()
if err != nil {
panic(err)
}
client.HandleRawUpdate(func(message []byte) {
fmt.Println(string(message))
if string(message) == "Hello" {
client.WriteText("Hello")
}
})
go func(connection *socketify.Connection) {
connection.WriteUpdate("client_id", connection.ID())
err := connection.ProcessUpdates()
if err != nil {
fmt.Println(err)
return
}
}(client)
}
}