feat(netxmocks): implement mocks for netxlite.Resolver (#398)
While there, make sure we require using &netxmocks.Dialer. Still part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
16aa8e5538
commit
c5dd9a68f1
9 changed files with 96 additions and 16 deletions
|
|
@ -16,8 +16,8 @@ type Dialer struct {
|
|||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext.
|
||||
func (d Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return d.MockDialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
var _ dialer = Dialer{}
|
||||
var _ dialer = &Dialer{}
|
||||
|
|
|
|||
34
internal/netxmocks/resolver.go
Normal file
34
internal/netxmocks/resolver.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package netxmocks
|
||||
|
||||
import "context"
|
||||
|
||||
// resolver is the interface we expect from a resolver
|
||||
type resolver interface {
|
||||
LookupHost(ctx context.Context, domain string) ([]string, error)
|
||||
Network() string
|
||||
Address() string
|
||||
}
|
||||
|
||||
// Resolver is a mockable Resolver.
|
||||
type Resolver struct {
|
||||
MockLookupHost func(ctx context.Context, domain string) ([]string, error)
|
||||
MockNetwork func() string
|
||||
MockAddress func() string
|
||||
}
|
||||
|
||||
// LookupHost implements Resolver.LookupHost.
|
||||
func (r *Resolver) LookupHost(ctx context.Context, domain string) ([]string, error) {
|
||||
return r.MockLookupHost(ctx, domain)
|
||||
}
|
||||
|
||||
// Address implements Resolver.Address.
|
||||
func (r *Resolver) Address() string {
|
||||
return r.MockAddress()
|
||||
}
|
||||
|
||||
// Network implements Resolver.Network.
|
||||
func (r *Resolver) Network() string {
|
||||
return r.MockNetwork()
|
||||
}
|
||||
|
||||
var _ resolver = &Resolver{}
|
||||
46
internal/netxmocks/resolver_test.go
Normal file
46
internal/netxmocks/resolver_test.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResolverLookupHost(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &Resolver{
|
||||
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "dns.google")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("expected nil addr")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverNetwork(t *testing.T) {
|
||||
r := &Resolver{
|
||||
MockNetwork: func() string {
|
||||
return "antani"
|
||||
},
|
||||
}
|
||||
if v := r.Network(); v != "antani" {
|
||||
t.Fatal("unexpected network", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverAddress(t *testing.T) {
|
||||
r := &Resolver{
|
||||
MockAddress: func() string {
|
||||
return "1.1.1.1"
|
||||
},
|
||||
}
|
||||
if v := r.Address(); v != "1.1.1.1" {
|
||||
t.Fatal("unexpected address", v)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue