-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflux_test.go
More file actions
121 lines (102 loc) · 2.8 KB
/
Copy pathflux_test.go
File metadata and controls
121 lines (102 loc) · 2.8 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
package flux_test
import (
"context"
"fmt"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/ocuris/flux"
)
// TestRaceSafety ensures that concurrent requests don't cause data races
// in the context pool or routing logic. Run with -race flag!
func TestRaceSafety(t *testing.T) {
app := flux.New(flux.Config{})
app.GET("/users/:id", func(c *flux.Context) error {
id := c.Param("id")
c.Set("id", id) // Test the internal store
return c.JSON(200, flux.Map{"id": id})
})
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
req := httptest.NewRequest("GET", fmt.Sprintf("/users/%d", idx), nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("expected 200, got %d", w.Code)
}
}(i)
}
wg.Wait()
}
// TestPathSanitization ensures that weird paths don't crash the server.
func TestPathSanitization(t *testing.T) {
app := flux.New(flux.Config{})
app.GET("/health", func(c *flux.Context) error {
return c.String(200, "ok")
})
evilPaths := []string{
"/health/",
"//health",
"/./health",
"/health/../health",
"/health?query=1#fragment",
}
for _, path := range evilPaths {
req := httptest.NewRequest("GET", path, nil)
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
// If it's a valid health check or a 404, it's fine as long as it didn't PANIC
if w.Code == 500 {
t.Errorf("Path %s caused a server error", path)
}
}
}
// TestMiddlewareLeakage ensures that data from one request is not
// visible in the next request due to pooling.
func TestMiddlewareLeakage(t *testing.T) {
app := flux.New(flux.Config{})
// Fast request that sets a key
app.GET("/set", func(c *flux.Context) error {
c.Set("secret", "top-hidden-data")
return c.String(200, "set")
})
// Request that should NOT see the key
app.GET("/check", func(c *flux.Context) error {
if _, ok := c.Get("secret"); ok {
return fmt.Errorf("LEAK DETECTED")
}
return c.String(200, "clean")
})
// Run set, then check
for i := 0; i < 100; i++ {
w1 := httptest.NewRecorder()
app.ServeHTTP(w1, httptest.NewRequest("GET", "/set", nil))
w2 := httptest.NewRecorder()
app.ServeHTTP(w2, httptest.NewRequest("GET", "/check", nil))
if w2.Code != 200 {
t.Fatal("Request leakage detected at iteration", i)
}
}
}
// TestGracefulShutdownTimeout ensures the server stops correctly
func TestGracefulStop(t *testing.T) {
app := flux.New(flux.Config{})
app.GET("/slow", func(c *flux.Context) error {
time.Sleep(100 * time.Millisecond)
return c.String(200, "done")
})
// Start server in background
go func() {
_ = app.Start(":9999")
}()
time.Sleep(50 * time.Millisecond)
// Trigger stop
stopErr := app.Stop(context.Background()) // Immediate stop for testing
if stopErr != nil {
t.Log("Stop returned:", stopErr)
}
}