-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_path_limiter_test.go
151 lines (142 loc) · 4.35 KB
/
request_path_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package rlutils
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRequestPathLimiter(t *testing.T) {
cases := []struct {
name string
contains []string
prefixes []string
suffixes []string
ignoreContains []string
ignorePrefixes []string
ignoreSuffixes []string
path string
key string
expectedToBeLimited bool
expectedKey string
}{
{
name: "Path contains limited segment",
contains: []string{"/user"},
prefixes: []string{"/api/"},
suffixes: []string{"/details"},
path: "/accounts/user/profile",
key: "host",
expectedToBeLimited: true,
expectedKey: "example.com/user",
},
{
name: "Path starts with limited prefix",
contains: []string{"user"},
prefixes: []string{"/api/"},
suffixes: []string{"/details"},
path: "/api/users/1",
key: "host",
expectedToBeLimited: true,
expectedKey: "example.com/api/",
},
{
name: "Path ends with limited suffix",
contains: []string{"user"},
prefixes: []string{"/api/"},
suffixes: []string{"/details"},
path: "/users/1/details",
key: "host",
expectedToBeLimited: true,
expectedKey: "example.com/details",
},
{
name: "Path does not match any criteria",
contains: []string{"user"},
prefixes: []string{"/api/"},
suffixes: []string{"/details"},
path: "/about",
key: "host",
expectedToBeLimited: false,
},
{
name: "Ignore path contains limited segment",
contains: []string{"/abc"},
prefixes: []string{"/abcd"},
suffixes: []string{"/abcde"},
path: "/abcdefg",
key: "host",
ignoreContains: []string{"ab"},
expectedToBeLimited: false,
expectedKey: "",
},
{
name: "Ignore path starts with limited prefix",
contains: []string{"/abc"},
prefixes: []string{"/abcd"},
suffixes: []string{"/abcde"},
path: "/abcdefg",
key: "host",
ignorePrefixes: []string{"/a"},
expectedToBeLimited: false,
expectedKey: "",
},
{
name: "Ignore path ends with limited suffix",
contains: []string{"/abc"},
prefixes: []string{"/abcd"},
suffixes: []string{"/abcde"},
path: "/abcdefg",
key: "host",
ignoreSuffixes: []string{"g"},
expectedToBeLimited: false,
expectedKey: "",
},
{
name: "Path contains limited segment with remote ip",
contains: []string{"/user"},
prefixes: []string{"/api/"},
suffixes: []string{"/details"},
path: "/accounts/user/profile",
key: "remote_addr",
expectedToBeLimited: true,
expectedKey: "127.0.0.1/user",
},
}
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, _ := NewRequestPathLimiter(
tc.contains,
tc.prefixes,
tc.suffixes,
reqLimit,
windowLen,
tc.key,
nil,
IgnorePathContains(tc.ignoreContains),
IgnorePathPrefixes(tc.ignorePrefixes),
IgnorePathSuffixes(tc.ignoreSuffixes),
)
// Using the mock counter instead of the real one.
limiter.Counter = mockCounter
req := httptest.NewRequest(http.MethodGet, tc.path, nil)
req.RemoteAddr = "127.0.0.1"
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)
}
})
}
}