-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfixes_test.go
More file actions
319 lines (265 loc) · 8.39 KB
/
fixes_test.go
File metadata and controls
319 lines (265 loc) · 8.39 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
package bon
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/nissy/bon/middleware"
)
// Test panic recovery
func TestMuxPanicRecovery(t *testing.T) {
r := NewRouter()
// Add recovery middleware
r.Use(middleware.Recovery())
// Handler that causes panic
r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("test panic in handler")
})
// Normal handler
r.Get("/normal", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("OK"))
})
// Test panic
req1 := httptest.NewRequest("GET", "/panic", nil)
rec1 := httptest.NewRecorder()
r.ServeHTTP(rec1, req1)
if rec1.Code != http.StatusInternalServerError {
t.Errorf("Expected status 500 for panic, got %d", rec1.Code)
}
if rec1.Body.String() != "Internal Server Error\n" {
t.Errorf("Expected 'Internal Server Error', got %s", rec1.Body.String())
}
// Verify normal requests still work
req2 := httptest.NewRequest("GET", "/normal", nil)
rec2 := httptest.NewRecorder()
r.ServeHTTP(rec2, req2)
if rec2.Code != 200 {
t.Errorf("Expected status 200 for normal request, got %d", rec2.Code)
}
}
// Context key type for testing
type testCtxKey string
// Test context propagation in middleware (fixed version)
func TestMiddlewareContextPropagationFixed(t *testing.T) {
r := NewRouter()
// Middleware that sets values in context
contextMiddleware := func(key, value string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), testCtxKey(key), value)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
r.Use(contextMiddleware("key1", "value1"))
r.Use(contextMiddleware("key2", "value2"))
r.Get("/context", func(w http.ResponseWriter, r *http.Request) {
val1 := r.Context().Value(testCtxKey("key1"))
val2 := r.Context().Value(testCtxKey("key2"))
response := fmt.Sprintf("key1=%v,key2=%v", val1, val2)
_, _ = w.Write([]byte(response))
})
req := httptest.NewRequest("GET", "/context", nil)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
expected := "key1=value1,key2=value2"
if rec.Body.String() != expected {
t.Errorf("Expected %s, got %s", expected, rec.Body.String())
}
}
// Test conditional headers in CORS middleware
func TestCORSConditionalHeaders(t *testing.T) {
tests := []struct {
name string
allowCredentials bool
expectCredHeader bool
method string
expectedStatus int
}{
{
name: "Credentials true",
allowCredentials: true,
expectCredHeader: true,
method: "GET",
expectedStatus: 200,
},
{
name: "Credentials false",
allowCredentials: false,
expectCredHeader: false,
method: "GET",
expectedStatus: 200,
},
{
name: "OPTIONS request",
allowCredentials: true,
expectCredHeader: true,
method: "OPTIONS",
expectedStatus: 204,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := NewRouter()
corsConfig := middleware.AccessControlConfig{
AllowOrigin: "*",
AllowCredentials: tt.allowCredentials,
AllowMethods: []string{"GET", "POST"},
}
r.Use(middleware.CORS(corsConfig))
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("OK"))
})
// Also register OPTIONS method (for CORS preflight)
r.Handle("OPTIONS", "/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// CORS middleware should handle this, so we shouldn't reach here
// Error if we reach here
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Should not reach here"))
}))
req := httptest.NewRequest(tt.method, "/test", nil)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, rec.Code)
}
// For OPTIONS requests, check if CORS headers are set
if tt.method == "OPTIONS" {
originHeader := rec.Header().Get("Access-Control-Allow-Origin")
if originHeader != "*" {
t.Errorf("Expected Access-Control-Allow-Origin header to be '*', got %s", originHeader)
}
}
credHeader := rec.Header().Get("Access-Control-Allow-Credentials")
if tt.expectCredHeader && credHeader != "true" {
t.Errorf("Expected Access-Control-Allow-Credentials header to be 'true', got %s", credHeader)
} else if !tt.expectCredHeader && credHeader != "" {
t.Errorf("Expected no Access-Control-Allow-Credentials header, got %s", credHeader)
}
})
}
}
// Test goroutine leak prevention in Timeout middleware
func TestTimeoutNoGoroutineLeak(t *testing.T) {
r := NewRouter()
// 100ms timeout
r.Use(middleware.Timeout(100 * time.Millisecond))
// Long-running handler
r.Get("/slow", func(w http.ResponseWriter, r *http.Request) {
select {
case <-time.After(200 * time.Millisecond):
_, _ = w.Write([]byte("Should not reach here"))
case <-r.Context().Done():
// Exit when context is canceled
return
}
})
req := httptest.NewRequest("GET", "/slow", nil)
rec := httptest.NewRecorder()
// Record goroutine count
// In actual test, use runtime.NumGoroutine
r.ServeHTTP(rec, req)
if rec.Code != http.StatusGatewayTimeout {
t.Errorf("Expected status %d, got %d", http.StatusGatewayTimeout, rec.Code)
}
// Wait a bit after timeout to verify goroutine termination
time.Sleep(300 * time.Millisecond)
// In actual test, check goroutine count again here
}
// File server security test
func TestFileServerSecurity(t *testing.T) {
// Create temporary directory for testing
tempDir := t.TempDir()
_ = tempDir // Keep for future use
// File server security is tested in file.go's resolveFilePath method
// Keep here as placeholder for integration test
tests := []struct {
name string
path string
expectedStatus int
}{
{
name: "Normal file",
path: "/static/test.txt",
expectedStatus: 404, // File doesn't exist
},
{
name: "Directory traversal attempt",
path: "/static/../../../etc/passwd",
expectedStatus: 403, // Forbidden
},
{
name: "URL encoded traversal",
path: "/static/..%2F..%2Fetc%2Fpasswd",
expectedStatus: 403,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// In actual test, test file server endpoint
// Currently skipped
})
}
}
// Test multiple middleware order and propagation
func TestMiddlewareChainOrder(t *testing.T) {
r := NewRouter()
var order []string
type mwKey string
// Middleware 1
mw1 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
order = append(order, "MW1-before")
ctx := context.WithValue(r.Context(), mwKey("mw1"), "value1")
next.ServeHTTP(w, r.WithContext(ctx))
order = append(order, "MW1-after")
})
}
// Middleware 2
mw2 := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
order = append(order, "MW2-before")
// Verify MW1 value is accessible
if val := r.Context().Value(mwKey("mw1")); val != "value1" {
t.Errorf("MW1 context value not found in MW2")
}
ctx := context.WithValue(r.Context(), mwKey("mw2"), "value2")
next.ServeHTTP(w, r.WithContext(ctx))
order = append(order, "MW2-after")
})
}
r.Use(mw1)
r.Use(mw2)
r.Get("/test", func(w http.ResponseWriter, r *http.Request) {
order = append(order, "Handler")
// Verify both middleware values are accessible
if val := r.Context().Value(mwKey("mw1")); val != "value1" {
t.Errorf("MW1 context value not found in handler")
}
if val := r.Context().Value(mwKey("mw2")); val != "value2" {
t.Errorf("MW2 context value not found in handler")
}
_, _ = w.Write([]byte("OK"))
})
order = []string{} // Reset
req := httptest.NewRequest("GET", "/test", nil)
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
expectedOrder := []string{
"MW1-before",
"MW2-before",
"Handler",
"MW2-after",
"MW1-after",
}
if len(order) != len(expectedOrder) {
t.Fatalf("Expected %d calls, got %d: %v", len(expectedOrder), len(order), order)
}
for i, expected := range expectedOrder {
if order[i] != expected {
t.Errorf("Order[%d]: expected %s, got %s", i, expected, order[i])
}
}
}