Skip to content

Commit

Permalink
Added test and fix for NoLookup interface
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Jan 7, 2024
1 parent 036f861 commit bef5e89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Binary file added GeoIP2-City-Test.mmdb
Binary file not shown.
9 changes: 6 additions & 3 deletions geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ type CityLookup interface {
// NoLookup is a Lookup implementation which always return empty result.
type NoLookup struct{}

func (l NoLookup) CountryCode(ip net.IP) string { return "" }
func (l NoLookup) ISP(ip net.IP) string { return "" }
func (l NoLookup) ASN(ip net.IP) string { return "" }
func (l NoLookup) CountryCode(ip net.IP) string { return "" }
func (l NoLookup) ISP(ip net.IP) string { return "" }
func (l NoLookup) ASN(ip net.IP) string { return "" }
func (l NoLookup) City(ip net.IP) (string, string) { return "", "" }
func (l NoLookup) Ready() <-chan struct{} {
ch := make(chan struct{})
close(ch)
return ch
}

var _ Lookup = NoLookup{}

type lookup struct {
runner *keepcurrent.Runner
db atomic.Value
Expand Down
14 changes: 14 additions & 0 deletions geo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@ func TestFromFile(t *testing.T) {
testLookupISP(t, l, "127.0.0.1", "")
testLookupISP(t, l, "1.1.1.1", "")
}

func TestLookupCity(t *testing.T) {
filePath := "./GeoIP2-City-Test.mmdb"
l, err := FromFile(filePath)
assert.NoError(t, err)

testLookupCity(t, l, "81.2.69.142", "London", "United Kingdom")
}

func testLookupCity(t *testing.T, l Lookup, ip string, expectedCity, expectedCountry string) {
city, country := l.City(net.ParseIP(ip))
assert.Equal(t, expectedCity, city)
assert.Equal(t, expectedCountry, country)
}

0 comments on commit bef5e89

Please sign in to comment.