-
Notifications
You must be signed in to change notification settings - Fork 93
/
conn_test.go
156 lines (140 loc) · 3.94 KB
/
conn_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package gws
import (
"bytes"
"errors"
"net"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/lxzan/gws/internal"
"github.com/stretchr/testify/assert"
)
type webSocketMocker struct {
sync.Mutex
onMessage func(socket *Conn, message *Message)
onPing func(socket *Conn, payload []byte)
onPong func(socket *Conn, payload []byte)
onClose func(socket *Conn, err error)
onOpen func(socket *Conn)
}
func (c *webSocketMocker) reset(socket *Conn, reader *bytes.Buffer, writer *bytes.Buffer) {
reader.Reset()
writer.Reset()
socket.br.Reset(reader)
atomic.StoreUint32(&socket.closed, 0)
}
func (c *webSocketMocker) OnOpen(socket *Conn) {
if c.onOpen != nil {
c.onOpen(socket)
}
}
func (c *webSocketMocker) OnClose(socket *Conn, err error) {
if c.onClose != nil {
c.onClose(socket, err)
}
}
func (c *webSocketMocker) OnPing(socket *Conn, payload []byte) {
if c.onPing != nil {
c.onPing(socket, payload)
}
}
func (c *webSocketMocker) OnPong(socket *Conn, payload []byte) {
if c.onPong != nil {
c.onPong(socket, payload)
}
}
func (c *webSocketMocker) OnMessage(socket *Conn, message *Message) {
if c.onMessage != nil {
c.onMessage(socket, message)
}
}
func TestOthers(t *testing.T) {
conn, _ := net.Pipe()
upgrader := NewUpgrader(new(BuiltinEventHandler), nil)
socket := &Conn{
conn: conn,
handler: new(webSocketMocker),
config: upgrader.option.getConfig(),
}
socket.SetDeadline(time.Time{})
socket.SetReadDeadline(time.Time{})
socket.SetWriteDeadline(time.Time{})
socket.LocalAddr()
socket.NetConn()
socket.RemoteAddr()
var as = assert.New(t)
var fh = frameHeader{}
fh.SetMask()
var maskKey [4]byte
copy(maskKey[:4], internal.AlphabetNumeric.Generate(4))
fh.SetMaskKey(10, maskKey)
as.Equal(true, fh.GetMask())
as.Equal(string(maskKey[:4]), string(fh.GetMaskKey()))
return
}
func TestConn_Close(t *testing.T) {
conn, _ := net.Pipe()
var options = initServerOption(nil)
var socket = &Conn{conn: conn, config: options.getConfig()}
_ = conn.Close()
assert.Error(t, socket.SetDeadline(time.Time{}))
assert.Error(t, socket.SetReadDeadline(time.Time{}))
assert.Error(t, socket.SetWriteDeadline(time.Time{}))
}
func TestConn_SubProtocol(t *testing.T) {
conn := new(Conn)
conn.SubProtocol()
}
func TestConn_EmitClose(t *testing.T) {
t.Run("", func(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
var serverOption = &ServerOption{CheckUtf8Enabled: true}
var clientOption = &ClientOption{}
var wg = &sync.WaitGroup{}
wg.Add(1)
clientHandler.onClose = func(socket *Conn, err error) {
if err.(*CloseError).Code == internal.CloseProtocolError.Uint16() {
wg.Done()
}
}
server, client := newPeer(serverHandler, serverOption, clientHandler, clientOption)
go client.ReadLoop()
server.emitClose(bytes.NewBuffer(internal.StatusCode(500).Bytes()))
wg.Wait()
})
t.Run("", func(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
var serverOption = &ServerOption{CheckUtf8Enabled: true}
var clientOption = &ClientOption{}
var wg = &sync.WaitGroup{}
wg.Add(1)
clientHandler.onClose = func(socket *Conn, err error) {
if err.(*CloseError).Code == 4000 {
wg.Done()
}
}
server, client := newPeer(serverHandler, serverOption, clientHandler, clientOption)
go client.ReadLoop()
server.emitClose(bytes.NewBuffer(internal.StatusCode(4000).Bytes()))
wg.Wait()
})
}
func TestConn_EmitError(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
var serverOption = &ServerOption{CheckUtf8Enabled: true}
var clientOption = &ClientOption{}
var wg = &sync.WaitGroup{}
wg.Add(1)
clientHandler.onClose = func(socket *Conn, err error) {
wg.Done()
}
server, client := newPeer(serverHandler, serverOption, clientHandler, clientOption)
go client.ReadLoop()
err := errors.New(string(internal.AlphabetNumeric.Generate(500)))
server.emitError(false, err)
wg.Wait()
}