refactor(dnsx): prepare for merging with netxlite (#515)

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-28 10:47:59 +02:00 committed by GitHub
commit 12cf4b9990
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 92 additions and 92 deletions

View file

@ -5,19 +5,19 @@ import "github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/model"
// HTTPSSvc is the result of HTTPS queries.
type HTTPSSvc = model.HTTPSSvc
// Decoder allows mocking dnsx.Decoder.
type Decoder struct {
// DNSDecoder allows mocking dnsx.DNSDecoder.
type DNSDecoder struct {
MockDecodeLookupHost func(qtype uint16, reply []byte) ([]string, error)
MockDecodeHTTPS func(reply []byte) (*HTTPSSvc, error)
}
// DecodeLookupHost calls MockDecodeLookupHost.
func (e *Decoder) DecodeLookupHost(qtype uint16, reply []byte) ([]string, error) {
func (e *DNSDecoder) DecodeLookupHost(qtype uint16, reply []byte) ([]string, error) {
return e.MockDecodeLookupHost(qtype, reply)
}
// DecodeHTTPS calls MockDecodeHTTPS.
func (e *Decoder) DecodeHTTPS(reply []byte) (*HTTPSSvc, error) {
func (e *DNSDecoder) DecodeHTTPS(reply []byte) (*HTTPSSvc, error) {
return e.MockDecodeHTTPS(reply)
}

View file

@ -7,10 +7,10 @@ import (
"github.com/miekg/dns"
)
func TestDecoder(t *testing.T) {
func TestDNSDecoder(t *testing.T) {
t.Run("DecodeLookupHost", func(t *testing.T) {
expected := errors.New("mocked error")
e := &Decoder{
e := &DNSDecoder{
MockDecodeLookupHost: func(qtype uint16, reply []byte) ([]string, error) {
return nil, expected
},
@ -26,7 +26,7 @@ func TestDecoder(t *testing.T) {
t.Run("DecodeHTTPS", func(t *testing.T) {
expected := errors.New("mocked error")
e := &Decoder{
e := &DNSDecoder{
MockDecodeHTTPS: func(reply []byte) (*HTTPSSvc, error) {
return nil, expected
},

View file

@ -1,11 +1,11 @@
package mocks
// Encoder allows mocking dnsx.Encoder.
type Encoder struct {
// DNSEncoder allows mocking dnsx.DNSEncoder.
type DNSEncoder struct {
MockEncode func(domain string, qtype uint16, padding bool) ([]byte, error)
}
// Encode calls MockEncode.
func (e *Encoder) Encode(domain string, qtype uint16, padding bool) ([]byte, error) {
func (e *DNSEncoder) Encode(domain string, qtype uint16, padding bool) ([]byte, error) {
return e.MockEncode(domain, qtype, padding)
}

View file

@ -7,10 +7,10 @@ import (
"github.com/miekg/dns"
)
func TestEncoder(t *testing.T) {
func TestDNSEncoder(t *testing.T) {
t.Run("Encode", func(t *testing.T) {
expected := errors.New("mocked error")
e := &Encoder{
e := &DNSEncoder{
MockEncode: func(domain string, qtype uint16, padding bool) ([]byte, error) {
return nil, expected
},

View file

@ -2,8 +2,8 @@ package mocks
import "context"
// RoundTripper allows mocking dnsx.RoundTripper.
type RoundTripper struct {
// DNSTransport allows mocking dnsx.DNSTransport.
type DNSTransport struct {
MockRoundTrip func(ctx context.Context, query []byte) (reply []byte, err error)
MockRequiresPadding func() bool
@ -16,26 +16,26 @@ type RoundTripper struct {
}
// RoundTrip calls MockRoundTrip.
func (txp *RoundTripper) RoundTrip(ctx context.Context, query []byte) (reply []byte, err error) {
func (txp *DNSTransport) RoundTrip(ctx context.Context, query []byte) (reply []byte, err error) {
return txp.MockRoundTrip(ctx, query)
}
// RequiresPadding calls MockRequiresPadding.
func (txp *RoundTripper) RequiresPadding() bool {
func (txp *DNSTransport) RequiresPadding() bool {
return txp.MockRequiresPadding()
}
// Network calls MockNetwork.
func (txp *RoundTripper) Network() string {
func (txp *DNSTransport) Network() string {
return txp.MockNetwork()
}
// Address calls MockAddress.
func (txp *RoundTripper) Address() string {
func (txp *DNSTransport) Address() string {
return txp.MockAddress()
}
// CloseIdleConnections calls MockCloseIdleConnections.
func (txp *RoundTripper) CloseIdleConnections() {
func (txp *DNSTransport) CloseIdleConnections() {
txp.MockCloseIdleConnections()
}

View file

@ -8,10 +8,10 @@ import (
"github.com/ooni/probe-cli/v3/internal/atomicx"
)
func TestRoundTripper(t *testing.T) {
func TestDNSTransport(t *testing.T) {
t.Run("RoundTrip", func(t *testing.T) {
expected := errors.New("mocked error")
txp := &RoundTripper{
txp := &DNSTransport{
MockRoundTrip: func(ctx context.Context, query []byte) ([]byte, error) {
return nil, expected
},
@ -26,7 +26,7 @@ func TestRoundTripper(t *testing.T) {
})
t.Run("RequiresPadding", func(t *testing.T) {
txp := &RoundTripper{
txp := &DNSTransport{
MockRequiresPadding: func() bool {
return true
},
@ -37,7 +37,7 @@ func TestRoundTripper(t *testing.T) {
})
t.Run("Network", func(t *testing.T) {
txp := &RoundTripper{
txp := &DNSTransport{
MockNetwork: func() string {
return "antani"
},
@ -48,7 +48,7 @@ func TestRoundTripper(t *testing.T) {
})
t.Run("Address", func(t *testing.T) {
txp := &RoundTripper{
txp := &DNSTransport{
MockAddress: func() string {
return "mascetti"
},
@ -60,7 +60,7 @@ func TestRoundTripper(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) {
called := &atomicx.Int64{}
txp := &RoundTripper{
txp := &DNSTransport{
MockCloseIdleConnections: func() {
called.Add(1)
},