-
Notifications
You must be signed in to change notification settings - Fork 94
/
client_test.go
420 lines (383 loc) · 12.1 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package gws
import (
"crypto/tls"
"io"
"net"
"net/http"
"testing"
"time"
"github.com/lxzan/gws/internal"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
NewClient(new(BuiltinEventHandler), nil)
{
var option = &ClientOption{
Addr: "ws://127.0.0.1",
}
NewClient(new(BuiltinEventHandler), option)
}
{
var option = &ClientOption{
Addr: "wss://127.0.0.1",
}
NewClient(new(BuiltinEventHandler), option)
}
{
var option = &ClientOption{
Addr: "unix:///",
}
NewClient(new(BuiltinEventHandler), option)
}
{
var option = &ClientOption{
Addr: "tls://127.0.0.1",
}
NewClient(new(BuiltinEventHandler), option)
}
}
func TestNewClientFromConn(t *testing.T) {
var as = assert.New(t)
t.Run("ok", func(t *testing.T) {
server := NewServer(BuiltinEventHandler{}, nil)
addr := ":" + nextPort()
go server.Run(addr)
time.Sleep(100 * time.Millisecond)
conn, err := net.Dial("tcp", "localhost"+addr)
if err != nil {
as.NoError(err)
return
}
_, _, err = NewClientFromConn(BuiltinEventHandler{}, nil, conn)
as.NoError(err)
})
t.Run("fail", func(t *testing.T) {
server := NewServer(BuiltinEventHandler{}, nil)
addr := ":" + nextPort()
go server.Run(addr)
time.Sleep(100 * time.Millisecond)
conn, _ := net.Pipe()
_, _, err := NewClientFromConn(BuiltinEventHandler{}, &ClientOption{}, conn)
as.Error(err)
})
}
func TestClientHandshake(t *testing.T) {
var as = assert.New(t)
option := &ClientOption{
RequestHeader: http.Header{},
PermessageDeflate: PermessageDeflate{Enabled: true},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
eventHandler: new(BuiltinEventHandler),
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
if _, _, err := d.handshake(); err != nil {
as.NoError(err)
return
}
}
func TestClientHandshakeFail(t *testing.T) {
var as = assert.New(t)
t.Run("", func(t *testing.T) {
option := &ClientOption{
RequestHeader: http.Header{},
PermessageDeflate: PermessageDeflate{Enabled: true},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 400 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.Error(err)
})
t.Run("", func(t *testing.T) {
option := &ClientOption{
PermessageDeflate: PermessageDeflate{Enabled: true},
RequestHeader: http.Header{},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upg: rade\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.Error(err)
})
t.Run("", func(t *testing.T) {
option := &ClientOption{
PermessageDeflate: PermessageDeflate{Enabled: true},
RequestHeader: http.Header{},
}
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: _ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.Error(err)
})
t.Run("", func(t *testing.T) {
option := &ClientOption{
PermessageDeflate: PermessageDeflate{Enabled: true},
RequestHeader: http.Header{},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket1\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.Error(err)
})
t.Run("", func(t *testing.T) {
option := &ClientOption{
PermessageDeflate: PermessageDeflate{Enabled: true},
RequestHeader: http.Header{},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket1\r\nConnection: Upgrade1\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.Error(err)
})
t.Run("deflate", func(t *testing.T) {
option := &ClientOption{
PermessageDeflate: PermessageDeflate{Enabled: true},
RequestHeader: http.Header{},
}
option.RequestHeader.Set(internal.SecWebSocketKey.Key, "1fTfP/qALD+eAWcU80P0bg==")
option = initClientOption(option)
srv, cli := net.Pipe()
var d = &connector{
option: option,
conn: cli,
secWebsocketKey: "1fTfP/qALD+eAWcU80P0bg==",
eventHandler: new(BuiltinEventHandler),
}
go func() {
var text = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Extensions: permessage-deflate\r\nSec-WebSocket-Accept: ygR8UkmG67DM75dkgZzwplwlEEo=\r\n\r\n"
for {
var buf = make([]byte, 1024)
srv.Read(buf)
srv.Write([]byte(text))
}
}()
_, _, err := d.handshake()
as.NoError(err)
})
}
var rsaCertPEM = []byte(`-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUJeohtgk8nnt8ofratXJg7kUJsI4wDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDEyMDcwODIyNThaFw0zMDEy
MDUwODIyNThaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCy+ZrIvwwiZv4bPmvKx/637ltZLwfgh3ouiEaTchGu
IQltthkqINHxFBqqJg44TUGHWthlrq6moQuKnWNjIsEc6wSD1df43NWBLgdxbPP0
x4tAH9pIJU7TQqbznjDBhzRbUjVXBIcn7bNknY2+5t784pPF9H1v7h8GqTWpNH9l
cz/v+snoqm9HC+qlsFLa4A3X9l5v05F1uoBfUALlP6bWyjHAfctpiJkoB9Yw1TJa
gpq7E50kfttwfKNkkAZIbib10HugkMoQJAs2EsGkje98druIl8IXmuvBIF6nZHuM
lt3UIZjS9RwPPLXhRHt1P0mR7BoBcOjiHgtSEs7Wk+j7AgMBAAGjUzBRMB0GA1Ud
DgQWBBQdheJv73XSOh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TestNewClientWSS(t *testing.T) {
var as = assert.New(t)
addr := "127.0.0.1:" + nextPort()
srv := NewServer(new(BuiltinEventHandler), nil)
certs, _ := tls.X509KeyPair(rsaCertPEM, rsaKeyPEM)
listener, err := tls.Listen("tcp", addr, &tls.Config{Certificates: []tls.Certificate{certs}})
if err != nil {
as.NoError(err)
return
}
go func() {
if err := srv.RunListener(listener); err != nil {
as.NoError(err)
return
}
}()
t.Run("", func(t *testing.T) {
opts := &ClientOption{
Addr: "wss://" + addr,
TlsConfig: &tls.Config{InsecureSkipVerify: true},
}
_, resp, err := NewClient(&BuiltinEventHandler{}, opts)
as.NoError(err)
as.Equal(opts.TlsConfig.ServerName, resp.Request.URL.Hostname())
})
t.Run("", func(t *testing.T) {
opts := &ClientOption{
Addr: "wss://" + addr,
}
_, _, err = NewClient(&BuiltinEventHandler{}, opts)
as.Error(err)
})
t.Run("", func(t *testing.T) {
_, _, err = NewClient(&BuiltinEventHandler{}, &ClientOption{
Addr: "wss://" + addr,
TlsConfig: &tls.Config{InsecureSkipVerify: true},
NewDialer: func() (Dialer, error) {
return nil, io.EOF
},
})
as.Error(err)
})
t.Run("", func(t *testing.T) {
_, _, err = NewClient(&BuiltinEventHandler{}, &ClientOption{
Addr: "wss://" + addr + "/%x",
TlsConfig: &tls.Config{InsecureSkipVerify: true},
NewDialer: func() (Dialer, error) {
return nil, io.EOF
},
})
as.Error(err)
})
}
func TestNewClient_WriteRequest(t *testing.T) {
t.Run("", func(t *testing.T) {
srv, conn := net.Pipe()
go func() {
for {
var p = make([]byte, 1024)
srv.Read(p)
time.Sleep(time.Second)
}
}()
c := connector{conn: conn, option: &ClientOption{
Addr: "ws://127.0.0.1/a=%",
HandshakeTimeout: 100 * time.Millisecond,
}}
_, _, err := c.request()
assert.Error(t, err)
})
t.Run("", func(t *testing.T) {
_, conn := net.Pipe()
c := connector{conn: conn, option: &ClientOption{
Addr: "ws://127.0.0.1:8080/",
HandshakeTimeout: 100 * time.Millisecond,
}}
_, _, err := c.request()
assert.Error(t, err)
})
t.Run("", func(t *testing.T) {
_, conn := net.Pipe()
c := connector{
conn: conn,
option: &ClientOption{Addr: "ws://127.0.0.1/a=%"},
}
_ = conn.Close()
_, _, err := c.handshake()
assert.Error(t, err)
})
}