-
Notifications
You must be signed in to change notification settings - Fork 14
/
setup_test.go
153 lines (122 loc) · 5.28 KB
/
setup_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
package geoip
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
maxminddb "github.com/oschwald/maxminddb-golang"
)
type testResponseRecorder struct {
*httpserver.ResponseWriterWrapper
}
func (testResponseRecorder) CloseNotify() <-chan bool { return nil }
func TestReplacers(t *testing.T) {
dbhandler, err := maxminddb.Open("./test-data/GeoLite2-City.mmdb")
if err != nil {
t.Errorf("geoip: Can't open database: GeoLite2-City.mmdb")
}
config := Config{}
l := GeoIP{
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return 0, nil
}),
DBHandler: dbhandler,
Config: config,
}
r := httptest.NewRequest("GET", "/", strings.NewReader(""))
r.RemoteAddr = "212.50.99.193"
rr := httpserver.NewResponseRecorder(testResponseRecorder{
ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
})
rr.Replacer = httpserver.NewReplacer(r, rr, "-")
l.ServeHTTP(rr, r)
if got, want := rr.Replacer.Replace("{geoip_country_code}"), "CY"; got != want {
t.Errorf("Expected custom placeholder {geoip_country_code} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_country_name}"), "Cyprus"; got != want {
t.Errorf("Expected custom placeholder {geoip_country_name} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_country_eu}"), "false"; got != want {
t.Errorf("Expected custom placeholder {geoip_country_eu} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_city_name}"), "Limassol"; got != want {
t.Errorf("Expected custom placeholder {geoip_city_name} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_latitude}"), "34.684100"; got != want {
t.Errorf("Expected custom placeholder {geoip_latitude} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_longitude}"), "33.037900"; got != want {
t.Errorf("Expected custom placeholder {geoip_longitude} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_time_zone}"), "Asia/Nicosia"; got != want {
t.Errorf("Expected custom placeholder {geoip_time_zone} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_geohash}"), "swpmrf13wbgg"; got != want {
t.Errorf("Expected custom placeholder {geoip_geohash} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_city_geoname_id}"), "146384"; got != want {
t.Errorf("Expected custom placeholder {geoip_city_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
}
if got, want := rr.Replacer.Replace("{geoip_country_geoname_id}"), "146669"; got != want {
t.Errorf("Expected custom placeholder {geoip_country_geoname_id} to be set (%s), but it wasn't; got: %s", want, got)
}
//
// Verify that a request via the loopback interface address results in
// the expected placeholder values.
//
var loopback_placeholders = [][2]string{
{"{geoip_country_code}", "**"},
{"{geoip_country_name}", "Loopback"},
{"{geoip_city_name}", "Loopback"},
{"{geoip_country_geoname_id}", "0"},
{"{geoip_city_geoname_id}", "0"},
{"{geoip_latitude}", "0.000000"},
{"{geoip_longitude}", "0.000000"},
{"{geoip_geohash}", "s00000000000"},
{"{geoip_time_zone}", ""},
}
r = httptest.NewRequest("GET", "/", strings.NewReader(""))
r.RemoteAddr = "127.0.0.1"
rr = httpserver.NewResponseRecorder(testResponseRecorder{
ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
})
rr.Replacer = httpserver.NewReplacer(r, rr, "-")
l.ServeHTTP(rr, r)
for _, v := range loopback_placeholders {
if got, want := rr.Replacer.Replace(v[0]), v[1]; got != want {
t.Errorf("Expected custom placeholder %s to be set (%s), but it wasn't; got: %s", v[0], want, got)
}
}
//
// Verify that a request via a private address results in the expected
// placeholder values. Note that the MaxMind DB doesn't include
// location data for private addresses.
//
var private_addr_placeholders = [][2]string{
{"{geoip_country_code}", "!!"},
{"{geoip_country_name}", "No Country"},
{"{geoip_city_name}", "No City"},
{"{geoip_country_geoname_id}", "0"},
{"{geoip_city_geoname_id}", "0"},
{"{geoip_latitude}", "0.000000"},
{"{geoip_longitude}", "0.000000"},
{"{geoip_geohash}", "s00000000000"},
{"{geoip_time_zone}", ""},
}
r = httptest.NewRequest("GET", "/", strings.NewReader(""))
r.RemoteAddr = "192.168.0.1"
rr = httpserver.NewResponseRecorder(testResponseRecorder{
ResponseWriterWrapper: &httpserver.ResponseWriterWrapper{ResponseWriter: httptest.NewRecorder()},
})
rr.Replacer = httpserver.NewReplacer(r, rr, "-")
l.ServeHTTP(rr, r)
if got, want := rr.Replacer.Replace("{geoip_country_code}"), "!!"; got != want {
t.Errorf("Expected custom placeholder {geoip_country_code} to be set (%s), but it wasn't; got: %s", want, got)
}
for _, v := range private_addr_placeholders {
if got, want := rr.Replacer.Replace(v[0]), v[1]; got != want {
t.Errorf("Expected custom placeholder %s to be set (%s), but it wasn't; got: %s", v[0], want, got)
}
}
}