e17a0b8f951af062eac19c12c5c59fe429d959b5 / internal/web/ratelimit_test.go · 691 bytes · raw
package web
import (
"testing"
"time"
)
func TestIPLimiterWindow(t *testing.T) {
limiter := newIPLimiter(2, time.Hour)
if !limiter.allow("a") || !limiter.allow("a") {
t.Fatal("first two hits for one key should be allowed")
}
if limiter.allow("a") {
t.Error("third hit within the window was allowed, want denied")
}
if !limiter.allow("b") {
t.Error("a different key should have its own budget")
}
}
func TestIPLimiterExpiry(t *testing.T) {
limiter := newIPLimiter(1, time.Millisecond)
if !limiter.allow("a") {
t.Fatal("first hit should be allowed")
}
time.Sleep(5 * time.Millisecond)
if !limiter.allow("a") {
t.Error("hit after the window should be allowed")
}
}