-
Notifications
You must be signed in to change notification settings - Fork 13
/
client_test.go
78 lines (64 loc) · 1.25 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
package websocket
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func BenchmarkRandKey(b *testing.B) {
var bf []byte
for i := 0; i < b.N; i++ {
bf = makeRandKey(bf[:0])
}
}
func TestDial(t *testing.T) {
text := []byte("Make fasthttp great again")
uri := "http://localhost:9843/"
ln := fasthttputil.NewInmemoryListener()
ws := Server{
Origin: uri,
}
ws.HandleData(func(conn *Conn, isBinary bool, data []byte) {
if !bytes.Equal(data, text) {
panic(fmt.Sprintf("%s <> %s", data, text))
}
})
ws.HandleClose(func(c *Conn, err error) {
if err != nil && err.(Error).Status != StatusGoAway {
t.Fatalf("Expected GoAway, got %s", err.(Error).Status)
}
})
s := fasthttp.Server{
Handler: ws.Upgrade,
}
ch := make(chan struct{}, 1)
go func() {
s.Serve(ln)
ch <- struct{}{}
}()
c, err := ln.Dial()
if err != nil {
t.Fatal(err)
}
conn, err := MakeClient(c, uri)
if err != nil {
t.Fatal(err)
}
_, err = conn.Write(text)
if err != nil {
t.Fatal(err)
}
fr := AcquireFrame()
fr.SetFin()
fr.SetClose()
fr.SetStatus(StatusGoAway)
conn.WriteFrame(fr)
ln.Close()
select {
case <-ch:
case <-time.After(time.Second * 5):
t.Fatal("timeout")
}
}