-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimiter_test.go
More file actions
52 lines (44 loc) · 1.19 KB
/
limiter_test.go
File metadata and controls
52 lines (44 loc) · 1.19 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
package pubengine
import (
"testing"
"time"
)
func TestLoginLimiterBlocksAfterMax(t *testing.T) {
limiter := NewLoginLimiter(2, 200*time.Millisecond)
ip := "203.0.113.10"
if !limiter.Allow(ip) {
t.Fatalf("expected first attempt to be allowed")
}
if !limiter.Allow(ip) {
t.Fatalf("expected second attempt to be allowed")
}
if limiter.Allow(ip) {
t.Fatalf("expected third attempt to be blocked")
}
}
func TestLoginLimiterResetsAfterWindow(t *testing.T) {
limiter := NewLoginLimiter(1, 150*time.Millisecond)
ip := "203.0.113.20"
if !limiter.Allow(ip) {
t.Fatalf("expected first attempt to be allowed")
}
if limiter.Allow(ip) {
t.Fatalf("expected second attempt to be blocked")
}
time.Sleep(200 * time.Millisecond)
if !limiter.Allow(ip) {
t.Fatalf("expected attempt after window to be allowed")
}
}
func TestLoginLimiterIsPerIP(t *testing.T) {
limiter := NewLoginLimiter(1, 200*time.Millisecond)
if !limiter.Allow("203.0.113.30") {
t.Fatalf("expected first ip to be allowed")
}
if !limiter.Allow("203.0.113.31") {
t.Fatalf("expected second ip to be allowed independently")
}
if limiter.Allow("203.0.113.30") {
t.Fatalf("expected first ip to be blocked after max")
}
}