-
Notifications
You must be signed in to change notification settings - Fork 5
/
conn_test.go
186 lines (160 loc) · 4.2 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package websocket
import (
"context"
"encoding/json"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/stretchr/testify/require"
"net/url"
"strings"
"testing"
"time"
)
const messagePrefix = 2
func TestConn_Ping(t *testing.T) {
ts, _, shutdown := server(t)
defer shutdown()
ctx := context.Background()
u := url.URL{Scheme: "ws", Host: strings.Replace(ts.URL, "http://", "", 1), Path: "/ws"}
c, _, _, err := ws.Dial(ctx, u.String())
require.NoError(t, err)
defer func() {
err := c.Close()
require.NoError(t, err)
}()
m := []byte("ping")
h := ws.Header{
Fin: true,
OpCode: ws.OpPing,
Masked: true,
Length: int64(len(m)),
}
err = ws.WriteHeader(c, h)
require.NoError(t, err)
_, err = c.Write(m)
require.NoError(t, err)
time.Sleep(1 * time.Millisecond)
resp := make([]byte, len(m)+2)
_, err = c.Read(resp)
require.NoError(t, err)
require.Equal(t, m, resp[2:], "response and request must be the same")
}
func TestConn_Pong(t *testing.T) {
ts, _, shutdown := server(t)
defer shutdown()
ctx := context.Background()
u := url.URL{Scheme: "ws", Host: strings.Replace(ts.URL, "http://", "", 1), Path: "/ws"}
c, _, _, err := ws.Dial(ctx, u.String())
require.NoError(t, err)
defer func() {
err := c.Close()
require.NoError(t, err)
}()
m := []byte("pong")
h := ws.Header{
Fin: true,
OpCode: ws.OpPong,
Masked: true,
Length: int64(len(m)),
}
err = ws.WriteHeader(c, h)
require.NoError(t, err)
_, err = c.Write(m)
require.NoError(t, err)
time.Sleep(1 * time.Millisecond)
require.NoError(t, err)
}
func TestConn_Send_bytes(t *testing.T) {
ts, wsServer, shutdown := server(t)
defer shutdown()
msg := []byte{0, 1, 2, 3, 4, 5, 6, 7}
wsServer.OnConnect(func(c *Conn) {
time.Sleep(300 * time.Millisecond)
err := c.Send(msg)
require.NoError(t, err)
})
u := url.URL{Scheme: "ws", Host: strings.Replace(ts.URL, "http://", "", 1), Path: "/ws"}
c, _, _, err := ws.Dial(context.Background(), u.String())
require.NoError(t, err)
err = c.SetDeadline(time.Now().Add(3000 * time.Millisecond))
require.NoError(t, err)
defer func() {
err := c.Close()
require.NoError(t, err)
}()
mes, op, err := wsutil.ReadServerData(c)
require.NoError(t, err)
require.Equal(t, true, op.IsData())
require.Equal(t, msg, mes, "response and request must be the same")
}
func TestConn_Send_struct(t *testing.T) {
ts, wsServer, shutdown := server(t)
defer shutdown()
msg := struct {
Value string `json:"value"`
}{
Value: "test",
}
wsServer.OnConnect(func(c *Conn) {
time.Sleep(300 * time.Millisecond)
err := c.Send(msg)
require.NoError(t, err)
})
u := url.URL{Scheme: "ws", Host: strings.Replace(ts.URL, "http://", "", 1), Path: "/ws"}
c, _, _, err := ws.Dial(context.Background(), u.String())
require.NoError(t, err)
err = c.SetDeadline(time.Now().Add(3000 * time.Millisecond))
require.NoError(t, err)
defer func() {
require.NoError(t, c.Close())
}()
mes, op, err := wsutil.ReadServerData(c)
require.NoError(t, err)
require.Equal(t, true, op.IsData())
b, err := json.Marshal(msg)
require.NoError(t, err)
require.Equal(t, b, mes, "response and request must be the same")
}
func TestConn_Fragment(t *testing.T) {
ts, _, shutdown := server(t)
defer shutdown()
ctx := context.Background()
u := url.URL{Scheme: "ws", Host: strings.Replace(ts.URL, "http://", "", 1), Path: "/ws"}
c, _, _, err := ws.Dial(ctx, u.String())
require.NoError(t, err)
defer func() {
require.NoError(t, c.Close())
}()
m1 := []byte("hello")
err = ws.WriteHeader(c, ws.Header{
Fin: false,
OpCode: ws.OpText,
Masked: true,
Length: int64(len(m1)),
})
require.NoError(t, err)
_, err = c.Write(m1)
require.NoError(t, err)
m2 := []byte(" world")
err = ws.WriteHeader(c, ws.Header{
Fin: true,
OpCode: ws.OpContinuation,
Masked: true,
Length: int64(len(m2)),
})
require.NoError(t, err)
_, err = c.Write(m2)
require.NoError(t, err)
time.Sleep(time.Millisecond * 10)
resp := make([]byte, 128)
n, err := c.Read(resp)
resp = resp[:n]
final := []byte{}
for _, b := range resp {
if b != 0x1 && b != 0x5 && b != 0x6 && b != 0x80 {
final = append(final, b)
}
}
require.NoError(t, err)
require.Equal(t, append(m1, m2...), final, "response and request must be the same")
}