line ending fix
This commit is contained in:
parent
a0e4de3d19
commit
0c347312bd
26 changed files with 1053 additions and 1046 deletions
|
|
@ -1,46 +1,46 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
httputils "dnsupdater/http"
|
||||
"dnsupdater/ip/internal"
|
||||
)
|
||||
|
||||
type Decoder func(io.Reader) ([]byte, error)
|
||||
|
||||
type Service struct {
|
||||
ServiceName string
|
||||
Url string
|
||||
Headers http.Header
|
||||
Decoder Decoder
|
||||
}
|
||||
|
||||
func (s Service) Name() string {
|
||||
return s.ServiceName
|
||||
}
|
||||
|
||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||
resp, err := httputils.Get(ctx, s.Url, s.Headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s.Decoder == nil {
|
||||
s.Decoder = io.ReadAll
|
||||
}
|
||||
|
||||
body, err := s.Decoder(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trim spaces and stuff.
|
||||
ip_str := strings.TrimSpace(string(body))
|
||||
|
||||
return internal.ParseIP(ip_str)
|
||||
}
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
httputils "dnsupdater/http"
|
||||
"dnsupdater/ip/internal"
|
||||
)
|
||||
|
||||
type Decoder func(io.Reader) ([]byte, error)
|
||||
|
||||
type Service struct {
|
||||
ServiceName string
|
||||
Url string
|
||||
Headers http.Header
|
||||
Decoder Decoder
|
||||
}
|
||||
|
||||
func (s Service) Name() string {
|
||||
return s.ServiceName
|
||||
}
|
||||
|
||||
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
||||
resp, err := httputils.Get(ctx, s.Url, s.Headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s.Decoder == nil {
|
||||
s.Decoder = io.ReadAll
|
||||
}
|
||||
|
||||
body, err := s.Decoder(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Trim spaces and stuff.
|
||||
ip_str := strings.TrimSpace(string(body))
|
||||
|
||||
return internal.ParseIP(ip_str)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,85 +1,85 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestService_Name(t *testing.T) {
|
||||
s := Service{ServiceName: "my_service"}
|
||||
|
||||
assert.Equal(t, "my_service", s.Name())
|
||||
}
|
||||
|
||||
func TestService_Lookup(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("255.240.85.2"))
|
||||
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(255, 240, 85, 2), ip)
|
||||
}
|
||||
|
||||
func TestService_Lookup_WithHeaders(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
||||
|
||||
_, err := w.Write([]byte("125.74.233.13"))
|
||||
assert.NoError(t, err)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
s := Service{
|
||||
Url: server.URL,
|
||||
Headers: http.Header{
|
||||
"Content-Type": []string{"application/json"},
|
||||
},
|
||||
}
|
||||
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, net.IPv4(125, 74, 233, 13), 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_ParseError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("random_string"))
|
||||
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: random_string")
|
||||
assert.Nil(t, ip)
|
||||
}
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestService_Name(t *testing.T) {
|
||||
s := Service{ServiceName: "my_service"}
|
||||
|
||||
assert.Equal(t, "my_service", s.Name())
|
||||
}
|
||||
|
||||
func TestService_Lookup(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("255.240.85.2"))
|
||||
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(255, 240, 85, 2), ip)
|
||||
}
|
||||
|
||||
func TestService_Lookup_WithHeaders(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
|
||||
|
||||
_, err := w.Write([]byte("125.74.233.13"))
|
||||
assert.NoError(t, err)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
s := Service{
|
||||
Url: server.URL,
|
||||
Headers: http.Header{
|
||||
"Content-Type": []string{"application/json"},
|
||||
},
|
||||
}
|
||||
|
||||
ip, err := s.Lookup(context.Background())
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, net.IPv4(125, 74, 233, 13), 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_ParseError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("random_string"))
|
||||
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: random_string")
|
||||
assert.Nil(t, ip)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue