-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_parameter_limiter_test.go
102 lines (92 loc) · 2.58 KB
/
get_parameter_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package rlutils
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// MockCounter implementation as previously described.
func TestGetParameterLimiter(t *testing.T) {
cases := []struct {
name string
getParameters map[string]string
queryString string
key string
expectedToBeLimited bool
expectedKey string
}{
{
name: "Request with matching get parameter should be limited",
getParameters: map[string]string{
"token": "123456",
},
queryString: "?token=123456",
key: "host",
expectedToBeLimited: true,
expectedKey: "example.com/token=123456",
},
{
name: "Request without matching get parameter should not be limited",
getParameters: map[string]string{
"token": "123456",
},
queryString: "?token=abcdef",
key: "host",
expectedToBeLimited: false,
},
{
name: "Request with multiple get parameters, one matches should be limited",
getParameters: map[string]string{
"token": "123456",
"sessionId": "ABCDEF",
},
queryString: "?token=123456&sessionId=XYZ",
key: "host",
expectedToBeLimited: true,
expectedKey: "example.com/token=123456",
},
{
name: "Request with matching get parameter should be limited with remote ip",
getParameters: map[string]string{
"token": "123456",
},
queryString: "?token=123456",
key: "remote_addr",
expectedToBeLimited: true,
expectedKey: "127.0.0.1/token=123456",
},
}
for _, tc := range cases {
t.Run(tc.name, func(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)
reqLimit := 5
windowLen := time.Minute
limiter, _ := NewGetParameterLimiter(
tc.getParameters,
reqLimit,
windowLen,
tc.key,
nil,
nil,
)
// Using the mock counter instead of the real one.
limiter.Counter = mockCounter
req := httptest.NewRequest(http.MethodGet, "http://example.com"+tc.queryString, nil)
req.RemoteAddr = "127.0.0.1:12345"
rule, err := limiter.Rule(req)
assert.NoError(t, err)
if tc.expectedToBeLimited {
assert.NotNil(t, rule)
assert.Equal(t, tc.expectedKey, rule.Key)
assert.Equal(t, reqLimit, rule.ReqLimit)
assert.Equal(t, windowLen, rule.WindowLen)
} else {
assert.Equal(t, -1, rule.ReqLimit)
}
})
}
}