| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package network |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestRemoteIpFromAddr(t *testing.T) { |
| 11 |
r := RemoteIPFromAddr("127.0.0.1:8080") |
| 12 |
if r != "127.0.0.1" { |
| 13 |
t.Fatalf("Expected '127.0.0.1', got %s", r) |
| 14 |
} |
| 15 |
|
| 16 |
r = RemoteIPFromAddr("[::1]:8080") |
| 17 |
if r != "[::1]" { |
| 18 |
t.Fatalf("Expected '[::1]', got %s", r) |
| 19 |
} |
| 20 |
|
| 21 |
r = RemoteIPFromAddr(":8080") |
| 22 |
if r != "" { |
| 23 |
t.Fatalf("Expected '', got %s", r) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestExtractRemoteIP(t *testing.T) { |
| 28 |
r := ExtractRemoteIP("192.168.0.1, 192.168.0.2, 192.168.0.3") |
| 29 |
if r != "192.168.0.1" { |
| 30 |
t.Fatalf("Expected '192.168.0.1', got %s", r) |
| 31 |
} |
| 32 |
|
| 33 |
r = ExtractRemoteIP("192.168.0.1,192.168.0.2,192.168.0.3") |
| 34 |
if r != "192.168.0.1" { |
| 35 |
t.Fatalf("Expected '192.168.0.1', got %s", r) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestIsPrimaryCountry(t *testing.T) { |
| 40 |
var b bool |
| 41 |
list := []string{"FR", "DE", "GR"} |
| 42 |
|
| 43 |
clientInfo := GeoIPRecord{ |
| 44 |
CountryCode: "FR", |
| 45 |
} |
| 46 |
|
| 47 |
b = IsPrimaryCountry(clientInfo, list) |
| 48 |
if !b { |
| 49 |
t.Fatal("Expected true, got false") |
| 50 |
} |
| 51 |
|
| 52 |
clientInfo = GeoIPRecord{ |
| 53 |
CountryCode: "GR", |
| 54 |
} |
| 55 |
|
| 56 |
b = IsPrimaryCountry(clientInfo, list) |
| 57 |
if b { |
| 58 |
t.Fatal("Expected false, got true") |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestIsAdditionalCountry(t *testing.T) { |
| 63 |
var b bool |
| 64 |
list := []string{"FR", "DE", "GR"} |
| 65 |
|
| 66 |
clientInfo := GeoIPRecord{ |
| 67 |
CountryCode: "FR", |
| 68 |
} |
| 69 |
|
| 70 |
b = IsAdditionalCountry(clientInfo, list) |
| 71 |
if b { |
| 72 |
t.Fatal("Expected false, got true") |
| 73 |
} |
| 74 |
|
| 75 |
clientInfo = GeoIPRecord{ |
| 76 |
CountryCode: "GR", |
| 77 |
} |
| 78 |
|
| 79 |
b = IsAdditionalCountry(clientInfo, list) |
| 80 |
if !b { |
| 81 |
t.Fatal("Expected true, got false") |
| 82 |
} |
| 83 |
} |