mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
ip/resolver/http/service.go: refactor default decoder function into its own module
This commit is contained in:
parent
c36a83c9b8
commit
1dedbbe2dd
3 changed files with 67 additions and 10 deletions
19
ip/resolver/decoder/text.go
Normal file
19
ip/resolver/decoder/text.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package decoder
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Text decoder that expects the IP address
|
||||
// to be on the first line of the response.
|
||||
// The decoder will only trim trailing and leading whitespace.
|
||||
func Text(r io.Reader) (string, error) {
|
||||
line, err := bufio.NewReader(r).ReadString('\n')
|
||||
if err == nil || err == io.EOF {
|
||||
line = strings.TrimRight(line, "\r\n")
|
||||
line = strings.TrimSpace(line)
|
||||
}
|
||||
return line, err
|
||||
}
|
||||
46
ip/resolver/decoder/text_test.go
Normal file
46
ip/resolver/decoder/text_test.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package decoder_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"dnsupdater/ip/resolver/decoder"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestService_Name(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected string
|
||||
expectError error
|
||||
}{
|
||||
{
|
||||
name: "simple",
|
||||
input: "192.168.0.1",
|
||||
expected: "192.168.0.1",
|
||||
expectError: io.EOF,
|
||||
},
|
||||
{
|
||||
name: "whitespace",
|
||||
input: " 192.168.0.1 ",
|
||||
expected: "192.168.0.1",
|
||||
expectError: io.EOF,
|
||||
},
|
||||
{
|
||||
name: "newline",
|
||||
input: "192.168.0.1\r\nmore stuff",
|
||||
expected: "192.168.0.1",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
ip, err := decoder.Text(strings.NewReader(test.input))
|
||||
assert.ErrorIs(t, err, test.expectError)
|
||||
assert.Equal(t, test.expected, ip)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,14 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
httputils "dnsupdater/http"
|
||||
"dnsupdater/ip/internal"
|
||||
"dnsupdater/ip/resolver/decoder"
|
||||
)
|
||||
|
||||
type Decoder func(io.Reader) (string, error)
|
||||
|
|
@ -32,14 +31,7 @@ func (s Service) Lookup(ctx context.Context) (net.IP, error) {
|
|||
}
|
||||
|
||||
if s.Decoder == nil {
|
||||
s.Decoder = func(r io.Reader) (string, error) {
|
||||
line, err := bufio.NewReader(r).ReadString('\n')
|
||||
if err == nil || err == io.EOF {
|
||||
line = strings.TrimRight(line, "\r\n")
|
||||
line = strings.TrimSpace(line)
|
||||
}
|
||||
return line, err
|
||||
}
|
||||
s.Decoder = decoder.Text
|
||||
}
|
||||
|
||||
body, err := s.Decoder(resp.Body)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue