1
0
Fork 0
mirror of https://gitlab.com/pnx-tools/dns-updater.git synced 2026-06-16 05:54:56 +02:00
dns-updater/ip/resolver/jsonip/service_test.go

45 lines
1 KiB
Go

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)
}