forked from revh/ipinfo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipinfo_test.go
68 lines (54 loc) · 1.31 KB
/
ipinfo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ipinfo
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var fakeMyIPResponse = `{
"ip": "12.34.56.78",
"hostname": "google-proxy-12-34-56-78.google.com",
"city": null,
"country": "EU",
"loc": "12.0000,3.0000",
"org": "AS15169 Google Inc."
}`
var fakeForeignIPResponse = `{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}`
func mockServer(mockResponse string) *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, mockResponse)
}))
return server
}
func TestForeignIP(t *testing.T) {
server := mockServer(fakeForeignIPResponse)
defer server.Close()
ipinfoURI = server.URL
testIP := "8.8.8.8"
info, err := ForeignIP(testIP)
if err != nil {
t.Errorf(`ForeignIP("%s") error %s`, testIP, err)
}
if info.IP != testIP {
t.Errorf(`ForeignIP("%s") expected %s got %s`, testIP, testIP, info.IP)
}
}
func TestMyIP(t *testing.T) {
server := mockServer(fakeForeignIPResponse)
defer server.Close()
ipinfoURI = server.URL
_, err := MyIP()
if err != nil {
t.Errorf(`MyIP() error %s`, err)
}
}