MCPcopy Index your code
hub / github.com/dnote/dnote / TestLimit

Function TestLimit

pkg/server/middleware/limit_test.go:24–52  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

22)
23
24func TestLimit(t *testing.T) {
25 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26 w.WriteHeader(http.StatusOK)
27 })
28
29 limiter := NewRateLimiter()
30 middleware := limiter.Limit(handler)
31
32 // Make burst + 5 requests from same IP
33 numRequests := serverRateLimitBurst + 5
34 blockedCount := 0
35
36 for range numRequests {
37 req := httptest.NewRequest("GET", "/test", nil)
38 req.RemoteAddr = "192.168.1.1:1234"
39 w := httptest.NewRecorder()
40
41 middleware.ServeHTTP(w, req)
42
43 if w.Code == http.StatusTooManyRequests {
44 blockedCount++
45 }
46 }
47
48 // At least some requests after burst should be blocked
49 if blockedCount == 0 {
50 t.Error("Expected some requests to be rate limited after burst")
51 }
52}
53
54func TestLimit_DifferentIPs(t *testing.T) {
55 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 5

LimitMethod · 0.95
NewRateLimiterFunction · 0.85
WriteHeaderMethod · 0.80
ServeHTTPMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected