@@ -18,19 +18,16 @@ func testHTTPRequest(remoteAddr string) *http.Request {
18
18
func TestCountryLimiter (t * testing.T ) {
19
19
abspath , _ := filepath .Abs ("./testdata/GeoIP2-Country-Test.mmdb" )
20
20
reqLimit := 10
21
- cl , err := NewCountryLimiter (abspath , []string {"US" }, reqLimit , 1 * time .Hour , nil , nil )
22
- if err != nil {
23
- t .Fatal (err )
24
- }
25
21
26
22
// Define your test cases
27
23
testCases := []struct {
28
- name string
29
- request * http.Request
30
- countries []string
31
- expectedCountry string
32
- expectedError bool
33
- allowed bool
24
+ name string
25
+ request * http.Request
26
+ countries []string
27
+ limitRateForOtherCountries bool
28
+ expectedCountry string
29
+ expectedError bool
30
+ allowed bool
34
31
}{
35
32
{
36
33
name : "Valid IP from United States With Port" ,
@@ -47,20 +44,49 @@ func TestCountryLimiter(t *testing.T) {
47
44
allowed : true ,
48
45
expectedError : false ,
49
46
},
50
-
51
47
{
52
48
name : "Invalid IP format" ,
53
49
request : testHTTPRequest ("invalid-ip" ),
54
50
expectedCountry : "" ,
55
51
allowed : false ,
56
52
expectedError : true ,
57
53
},
54
+ {
55
+ name : "Valid IP from United States With Port and limitRateForOtherCountries,empty country" ,
56
+ request : testHTTPRequest ("1.1.1.1" ),
57
+ expectedCountry : "" ,
58
+ countries : []string {"US" },
59
+ limitRateForOtherCountries : true ,
60
+ allowed : false ,
61
+ expectedError : false ,
62
+ },
63
+
64
+ {
65
+ name : "Valid IP from United States With Port and limitRateForOtherCountries,Franch" ,
66
+ request : testHTTPRequest ("67.43.156.0" ),
67
+ expectedCountry : "BT" ,
68
+ countries : []string {"US" },
69
+ limitRateForOtherCountries : true ,
70
+ allowed : true ,
71
+ expectedError : false ,
72
+ },
58
73
}
59
74
60
75
for _ , tc := range testCases {
61
76
t .Run (tc .name , func (t * testing.T ) {
62
77
// Run the country function to get the ISO country code
63
-
78
+ cl , err := NewCountryLimiter (
79
+ abspath ,
80
+ []string {"US" },
81
+ reqLimit ,
82
+ 1 * time .Hour ,
83
+ nil ,
84
+ tc .limitRateForOtherCountries ,
85
+ nil ,
86
+ )
87
+ if err != nil {
88
+ t .Fatal (err )
89
+ }
64
90
remoteAddr := strings .Split (tc .request .RemoteAddr , ":" )[0 ]
65
91
country , err := cl .country (remoteAddr )
66
92
if tc .expectedError {
0 commit comments