2021-09-05 14:49:38 +02:00
|
|
|
package mocks
|
2021-06-23 16:21:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-09-07 23:12:23 +02:00
|
|
|
func TestResolver(t *testing.T) {
|
|
|
|
t.Run("LookupHost", func(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")
|
|
|
|
}
|
|
|
|
})
|
2021-06-23 16:21:13 +02:00
|
|
|
|
2021-09-07 23:12:23 +02:00
|
|
|
t.Run("Network", func(t *testing.T) {
|
|
|
|
r := &Resolver{
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "antani"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if v := r.Network(); v != "antani" {
|
|
|
|
t.Fatal("unexpected network", v)
|
|
|
|
}
|
|
|
|
})
|
2021-06-23 16:21:13 +02:00
|
|
|
|
2021-09-07 23:12:23 +02:00
|
|
|
t.Run("Address", func(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)
|
|
|
|
}
|
|
|
|
})
|
2021-09-05 18:03:50 +02:00
|
|
|
|
2021-09-07 23:12:23 +02:00
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
r := &Resolver{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called = true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r.CloseIdleConnections()
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
2021-09-05 18:03:50 +02:00
|
|
|
}
|