-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
159 lines (141 loc) · 4.07 KB
/
middleware_test.go
File metadata and controls
159 lines (141 loc) · 4.07 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
package cachegrid
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestHTTPRateLimitMiddleware(t *testing.T) {
c := newTestCache(t)
handler := HTTPRateLimit(c, 3, time.Minute)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
for i := 0; i < 3; i++ {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "1.2.3.4:1234"
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("request %d: expected 200, got %d", i, rec.Code)
}
if rec.Header().Get("X-RateLimit-Limit") == "" {
t.Fatal("expected X-RateLimit-Limit header")
}
}
// 4th request should be rate limited
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "1.2.3.4:1234"
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusTooManyRequests {
t.Fatalf("expected 429, got %d", rec.Code)
}
}
func TestHTTPRateLimitByIP(t *testing.T) {
c := newTestCache(t)
handler := HTTPRateLimit(c, 1, time.Minute)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
// First IP
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.1:5000"
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("first IP first req: expected 200, got %d", rec.Code)
}
// Second IP — should be independent
rec2 := httptest.NewRecorder()
req2 := httptest.NewRequest("GET", "/", nil)
req2.RemoteAddr = "10.0.0.2:5000"
handler.ServeHTTP(rec2, req2)
if rec2.Code != http.StatusOK {
t.Fatalf("second IP first req: expected 200, got %d", rec2.Code)
}
}
func TestHTTPCacheMiddleware(t *testing.T) {
c := newTestCache(t)
callCount := 0
handler := HTTPCache(c, 5*time.Minute)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callCount++
w.Header().Set("X-Custom", "value")
w.WriteHeader(http.StatusOK)
w.Write([]byte("hello"))
}))
// First request — MISS
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/data", nil)
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", rec.Code)
}
if rec.Header().Get("X-Cache") != "MISS" {
t.Fatalf("expected X-Cache: MISS, got %s", rec.Header().Get("X-Cache"))
}
if callCount != 1 {
t.Fatalf("expected 1 call, got %d", callCount)
}
// Second request — HIT
rec2 := httptest.NewRecorder()
req2 := httptest.NewRequest("GET", "/api/data", nil)
handler.ServeHTTP(rec2, req2)
if rec2.Header().Get("X-Cache") != "HIT" {
t.Fatalf("expected X-Cache: HIT, got %s", rec2.Header().Get("X-Cache"))
}
if callCount != 1 {
t.Fatalf("handler should not be called on cache hit, got %d calls", callCount)
}
}
func TestHTTPCacheSkipNonGet(t *testing.T) {
c := newTestCache(t)
handler := HTTPCache(c, 5*time.Minute)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
// POST should not be cached
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/api/data", nil)
handler.ServeHTTP(rec, req)
if rec.Header().Get("X-Cache") != "" {
t.Fatal("POST should not have X-Cache header")
}
}
func TestClientIPExtraction(t *testing.T) {
tests := []struct {
name string
headers map[string]string
remoteIP string
expected string
}{
{
name: "X-Forwarded-For",
headers: map[string]string{"X-Forwarded-For": "203.0.113.1, 70.41.3.18"},
remoteIP: "127.0.0.1:1234",
expected: "203.0.113.1",
},
{
name: "X-Real-IP",
headers: map[string]string{"X-Real-IP": "203.0.113.2"},
remoteIP: "127.0.0.1:1234",
expected: "203.0.113.2",
},
{
name: "RemoteAddr",
headers: nil,
remoteIP: "10.0.0.1:5000",
expected: "10.0.0.1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = tt.remoteIP
for k, v := range tt.headers {
req.Header.Set(k, v)
}
got := clientIP(req)
if got != tt.expected {
t.Fatalf("expected %s, got %s", tt.expected, got)
}
})
}
}