-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
152 lines (131 loc) · 2.86 KB
/
handler_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
package websocket
import (
"crypto/tls"
"crypto/x509"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/gorilla/websocket"
)
type ExampleMessage struct {
Count int `json:"count"`
}
func TestHTTPEcho(t *testing.T) {
echo := func(c *Channel, op OpCode, data []byte) {
// echo
c.Send(op, data)
}
wh, err := NewHandler(echo, 1, 1)
if err != nil {
panic(err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wh.UpgradeHandler(w, r)
}))
defer server.Close()
url := "ws" + strings.TrimPrefix(server.URL, "http")
c, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
panic(err)
}
defer c.Close()
for i := 0; i < 3; i++ {
err = c.WriteJSON(&ExampleMessage{
Count: i,
})
if err != nil {
panic(err)
}
var resp ExampleMessage
err = c.ReadJSON(&resp)
if err != nil {
panic(err)
}
if resp.Count != i {
t.Errorf("should have recieved echo response of: count = %d", i)
}
}
}
func TestHTTPSEcho(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
echo := func(c *Channel, op OpCode, data []byte) {
c.SetOnClose(func() {
wg.Done()
})
// echo
c.Send(op, data)
}
wh, err := NewHandler(echo, 1, 1)
if err != nil {
panic(err)
}
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wh.UpgradeHandler(w, r)
}))
defer server.Close()
dialer := &websocket.Dialer{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
HandshakeTimeout: 30 * time.Second,
}
dialer.TLSClientConfig = &tls.Config{RootCAs: rootCAs(t, server)}
url := "wss" + strings.TrimPrefix(server.URL, "https")
c, _, err := dialer.Dial(url, nil)
if err != nil {
panic(err)
}
for i := 0; i < 3; i++ {
err = c.WriteJSON(&ExampleMessage{
Count: i,
})
if err != nil {
c.Close()
panic(err)
}
var resp ExampleMessage
err = c.ReadJSON(&resp)
if err != nil {
c.Close()
panic(err)
}
if resp.Count != i {
t.Errorf("should have recieved echo response of: count = %d", i)
}
}
c.Close()
timedOut := waitTimeout(&wg, time.Second)
if timedOut {
t.Errorf("timed out waiting for OnClose to be called")
}
}
func rootCAs(t *testing.T, s *httptest.Server) *x509.CertPool {
certs := x509.NewCertPool()
for _, c := range s.TLS.Certificates {
roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
if err != nil {
t.Fatalf("error parsing server's root cert: %v", err)
}
for _, root := range roots {
certs.AddCert(root)
}
}
return certs
}
// waitGroupTimeout waits for the waitgroup for the specified max timeout.
func waitGroupTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}