ooni-probe-cli/internal/netxlite/http3_test.go
Simone Basso c6b3889a33
fix(netx): ensure we create ~same HTTP3 and HTTP2 transports (#795)
1. Use the netxlite.NewHTTPTransport factory for creating a new
HTTP2 (and HTTP1) transport;

2. Recognize the netxlite.NewOOHTTPTransport has now become
an implementation detail so make it private;

3. Recognize that netxlite.NewHTTP3Transport should call
netxlite.WrapTransport so it returns the same typechain
returned by netxlite.NewHTTPTransport (modulo, of course,
the real underlying transport), so ensure that we are
calling netxlite.WrapTransport in NewHTTP3Transport;

4. Recognize that the table based constructor inside of
netx needs a logger to create HTTPTransport instances using
either netxlite.NewHTTP{,3}Transport so pass this argument
along and ensure it's not nil using a constructor inside
model that guarantees that;

5. Cleanup netx's tests to avoid type asserting on the
typechains returned by netxlite since we already test
that inside netxlite;

6. Recognize that now we can make more legacy names inside
of netxlite private because we don't need to use them
inside tests anymore (because of previous point).

Reference issue: https://github.com/ooni/probe/issues/2121
2022-06-05 17:41:06 +02:00

92 lines
2.0 KiB
Go

package netxlite
import (
"crypto/tls"
"errors"
"net/http"
"testing"
"github.com/apex/log"
"github.com/lucas-clemente/quic-go/http3"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
nlmocks "github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestHTTP3Transport(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) {
var (
calledHTTP3 bool
calledDialer bool
)
txp := &http3Transport{
child: &nlmocks.HTTP3RoundTripper{
MockClose: func() error {
calledHTTP3 = true
return nil
},
},
dialer: &mocks.QUICDialer{
MockCloseIdleConnections: func() {
calledDialer = true
},
},
}
txp.CloseIdleConnections()
if !calledHTTP3 || !calledDialer {
t.Fatal("not called")
}
})
t.Run("Network", func(t *testing.T) {
txp := &http3Transport{}
if txp.Network() != "quic" {
t.Fatal("unexpected .Network return value")
}
})
t.Run("RoundTrip", func(t *testing.T) {
expected := errors.New("mocked error")
txp := &http3Transport{
child: &nlmocks.HTTP3RoundTripper{
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
return nil, expected
},
},
}
resp, err := txp.RoundTrip(&http.Request{})
if !errors.Is(err, expected) {
t.Fatal("unexpected err", err)
}
if resp != nil {
t.Fatal("unexpected resp")
}
})
}
func TestNewHTTP3Transport(t *testing.T) {
t.Run("creates the correct type chain", func(t *testing.T) {
qd := &mocks.QUICDialer{}
config := &tls.Config{}
txp := NewHTTP3Transport(log.Log, qd, config)
logger := txp.(*httpTransportLogger)
if logger.Logger != log.Log {
t.Fatal("invalid logger")
}
ew := logger.HTTPTransport.(*httpTransportErrWrapper)
h3txp := ew.HTTPTransport.(*http3Transport)
if h3txp.dialer != qd {
t.Fatal("invalid dialer")
}
h3 := h3txp.child.(*http3.RoundTripper)
if h3.Dial == nil {
t.Fatal("invalid Dial")
}
if !h3.DisableCompression {
t.Fatal("invalid DisableCompression")
}
if h3.TLSClientConfig != config {
t.Fatal("invalid TLSClientConfig")
}
})
}