This repository was archived by the owner on Jul 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipdata_test.go
More file actions
130 lines (109 loc) · 2.67 KB
/
ipdata_test.go
File metadata and controls
130 lines (109 loc) · 2.67 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ipdata
import (
"net/http"
"net/http/httptest"
"testing"
)
var (
mux *http.ServeMux
server *httptest.Server
)
func setUp() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
}
func tearDown() {
server.Close()
}
func assertEqual(t *testing.T, result interface{}, expect interface{}) {
if result != expect {
t.Fatalf("Expect (Value: %v) (Type: %T) - Got (Value: %v) (Type: %T)", expect, expect, result, result)
}
}
const fakeResponse = `
{
"ip": "foo",
"city": "bar",
"region": "baz",
"country_name": "",
"country_code": "",
"continent_name": "",
"continent_code": "",
"latitude": 37.751,
"longitude": -97.822,
"asn": "",
"organisation": "",
"postal": "",
"currency": "USD",
"currency_symbol": "$",
"calling_code": "1",
"flag": "https://ipdata.co/flags/us.png",
"time_zone": ""
}
`
func TestLookupOK(t *testing.T) {
setUp()
defer tearDown()
mux.HandleFunc("/foo/en", func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.Method, "GET")
w.Header().Add("Content-Type", acceptContentType)
_, _ = w.Write([]byte(fakeResponse))
})
idc := NewClient(WithURL(server.URL))
r, err := idc.Lookup(WithIP("foo"))
if err != nil {
t.Fatal(err)
}
assertEqual(t, r.IP, "foo")
assertEqual(t, r.City, "bar")
assertEqual(t, r.Region, "baz")
}
func TestLookupFail(t *testing.T) {
setUp()
defer tearDown()
errMsg := "127.0.0.1 is a private IP address"
mux.HandleFunc("/127.0.0.1/en", func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.Method, "GET")
w.Header().Add("Content-Type", acceptContentType)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(errMsg))
})
idc := NewClient(WithURL(server.URL))
_, err := idc.Lookup(WithIP("127.0.0.1"))
if err == nil {
t.Fatal("Expected error, got nil")
}
assertEqual(t, err.Error(), errMsg)
}
func TestWithAPIKey(t *testing.T) {
apiKey := "foo"
idc := NewClient(WithAPIKey(apiKey))
assertEqual(t, idc.APIKey, apiKey)
}
func TestWithLanguage(t *testing.T) {
lang := "vi"
idc := NewClient(WithLanguage(lang))
assertEqual(t, idc.Language, lang)
}
func TestWithURL(t *testing.T) {
url := "http://localhost"
idc := NewClient(WithURL(url))
assertEqual(t, idc.url, url)
}
func TestLookupNoIP(t *testing.T) {
setUp()
defer tearDown()
mux.HandleFunc("/en", func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.Method, "GET")
w.Header().Add("Content-Type", acceptContentType)
_, _ = w.Write([]byte(fakeResponse))
})
idc := NewClient(WithURL(server.URL))
r, err := idc.Lookup()
if err != nil {
t.Fatal(err)
}
assertEqual(t, r.IP, "foo")
assertEqual(t, r.City, "bar")
assertEqual(t, r.Region, "baz")
}