a3654f60b7
We would like to refactor the code so that a DoH resolver owns the connections of its underlying HTTP client. To do that, we need first to incorporate CloseIdleConnections into the Resolver model. Then, we need to add the same function to all netxlite types that wrap a Resolver type. At the same time, we want the rest of the code for now to continue with the simpler definition of a Resolver, now called ResolverLegacy. We will eventually propagate this change to the rest of the tree and simplify the way in which we manage Resolvers. To make this possible, we introduce a new factory function that adapts a ResolverLegacy to become a Resolver. See https://github.com/ooni/probe/issues/1591.
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package mocks
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestResolverCloseIdleConnections(t *testing.T) {
|
|
var called bool
|
|
r := &Resolver{
|
|
MockCloseIdleConnections: func() {
|
|
called = true
|
|
},
|
|
}
|
|
r.CloseIdleConnections()
|
|
if !called {
|
|
t.Fatal("not called")
|
|
}
|
|
}
|