Include IP in decimal format

Fixes #19
This commit is contained in:
Martin Polden
2016-07-06 23:44:33 +02:00
parent cfb86fe124
commit 6a9202851d
4 changed files with 44 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package api
import (
"io/ioutil"
"log"
"math/big"
"net"
"net/http"
"net/http/httptest"
@ -84,7 +85,7 @@ func TestJSONHandlers(t *testing.T) {
out string
status int
}{
{s.URL, `{"ip":"127.0.0.1","country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200},
{s.URL, `{"ip":"127.0.0.1","ip_decimal":2130706433,"country":"Elbonia","city":"Bornyasherk","hostname":"localhost"}`, 200},
{s.URL + "/port/foo", `{"error":"404 page not found"}`, 404},
{s.URL + "/port/0", `{"error":"Invalid port: 0"}`, 400},
{s.URL + "/port/65356", `{"error":"Invalid port: 65356"}`, 400},
@ -161,3 +162,19 @@ func TestCLIMatcher(t *testing.T) {
}
}
}
func TestIPToDecimal(t *testing.T) {
var tests = []struct {
in string
out *big.Int
}{
{"127.0.0.1", big.NewInt(2130706433)},
{"::1", big.NewInt(1)},
}
for _, tt := range tests {
i := ipToDecimal(net.ParseIP(tt.in))
if i.Cmp(tt.out) != 0 {
t.Errorf("Expected %d, got %d for IP %s", tt.out, i, tt.in)
}
}
}