-
Notifications
You must be signed in to change notification settings - Fork 71
/
websocket_test.go
69 lines (56 loc) · 1.44 KB
/
websocket_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
package tunnel_test
import (
"fmt"
"net/http"
"reflect"
"testing"
"github.com/koding/tunnel/tunneltest"
)
func testWebsocket(name string, n int, t *testing.T, tt *tunneltest.TunnelTest) {
conn, err := websocketDial(tt, "http")
if err != nil {
t.Fatalf("Dial()=%s", err)
}
defer conn.Close()
for i := 0; i < n; i++ {
want := &EchoMessage{
Value: fmt.Sprintf("message #%d", i),
Close: i == (n - 1),
}
err := conn.WriteJSON(want)
if err != nil {
t.Errorf("(test %s) %d: failed sending %q: %s", name, i, want, err)
continue
}
got := &EchoMessage{}
err = conn.ReadJSON(got)
if err != nil {
t.Errorf("(test %s) %d: failed reading: %s", name, i, err)
continue
}
if !reflect.DeepEqual(got, want) {
t.Errorf("(test %s) %d: got %+v, want %+v", name, i, got, want)
}
}
}
func testHandler(t *testing.T, fn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := fn(w, r); err != nil {
t.Errorf("handler func error: %s", err)
}
}
}
func TestWebsocket(t *testing.T) {
tt, err := tunneltest.Serve(singleHTTP(testHandler(t, handlerEchoWS(nil))))
if err != nil {
t.Fatal(err)
}
testWebsocket("handlerEchoWS", 100, t, tt)
}
func TestLatencyWebsocket(t *testing.T) {
tt, err := tunneltest.Serve(singleHTTP(testHandler(t, handlerEchoWS(sleep))))
if err != nil {
t.Fatal(err)
}
testWebsocket("handlerLatencyEchoWS", 20, t, tt)
}