-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_agent_limiter_test.go
66 lines (54 loc) · 1.66 KB
/
user_agent_limiter_test.go
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
package rlutils
import (
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockCounter struct {
mock.Mock
}
func (m *MockCounter) Get(key string, window time.Time) (int, error) {
args := m.Called(key, window)
return args.Int(0), args.Error(1)
}
func (m *MockCounter) Increment(key string, window time.Time) error {
args := m.Called(key, window)
return args.Error(0)
}
func TestUserAgentLimiter(t *testing.T) {
mockCounter := new(MockCounter)
mockCounter.On("Get", mock.Anything, mock.Anything).Return(1, nil)
mockCounter.On("Increment", mock.Anything, mock.Anything).Return(nil)
userAgents := []string{"TestBot", "SuperBot"}
reqLimit := 5
windowLen := 1 * time.Minute
limiter := NewUserAgentLimiter(
userAgents,
reqLimit,
windowLen,
nil,
nil,
)
limiter.Counter = mockCounter
// Create a request with matching User-Agent
req := httptest.NewRequest("GET", "http://example.com", nil)
req.Header.Set("User-Agent", "TestBot 1.0")
// Check if the correct rule is returned for a matching User-Agent string
rule, err := limiter.Rule(req)
assert.NoError(t, err)
assert.NotNil(t, rule)
if rule != nil {
assert.Equal(t, reqLimit, rule.ReqLimit)
assert.Equal(t, windowLen, rule.WindowLen)
assert.Equal(t, "TestBot", rule.Key)
}
// Create a request with a non-matching User-Agent
nonMatchingReq := httptest.NewRequest("GET", "http://example.com", nil)
nonMatchingReq.Header.Set("User-Agent", "UnknownBot 1.0")
// Check if no rule is returned for a non-matching User-Agent string
noRule, err := limiter.Rule(nonMatchingReq)
assert.NoError(t, err)
assert.Equal(t, noRule.ReqLimit, -1)
}