ip/resolver/jsonip/service_test.go: adding json and ip parse test.
This commit is contained in:
parent
acf8976f53
commit
e7204794e2
1 changed files with 28 additions and 0 deletions
|
|
@ -43,3 +43,31 @@ func TestService_Lookup_HTTPError(t *testing.T) {
|
||||||
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
|
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
|
||||||
assert.Nil(t, ip)
|
assert.Nil(t, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestService_Lookup_JsonParseError(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, err := w.Write([]byte(`invalid_json`))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
s := Service{url: server.URL}
|
||||||
|
|
||||||
|
ip, err := s.Lookup(context.Background())
|
||||||
|
assert.EqualError(t, err, "invalid character 'i' looking for beginning of value")
|
||||||
|
assert.Nil(t, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestService_Lookup_IPParseError(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, err := w.Write([]byte(`{"ip":"xxx","geo-ip":"https://getjsonip.com/#plus","API Help":"https://getjsonip.com/#docs"}`))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
s := Service{url: server.URL}
|
||||||
|
|
||||||
|
ip, err := s.Lookup(context.Background())
|
||||||
|
assert.EqualError(t, err, "invalid IP address: xxx")
|
||||||
|
assert.Nil(t, ip)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue