simple geo ip server
This commit is contained in:
50
internal/geo/resolver_test.go
Normal file
50
internal/geo/resolver_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package geo
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupValidIP(t *testing.T) {
|
||||
dbPath := filepath.Join("..", "..", "GeoLite2-City.mmdb")
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
t.Skipf("mmdb not available at %s: %v", dbPath, err)
|
||||
}
|
||||
|
||||
resolver, err := NewResolver(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open db: %v", err)
|
||||
}
|
||||
defer resolver.Close()
|
||||
|
||||
loc, err := resolver.Lookup("1.1.1.1")
|
||||
if err != nil {
|
||||
t.Fatalf("lookup failed: %v", err)
|
||||
}
|
||||
|
||||
if loc.IP != "1.1.1.1" {
|
||||
t.Errorf("unexpected IP: %s", loc.IP)
|
||||
}
|
||||
// Ensure coordinates are populated for sanity.
|
||||
if loc.Latitude == 0 && loc.Longitude == 0 {
|
||||
t.Errorf("expected non-zero coordinates, got lat=%f lon=%f", loc.Latitude, loc.Longitude)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupInvalidIP(t *testing.T) {
|
||||
dbPath := filepath.Join("..", "..", "GeoLite2-City.mmdb")
|
||||
if _, err := os.Stat(dbPath); err != nil {
|
||||
t.Skipf("mmdb not available at %s: %v", dbPath, err)
|
||||
}
|
||||
|
||||
resolver, err := NewResolver(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open db: %v", err)
|
||||
}
|
||||
defer resolver.Close()
|
||||
|
||||
if _, err := resolver.Lookup("not-an-ip"); err == nil {
|
||||
t.Fatal("expected error for invalid IP")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user