(t *testing.T)
| 22 | ) |
| 23 | |
| 24 | func 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 | |
| 54 | func TestLimit_DifferentIPs(t *testing.T) { |
| 55 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
nothing calls this directly
no test coverage detected