| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestIPLast(t *testing.T) { |
| 10 | got := ipLast([]string{"1.2.3.4", "--app", "a"}) |
| 11 | if strings.Join(got, " ") != "--app a 1.2.3.4" { |
| 12 | t.Fatalf("ipLast = %v", got) |
| 13 | } |
| 14 | got = ipLast([]string{"--app", "a", "1.2.3.4"}) |
| 15 | if strings.Join(got, " ") != "--app a 1.2.3.4" { |
| 16 | t.Fatalf("ipLast = %v", got) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestPrintGeo(t *testing.T) { |
| 21 | lat, lon := 51.51, -0.09 |
| 22 | var out bytes.Buffer |
| 23 | printGeo(&out, "81.2.69.142", apiGeoResponse{Found: true, Country: "GB", Subdivision: "GB-ENG", Lat: &lat, Lon: &lon, |
| 24 | Source: "DBIP-City-Lite", Attribution: "IP Geolocation by DB-IP", AttributionURL: "https://db-ip.com"}) |
| 25 | for _, want := range []string{"country GB", "subdivision GB-ENG", "51.51, -0.09", "IP Geolocation by DB-IP"} { |
| 26 | if !strings.Contains(out.String(), want) { |
| 27 | t.Errorf("output lacks %q:\n%s", want, out.String()) |
| 28 | } |
| 29 | } |
| 30 | out.Reset() |
| 31 | printGeo(&out, "10.0.0.1", apiGeoResponse{}) |
| 32 | if !strings.Contains(out.String(), "no location") { |
| 33 | t.Fatalf("miss = %q", out.String()) |
| 34 | } |
| 35 | } |
| 36 | |