remove ip/resolver/jsonip
This commit is contained in:
parent
ae60d3bf35
commit
22a72de719
2 changed files with 0 additions and 115 deletions
|
|
@ -1,42 +0,0 @@
|
||||||
package jsonip
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"dnsupdater/http"
|
|
||||||
"dnsupdater/ip/internal"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Service struct {
|
|
||||||
url string
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Service {
|
|
||||||
return &Service{
|
|
||||||
url: "https://jsonip.com",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Service) Name() string {
|
|
||||||
return "jsonip"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
|
||||||
resp, err := http.Get(ctx, s.url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var v struct {
|
|
||||||
Ip string `json:"ip"`
|
|
||||||
Location string `json:"geo-ip"`
|
|
||||||
Help string `json:"API Help"`
|
|
||||||
}
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return internal.ParseIP(v.Ip)
|
|
||||||
}
|
|
||||||
|
|
@ -1,73 +0,0 @@
|
||||||
package jsonip
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestService_Name(t *testing.T) {
|
|
||||||
s := Service{}
|
|
||||||
|
|
||||||
assert.Equal(t, "jsonip", s.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestService_Lookup(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
_, err := w.Write([]byte(`{"ip":"211.46.32.214","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.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, net.IPv4(211, 46, 32, 214), ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestService_Lookup_HTTPError(t *testing.T) {
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(404)
|
|
||||||
}))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
s := Service{url: server.URL}
|
|
||||||
|
|
||||||
ip, err := s.Lookup(context.Background())
|
|
||||||
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
|
|
||||||
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