-
Notifications
You must be signed in to change notification settings - Fork 87
/
websocket_test.go
64 lines (53 loc) · 1.38 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
package turnpike
import (
"fmt"
"io"
"net"
"net/http"
"testing"
)
func newTestWebsocketServer(t *testing.T) (int, Router, io.Closer) {
r := NewDefaultRouter()
r.RegisterRealm(testRealm, Realm{})
s := newWebsocketServer(r)
server := &http.Server{
Handler: s,
}
var addr net.TCPAddr
l, err := net.ListenTCP("tcp", &addr)
if err != nil {
t.Fatal(err)
}
go server.Serve(l)
return l.Addr().(*net.TCPAddr).Port, r, l
}
func TestWSHandshakeJSON(t *testing.T) {
port, r, closer := newTestWebsocketServer(t)
defer closer.Close()
client, err := NewWebsocketPeer(JSON, fmt.Sprintf("ws://localhost:%d/", port), nil, nil, nil)
if err != nil {
t.Fatal(err)
}
client.Send(&Hello{Realm: testRealm})
go r.Accept(client)
if msg, ok := <-client.Receive(); !ok {
t.Fatal("Receive buffer closed")
} else if _, ok := msg.(*Welcome); !ok {
t.Errorf("Message not Welcome message: %T, %+v", msg, msg)
}
}
func TestWSHandshakeMsgpack(t *testing.T) {
port, r, closer := newTestWebsocketServer(t)
defer closer.Close()
client, err := NewWebsocketPeer(MSGPACK, fmt.Sprintf("ws://localhost:%d/", port), nil, nil, nil)
if err != nil {
t.Fatal(err)
}
client.Send(&Hello{Realm: testRealm})
go r.Accept(client)
if msg, ok := <-client.Receive(); !ok {
t.Fatal("Receive buffer closed")
} else if _, ok := msg.(*Welcome); !ok {
t.Errorf("Message not Welcome message: %T, %+v", msg, msg)
}
}