Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
239 lines (192 loc) · 6.28 KB
/
Copy pathintegration_test.go
File metadata and controls
239 lines (192 loc) · 6.28 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
// ABOUTME: Integration tests for WebSocket middleware chain.
// ABOUTME: Validates end-to-end middleware execution order and group isolation.
package websocket_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/bingo-project/websocket"
"github.com/bingo-project/websocket/jsonrpc"
"github.com/bingo-project/websocket/middleware"
)
func TestFullMiddlewareChain(t *testing.T) {
// Setup
hub := websocket.NewHub()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go hub.Run(ctx)
router := websocket.NewRouter()
// Add middlewares with tracking
var order []string
router.Use(
func(next websocket.Handler) websocket.Handler {
return func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "recovery")
return middleware.Recovery(next)(c)
}
},
func(next websocket.Handler) websocket.Handler {
return func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "requestid")
return middleware.RequestID(next)(c)
}
},
)
// Public handler
router.Handle("public.test", func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "handler")
return jsonrpc.NewResponse(c.Request.ID, "ok")
})
// Private handler with auth middleware
private := router.Group(middleware.Auth)
private.Handle("private.test", func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "private-handler")
return jsonrpc.NewResponse(c.Request.ID, "ok")
})
// Test public method - should pass through all middlewares
client := &websocket.Client{
ID: "test",
Send: make(chan []byte, 256),
}
c := &websocket.Context{
Context: context.Background(),
Request: &jsonrpc.Request{ID: 1, Method: "public.test"},
Client: client,
Method: "public.test",
StartTime: time.Now(),
}
resp := router.Dispatch(c)
assert.Nil(t, resp.Error)
assert.Equal(t, []string{"recovery", "requestid", "handler"}, order)
// Test private method without auth - should be rejected
order = nil
c.Method = "private.test"
c.Request.Method = "private.test"
resp = router.Dispatch(c)
assert.NotNil(t, resp.Error)
assert.Equal(t, "Unauthorized", resp.Error.Reason)
// Test private method with auth - should pass
order = nil
client.UserID = "user-1"
client.Platform = "web"
client.LoginTime = 1000
resp = router.Dispatch(c)
assert.Nil(t, resp.Error)
assert.Contains(t, order, "private-handler")
}
func TestMiddlewareChain_ExecutionOrder(t *testing.T) {
router := websocket.NewRouter()
var order []string
// Add numbered middlewares to verify order
for i := 1; i <= 3; i++ {
n := i // capture
router.Use(func(next websocket.Handler) websocket.Handler {
return func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "before-"+string(rune('0'+n)))
resp := next(c)
order = append(order, "after-"+string(rune('0'+n)))
return resp
}
})
}
router.Handle("test", func(c *websocket.Context) *jsonrpc.Response {
order = append(order, "handler")
return jsonrpc.NewResponse(c.Request.ID, "ok")
})
c := &websocket.Context{
Context: context.Background(),
Request: &jsonrpc.Request{ID: 1, Method: "test"},
Method: "test",
StartTime: time.Now(),
}
router.Dispatch(c)
// Verify onion execution: before1 -> before2 -> before3 -> handler -> after3 -> after2 -> after1
expected := []string{
"before-1", "before-2", "before-3",
"handler",
"after-3", "after-2", "after-1",
}
assert.Equal(t, expected, order)
}
func TestRateLimitIntegration(t *testing.T) {
router := websocket.NewRouter()
// Create rate limiter store
store := middleware.NewRateLimiterStore()
// Apply rate limit of 1 request per second
router.Use(middleware.RateLimitWithStore(&middleware.RateLimitConfig{
Default: 1,
}, store))
router.Handle("test", func(c *websocket.Context) *jsonrpc.Response {
return jsonrpc.NewResponse(c.Request.ID, "ok")
})
client := &websocket.Client{
ID: "test-client",
Send: make(chan []byte, 256),
}
c := &websocket.Context{
Context: context.Background(),
Request: &jsonrpc.Request{ID: 1, Method: "test"},
Client: client,
Method: "test",
StartTime: time.Now(),
}
// First request should pass
resp := router.Dispatch(c)
assert.Nil(t, resp.Error)
// Second request should pass (burst allows 2)
resp = router.Dispatch(c)
assert.Nil(t, resp.Error)
// Third request should be rate limited
resp = router.Dispatch(c)
assert.NotNil(t, resp.Error)
assert.Equal(t, "TooManyRequests", resp.Error.Reason)
// Clean up
store.Remove(client)
}
func TestGroupMiddlewareIsolation(t *testing.T) {
router := websocket.NewRouter()
var publicMiddlewareCalled, privateMiddlewareCalled bool
// Global middleware
router.Use(func(next websocket.Handler) websocket.Handler {
return func(c *websocket.Context) *jsonrpc.Response {
publicMiddlewareCalled = true
return next(c)
}
})
// Public group (no additional middleware)
public := router.Group()
public.Handle("public.test", func(c *websocket.Context) *jsonrpc.Response {
return jsonrpc.NewResponse(c.Request.ID, "public")
})
// Private group with additional middleware
private := router.Group(func(next websocket.Handler) websocket.Handler {
return func(c *websocket.Context) *jsonrpc.Response {
privateMiddlewareCalled = true
return next(c)
}
})
private.Handle("private.test", func(c *websocket.Context) *jsonrpc.Response {
return jsonrpc.NewResponse(c.Request.ID, "private")
})
// Test public method - only global middleware should be called
c := &websocket.Context{
Context: context.Background(),
Request: &jsonrpc.Request{ID: 1, Method: "public.test"},
Method: "public.test",
StartTime: time.Now(),
}
publicMiddlewareCalled = false
privateMiddlewareCalled = false
router.Dispatch(c)
assert.True(t, publicMiddlewareCalled, "global middleware should be called")
assert.False(t, privateMiddlewareCalled, "private middleware should NOT be called for public method")
// Test private method - both middlewares should be called
c.Method = "private.test"
c.Request.Method = "private.test"
publicMiddlewareCalled = false
privateMiddlewareCalled = false
router.Dispatch(c)
assert.True(t, publicMiddlewareCalled, "global middleware should be called")
assert.True(t, privateMiddlewareCalled, "private middleware should be called for private method")
}