-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountry_limiter_test.go
170 lines (159 loc) · 4.31 KB
/
country_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package rlutils
import (
"context"
"net/http"
"path/filepath"
"strings"
"testing"
"time"
)
// testHTTPRequest is a utility function that creates a new http.Request with a RemoteAddr set
func testHTTPRequest(remoteAddr string) *http.Request {
request, _ := http.NewRequest("GET", "http://example.com", nil)
request.RemoteAddr = remoteAddr
return request
}
func TestCountryLimiter(t *testing.T) {
abspath, _ := filepath.Abs("./testdata/GeoIP2-Country-Test.mmdb")
reqLimit := 10
// Define your test cases
testCases := []struct {
name string
request *http.Request
countries []string
skipCountries []string
expectedCountry string
expectedError bool
shouldLimit bool
}{
{
name: "Valid IP from United States With Port",
request: testHTTPRequest("50.114.0.1:1234"),
expectedCountry: "US",
countries: []string{"US"},
shouldLimit: true,
expectedError: false,
},
{
name: "Valid IP from United States",
request: testHTTPRequest("50.114.0.1"),
expectedCountry: "US",
shouldLimit: true,
expectedError: false,
},
{
name: "Invalid IP format",
request: testHTTPRequest("invalid-ip"),
expectedCountry: "",
shouldLimit: false,
expectedError: true,
},
{
name: "Valid IP from United States With WildCard",
request: testHTTPRequest("50.114.0.1"),
expectedCountry: "US",
countries: []string{"*"},
shouldLimit: true,
expectedError: false,
},
{
name: "Valid IP from United States With Skip country",
request: testHTTPRequest("50.114.0.1"),
expectedCountry: "US",
countries: []string{"*"},
skipCountries: []string{"US"},
shouldLimit: false,
expectedError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Run the country function to get the ISO country code
cl, err := NewCountryLimiter(
abspath,
[]string{"US"},
tc.skipCountries,
reqLimit,
1*time.Hour,
nil,
nil,
)
if err != nil {
t.Fatal(err)
}
remoteAddr := strings.Split(tc.request.RemoteAddr, ":")[0]
country, err := cl.country(remoteAddr)
if tc.expectedError {
if err == nil {
t.Errorf("Expected an error but did not get one")
}
} else {
if err != nil {
t.Errorf("Did not expect an error but got: %v", err)
}
if country != tc.expectedCountry {
t.Errorf("Expected country %s, but got %s", tc.expectedCountry, country)
}
}
rule, ruleErr := cl.Rule(tc.request)
if ruleErr != nil {
if !tc.expectedError {
t.Errorf("Rule method returned an unexpected error: %v", ruleErr)
}
} else {
if tc.shouldLimit && (rule == nil || rule.ReqLimit != reqLimit) {
t.Errorf("Expected shouldLimit rule with limit %d, but got %+v", reqLimit, rule)
}
if !tc.shouldLimit && (rule == nil || rule.ReqLimit != -1) {
t.Errorf("Expected disshouldLimit rule with no limiting, but got %+v", rule)
}
}
})
}
}
func TestCountryLimiterWithContext(t *testing.T) {
abspath, _ := filepath.Abs("./testdata/GeoIP2-Country-Test.mmdb")
reqLimit := 10
// Define your test cases
testCases := []struct {
name string
request *http.Request
countries []string
expectedCountry string
shouldLimit bool
}{
{
name: "Valid IP from United States With Port",
request: testHTTPRequest("50.114.0.1:1234"),
countries: []string{"US"},
shouldLimit: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Run the country function to get the ISO country code
cl, err := NewCountryLimiter(
abspath,
[]string{"US"},
[]string{},
reqLimit,
1*time.Hour,
nil,
nil,
)
if err != nil {
t.Fatal(err)
}
ctx := tc.request.Context()
ctx = context.WithValue(ctx, ContextCountryKey, "US")
tc.request = tc.request.WithContext(ctx)
rule, _ := cl.Rule(tc.request)
if tc.shouldLimit && (rule == nil || rule.ReqLimit != reqLimit) {
t.Errorf("Expected shouldLimit rule with limit %d, but got %+v", reqLimit, rule)
}
if !tc.shouldLimit && (rule == nil || rule.ReqLimit != -1) {
t.Errorf("Expected disshouldLimit rule with no limiting, but got %+v", rule)
}
})
}
}