-
Notifications
You must be signed in to change notification settings - Fork 19
/
ws_test.go
68 lines (53 loc) · 1.36 KB
/
ws_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
package gongular
import (
"fmt"
"net"
"net/http"
"testing"
"io"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
websocket_go "golang.org/x/net/websocket"
)
type wsTest struct {
Param struct {
UserID int
}
Query struct {
Track bool
Username string
}
}
func (w *wsTest) Before(c *Context) (http.Header, error) {
return nil, nil
}
func (w *wsTest) Handle(conn *websocket.Conn) {
_, msg, err := conn.ReadMessage()
if err != nil {
conn.Close()
}
toSend := fmt.Sprintf("%s:%d:%s:%t", msg, w.Param.UserID, w.Query.Username, w.Query.Track)
conn.WriteMessage(websocket.TextMessage, []byte(toSend))
}
func TestWS_Simple(t *testing.T) {
e := newEngineTest()
e.GetWSRouter().Handle("/ws1/:UserID", &wsTest{})
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
go func() {
http.Serve(listener, e.GetHandler())
}()
url := fmt.Sprintf("ws://%s/ws1/5?Track=true&Username=musti", listener.Addr().String())
origin := fmt.Sprintf("http://%s/", listener.Addr().String())
ws, err := websocket_go.Dial(url, "", origin)
require.NoError(t, err)
_, err = io.WriteString(ws, "selam")
require.NoError(t, err)
var buf = make([]byte, 1024)
n, err := ws.Read(buf)
require.NoError(t, err)
result := string(buf[:n])
listener.Close()
assert.Equal(t, "selam:5:musti:true", result)
}