1
0
Fork 0

ip/resolver: make Service.Lookup() accept a context

This commit is contained in:
Henrik Hautakoski 2023-12-02 11:58:53 +01:00
parent 43705f81d1
commit ec9056e9e0
6 changed files with 42 additions and 13 deletions

View file

@ -1,6 +1,7 @@
package basic_http
import (
"context"
"fmt"
"io"
"net"
@ -18,8 +19,8 @@ func (s Service) Name() string {
return s.ServiceName
}
func (s Service) Lookup() (net.IP, error) {
req, err := http.NewRequest("GET", s.Url, nil)
func (s Service) Lookup(ctx context.Context) (net.IP, error) {
req, err := http.NewRequestWithContext(ctx, "GET", s.Url, nil)
if err != nil {
return nil, err
}

View file

@ -1,6 +1,7 @@
package basic_http
import (
"context"
"net"
"net/http"
"net/http/httptest"
@ -24,7 +25,7 @@ func TestService_Lookup(t *testing.T) {
s := Service{Url: server.URL}
ip, err := s.Lookup()
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(255, 240, 85, 2), ip)
@ -46,7 +47,7 @@ func TestService_Lookup_WithHeaders(t *testing.T) {
},
}
ip, err := s.Lookup()
ip, err := s.Lookup(context.Background())
assert.NoError(t, err)
assert.Equal(t, net.IPv4(125, 74, 233, 13), ip)
@ -62,7 +63,7 @@ func TestService_Lookup_HTTPError(t *testing.T) {
Url: server.URL,
}
ip, err := s.Lookup()
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "HTTP Response: 404 Not Found")
assert.Nil(t, ip)
}
@ -78,7 +79,7 @@ func TestService_Lookup_ParseError(t *testing.T) {
Url: server.URL,
}
ip, err := s.Lookup()
ip, err := s.Lookup(context.Background())
assert.EqualError(t, err, "Failed to parse ip: random_string")
assert.Nil(t, ip)
}