-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws_test.go
95 lines (80 loc) · 2.77 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
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
package govalin_test
import (
"testing"
"github.com/gorilla/websocket"
"github.com/pkkummermo/govalin"
"github.com/pkkummermo/govalin/internal/govalintesting"
"github.com/stretchr/testify/assert"
)
func TestWebsocketOpen(t *testing.T) {
govalintesting.HTTPTestUtil(func(app *govalin.App) *govalin.App {
app.Ws("/ws", func(wsConfig *govalin.WsConfig) {
wsConfig.OnOpen = func(wsConnection *govalin.WsConnection) {
err := wsConnection.SendText("Hello open")
assert.Nil(t, err, "Should not return error when sending text")
}
})
return app
}, func(http govalintesting.GovalinHTTP) {
ws := http.Websocket("/ws")
defer ws.Close()
_, message, err := ws.ReadMessage()
assert.Nil(t, err, "Should not return error when reading message")
assert.Equal(t, "Hello open", string(message), "Should receive message from server")
})
}
func TestWebsocketOnMessage(t *testing.T) {
govalintesting.HTTPTestUtil(func(app *govalin.App) *govalin.App {
app.Ws("/ws", func(wsConfig *govalin.WsConfig) {
wsConfig.OnMessage = func(wsMessage *govalin.WsMessage) {
err := wsMessage.ReplyText(wsMessage.AsText())
assert.Nil(t, err, "Should not return error when replying text")
}
})
return app
}, func(http govalintesting.GovalinHTTP) {
ws := http.Websocket("/ws")
defer ws.Close()
err := ws.WriteMessage(websocket.TextMessage, []byte("Hello server"))
assert.Nil(t, err, "Should not return error when sending message")
_, message, err := ws.ReadMessage()
assert.Nil(t, err, "Should not return error when reading message")
assert.Equal(t, "Hello server", string(message), "Should receive echo from server")
})
}
func TestWebsocketOnCloseDefaultAbnormal(t *testing.T) {
govalintesting.HTTPTestUtil(func(app *govalin.App) *govalin.App {
app.Ws("/ws", func(wsConfig *govalin.WsConfig) {
wsConfig.OnClose = func(closeCode int, _ string) {
assert.Equal(
t,
websocket.CloseAbnormalClosure,
closeCode, "Should receive 'abnormal' (actually quite normal) closure code",
)
}
})
return app
}, func(http govalintesting.GovalinHTTP) {
ws := http.Websocket("/ws")
ws.Close()
})
}
func TestWebsocketOnCloseNormal(t *testing.T) {
govalintesting.HTTPTestUtil(func(app *govalin.App) *govalin.App {
app.Ws("/ws", func(wsConfig *govalin.WsConfig) {
wsConfig.OnClose = func(closeCode int, _ string) {
assert.Equal(
t,
websocket.CloseNormalClosure,
closeCode, "Should receive 'normal' closure code",
)
}
})
return app
}, func(http govalintesting.GovalinHTTP) {
ws := http.Websocket("/ws")
closeMessage := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Test normal closure")
assert.NoError(t, ws.WriteMessage(websocket.CloseMessage, closeMessage), "Should not return error when closing")
ws.Close()
})
}