-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstream_test.go
More file actions
340 lines (277 loc) · 8.5 KB
/
Copy pathstream_test.go
File metadata and controls
340 lines (277 loc) · 8.5 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2025 coregx. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package stream_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/coregx/fursy"
"github.com/coregx/fursy/plugins/stream"
"github.com/coregx/stream/sse"
"github.com/coregx/stream/websocket"
)
// Test 1: SSEHub middleware stores hub in context.
func TestSSEHub_Middleware(t *testing.T) {
t.Helper()
hub := sse.NewHub[string]()
defer hub.Close()
router := fursy.New()
router.Use(stream.SSEHub(hub))
router.Handle("GET", "/test", func(c *fursy.Context) error {
retrievedHub, ok := stream.GetSSEHub[string](c)
if !ok {
t.Error("hub not found in context")
}
if retrievedHub != hub {
t.Error("wrong hub retrieved")
}
return c.JSON(200, map[string]string{"status": "ok"})
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}
// Test 2: WebSocketHub middleware stores hub in context.
func TestWebSocketHub_Middleware(t *testing.T) {
t.Helper()
hub := websocket.NewHub()
defer hub.Close()
router := fursy.New()
router.Use(stream.WebSocketHub(hub))
router.Handle("GET", "/test", func(c *fursy.Context) error {
retrievedHub, ok := stream.GetWebSocketHub(c)
if !ok {
t.Error("hub not found in context")
}
if retrievedHub != hub {
t.Error("wrong hub retrieved")
}
return c.JSON(200, map[string]string{"status": "ok"})
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}
// Test 3: GetSSEHub returns false if hub not in context.
func TestGetSSEHub_NotFound(t *testing.T) {
t.Helper()
router := fursy.New()
router.Handle("GET", "/test", func(c *fursy.Context) error {
_, ok := stream.GetSSEHub[string](c)
if ok {
t.Error("expected hub not found, but got ok=true")
}
return c.JSON(200, map[string]string{"status": "ok"})
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}
// Test 4: GetSSEHub returns false if wrong type.
func TestGetSSEHub_WrongType(t *testing.T) {
t.Helper()
hub := sse.NewHub[string]()
defer hub.Close()
router := fursy.New()
router.Use(stream.SSEHub(hub))
router.Handle("GET", "/test", func(c *fursy.Context) error {
// Try to get hub with wrong type (int instead of string).
_, ok := stream.GetSSEHub[int](c)
if ok {
t.Error("expected type mismatch, but got ok=true")
}
return c.JSON(200, map[string]string{"status": "ok"})
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}
// Test 5: GetWebSocketHub returns false if hub not in context.
func TestGetWebSocketHub_NotFound(t *testing.T) {
t.Helper()
router := fursy.New()
router.Handle("GET", "/test", func(c *fursy.Context) error {
_, ok := stream.GetWebSocketHub(c)
if ok {
t.Error("expected hub not found, but got ok=true")
}
return c.JSON(200, map[string]string{"status": "ok"})
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}
// Test 6: SSEHub middleware with multiple handlers.
func TestSSEHub_MultipleHandlers(t *testing.T) {
t.Helper()
hub := sse.NewHub[string]()
defer hub.Close()
router := fursy.New()
router.Use(stream.SSEHub(hub))
// Handler 1: Check hub exists.
router.Handle("GET", "/handler1", func(c *fursy.Context) error {
_, ok := stream.GetSSEHub[string](c)
if !ok {
t.Error("hub not found in handler1")
}
return c.JSON(200, map[string]string{"handler": "1"})
})
// Handler 2: Check hub exists.
router.Handle("GET", "/handler2", func(c *fursy.Context) error {
_, ok := stream.GetSSEHub[string](c)
if !ok {
t.Error("hub not found in handler2")
}
return c.JSON(200, map[string]string{"handler": "2"})
})
// Test handler1.
req1 := httptest.NewRequest("GET", "/handler1", http.NoBody)
w1 := httptest.NewRecorder()
router.ServeHTTP(w1, req1)
if w1.Code != 200 {
t.Errorf("handler1: expected 200, got %d", w1.Code)
}
// Test handler2.
req2 := httptest.NewRequest("GET", "/handler2", http.NoBody)
w2 := httptest.NewRecorder()
router.ServeHTTP(w2, req2)
if w2.Code != 200 {
t.Errorf("handler2: expected 200, got %d", w2.Code)
}
}
// Test 7: WebSocketHub middleware with multiple handlers.
func TestWebSocketHub_MultipleHandlers(t *testing.T) {
t.Helper()
hub := websocket.NewHub()
defer hub.Close()
router := fursy.New()
router.Use(stream.WebSocketHub(hub))
// Handler 1: Check hub exists.
router.Handle("GET", "/handler1", func(c *fursy.Context) error {
_, ok := stream.GetWebSocketHub(c)
if !ok {
t.Error("hub not found in handler1")
}
return c.JSON(200, map[string]string{"handler": "1"})
})
// Handler 2: Check hub exists.
router.Handle("GET", "/handler2", func(c *fursy.Context) error {
_, ok := stream.GetWebSocketHub(c)
if !ok {
t.Error("hub not found in handler2")
}
return c.JSON(200, map[string]string{"handler": "2"})
})
// Test handler1.
req1 := httptest.NewRequest("GET", "/handler1", http.NoBody)
w1 := httptest.NewRecorder()
router.ServeHTTP(w1, req1)
if w1.Code != 200 {
t.Errorf("handler1: expected 200, got %d", w1.Code)
}
// Test handler2.
req2 := httptest.NewRequest("GET", "/handler2", http.NoBody)
w2 := httptest.NewRecorder()
router.ServeHTTP(w2, req2)
if w2.Code != 200 {
t.Errorf("handler2: expected 200, got %d", w2.Code)
}
}
// Test 8: SSEHub with nested route groups.
func TestSSEHub_NestedGroups(t *testing.T) {
t.Helper()
hub := sse.NewHub[string]()
defer hub.Close()
router := fursy.New()
router.Use(stream.SSEHub(hub))
// Group /api.
api := router.Group("/api")
api.Handle("GET", "/events", func(c *fursy.Context) error {
_, ok := stream.GetSSEHub[string](c)
if !ok {
t.Error("hub not found in /api/events")
}
return c.JSON(200, map[string]string{"path": "/api/events"})
})
// Nested group /api/v1.
v1 := api.Group("/v1")
v1.Handle("GET", "/notifications", func(c *fursy.Context) error {
_, ok := stream.GetSSEHub[string](c)
if !ok {
t.Error("hub not found in /api/v1/notifications")
}
return c.JSON(200, map[string]string{"path": "/api/v1/notifications"})
})
// Test /api/events.
req1 := httptest.NewRequest("GET", "/api/events", http.NoBody)
w1 := httptest.NewRecorder()
router.ServeHTTP(w1, req1)
if w1.Code != 200 {
t.Errorf("/api/events: expected 200, got %d", w1.Code)
}
// Test /api/v1/notifications.
req2 := httptest.NewRequest("GET", "/api/v1/notifications", http.NoBody)
w2 := httptest.NewRecorder()
router.ServeHTTP(w2, req2)
if w2.Code != 200 {
t.Errorf("/api/v1/notifications: expected 200, got %d", w2.Code)
}
}
// Test 9: SSEUpgrade works with different HTTP methods.
// Note: SSE technically works with any HTTP method, though GET is conventional.
func TestSSEUpgrade_DifferentMethods(t *testing.T) {
t.Helper()
router := fursy.New()
// SSE endpoint that accepts POST (unconventional but valid).
router.Handle("POST", "/events", func(c *fursy.Context) error {
return stream.SSEUpgrade(c, func(conn *sse.Conn) error {
// Send a test event and close.
return conn.SendData("test")
})
})
req := httptest.NewRequest("POST", "/events", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Should succeed (SSE doesn't restrict HTTP methods).
if w.Code != 200 {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
// Verify SSE content-type header.
if ct := w.Header().Get("Content-Type"); ct != "text/event-stream" {
t.Errorf("expected text/event-stream, got %q", ct)
}
}
// Test 10: WebSocketUpgrade with invalid request (no Upgrade header).
func TestWebSocketUpgrade_InvalidRequest(t *testing.T) {
t.Helper()
router := fursy.New()
router.Handle("GET", "/ws", func(c *fursy.Context) error {
// WebSocket requires Upgrade header.
return stream.WebSocketUpgrade(c, func(_ *websocket.Conn) error {
return nil
}, nil)
})
req := httptest.NewRequest("GET", "/ws", http.NoBody)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Should return error (WebSocket upgrade failed).
if w.Code == 200 {
t.Error("expected error for request without Upgrade header, but got 200")
}
}