-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_test.go
More file actions
116 lines (92 loc) · 3.31 KB
/
websocket_test.go
File metadata and controls
116 lines (92 loc) · 3.31 KB
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
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
api "dappco.re/go/core/api"
)
// ── Stub groups ─────────────────────────────────────────────────────────
// wsStubGroup is a basic RouteGroup for WebSocket tests.
type wsStubGroup struct{}
func (s *wsStubGroup) Name() string { return "wsstub" }
func (s *wsStubGroup) BasePath() string { return "/v1/wsstub" }
func (s *wsStubGroup) RegisterRoutes(rg *gin.RouterGroup) {
rg.GET("/ping", func(c *gin.Context) {
c.JSON(200, api.OK("pong"))
})
}
// wsStubStreamGroup embeds wsStubGroup and implements StreamGroup.
type wsStubStreamGroup struct{ wsStubGroup }
func (s *wsStubStreamGroup) Channels() []string {
return []string{"wsstub.events", "wsstub.updates"}
}
// ── WebSocket endpoint ──────────────────────────────────────────────────
func TestWSEndpoint_Good(t *testing.T) {
gin.SetMode(gin.TestMode)
// Create a WebSocket upgrader that writes "hello" to every connection.
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
wsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
t.Logf("upgrade error: %v", err)
return
}
defer conn.Close()
_ = conn.WriteMessage(websocket.TextMessage, []byte("hello"))
})
e, err := api.New(api.WithWSHandler(wsHandler))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
srv := httptest.NewServer(e.Handler())
defer srv.Close()
// Dial the WebSocket endpoint.
wsURL := "ws" + strings.TrimPrefix(srv.URL, "http") + "/ws"
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
if err != nil {
t.Fatalf("failed to dial WebSocket: %v", err)
}
defer conn.Close()
_, msg, err := conn.ReadMessage()
if err != nil {
t.Fatalf("failed to read message: %v", err)
}
if string(msg) != "hello" {
t.Fatalf("expected message=%q, got %q", "hello", string(msg))
}
}
func TestNoWSHandler_Good(t *testing.T) {
gin.SetMode(gin.TestMode)
// Without WithWSHandler, GET /ws should return 404.
e, _ := api.New()
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/ws", nil)
h.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404 for /ws without handler, got %d", w.Code)
}
}
// ── Channel listing ─────────────────────────────────────────────────────
func TestChannelListing_Good(t *testing.T) {
e, _ := api.New()
// Register a plain RouteGroup (no channels) and a StreamGroup.
e.Register(&wsStubGroup{})
e.Register(&wsStubStreamGroup{})
channels := e.Channels()
if len(channels) != 2 {
t.Fatalf("expected 2 channels, got %d", len(channels))
}
if channels[0] != "wsstub.events" {
t.Fatalf("expected channels[0]=%q, got %q", "wsstub.events", channels[0])
}
if channels[1] != "wsstub.updates" {
t.Fatalf("expected channels[1]=%q, got %q", "wsstub.updates", channels[1])
}
}