-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware_test.go
171 lines (141 loc) · 5.15 KB
/
middleware_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
171
package traefikgeoip2_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
mw "github.com/forestvpn/traefikgeoip2"
)
const (
ValidIP = "188.193.88.199"
ValidIPFromHeader = "188.193.88.245"
ValidIPNoCity = "20.1.184.61"
)
func TestGeoIPConfig(t *testing.T) {
mwCfg := mw.CreateConfig()
if mw.DefaultDBPath != mwCfg.DBPath {
t.Fatalf("Incorrect path")
}
mwCfg.DBPath = "./non-existing"
mw.ResetLookup()
_, err := mw.New(context.TODO(), nil, mwCfg, "")
if err != nil {
t.Fatalf("Must not fail on missing DB")
}
mwCfg.DBPath = "justfile"
_, err = mw.New(context.TODO(), nil, mwCfg, "")
if err != nil {
t.Fatalf("Must not fail on invalid DB format")
}
}
func TestGeoIPBasic(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-City.mmdb"
called := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { called = true })
mw.ResetLookup()
instance, err := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
if err != nil {
t.Fatalf("Error creating %v", err)
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
instance.ServeHTTP(recorder, req)
if recorder.Result().StatusCode != http.StatusOK {
t.Fatalf("Invalid return code")
}
if called != true {
t.Fatalf("next handler was not called")
}
}
func TestMissingGeoIPDB(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./missing"
called := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { called = true })
mw.ResetLookup()
instance, err := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
if err != nil {
t.Fatalf("Error creating %v", err)
}
recorder := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
instance.ServeHTTP(recorder, req)
if recorder.Result().StatusCode != http.StatusOK {
t.Fatalf("Invalid return code")
}
if called != true {
t.Fatalf("next handler was not called")
}
assertHeader(t, req, mw.CountryHeader, mw.Unknown)
assertHeader(t, req, mw.RegionHeader, mw.Unknown)
assertHeader(t, req, mw.CityHeader, mw.Unknown)
}
func TestGeoIPFromRemoteAddr(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-City.mmdb"
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.RemoteAddr = fmt.Sprintf("%s:9999", ValidIP)
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, "DE")
assertHeader(t, req, mw.RegionHeader, "BY")
assertHeader(t, req, mw.CityHeader, "Munich")
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.RemoteAddr = fmt.Sprintf("%s:9999", ValidIPNoCity)
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, "US")
assertHeader(t, req, mw.RegionHeader, mw.Unknown)
assertHeader(t, req, mw.CityHeader, mw.Unknown)
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.RemoteAddr = "qwerty:9999"
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, mw.Unknown)
assertHeader(t, req, mw.RegionHeader, mw.Unknown)
assertHeader(t, req, mw.CityHeader, mw.Unknown)
}
func TestGeoIPFromIPHeader(t *testing.T) {
ipHeader := "X-Forwarded-For"
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-City.mmdb"
mwCfg.IPHeader = ipHeader
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Set(ipHeader, ValidIPFromHeader)
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, "DE")
assertHeader(t, req, mw.RegionHeader, "BY")
assertHeader(t, req, mw.CityHeader, "Munich")
assertHeader(t, req, mw.CoordinatesHeader, "48.1663, 11.5683")
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Set(ipHeader, "qwerty")
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, mw.Unknown)
assertHeader(t, req, mw.RegionHeader, mw.Unknown)
assertHeader(t, req, mw.CityHeader, mw.Unknown)
assertHeader(t, req, mw.CoordinatesHeader, mw.Unknown)
}
func TestGeoIPCountryDBFromRemoteAddr(t *testing.T) {
mwCfg := mw.CreateConfig()
mwCfg.DBPath = "./GeoLite2-Country.mmdb"
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
mw.ResetLookup()
instance, _ := mw.New(context.TODO(), next, mwCfg, "traefik-geoip2")
req := httptest.NewRequest(http.MethodGet, "http://localhost", nil)
req.RemoteAddr = fmt.Sprintf("%s:9999", ValidIP)
instance.ServeHTTP(httptest.NewRecorder(), req)
assertHeader(t, req, mw.CountryHeader, "DE")
assertHeader(t, req, mw.RegionHeader, mw.Unknown)
assertHeader(t, req, mw.CityHeader, mw.Unknown)
}
func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()
if req.Header.Get(key) != expected {
t.Fatalf("invalid value of header [%s] != %s", key, req.Header.Get(key))
}
}