From f71b4a3502c71c42ba0a87475f2a27541d8b6e30 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Thu, 14 Sep 2023 15:49:58 +0200 Subject: [PATCH] cleanup(netxlite): add file for quirky behavior required by netx There are a bunch of quirks in the codebase that are there because the ./legacy/netx package assumes them. So, in light of the fact that I'm going to modify how we construct HTTP clients, I feel a bit safer if we move what I should not touch in a different place. Part of https://github.com/ooni/probe/issues/2531 --- internal/netxlite/{http.go => httplegacy.go} | 18 +------------ .../{http_test.go => httplegacy_test.go} | 10 ------- internal/netxlite/httpwrap.go | 27 +++++++++++++++++++ internal/netxlite/httpwrap_test.go | 16 +++++++++++ 4 files changed, 44 insertions(+), 27 deletions(-) rename internal/netxlite/{http.go => httplegacy.go} (89%) rename internal/netxlite/{http_test.go => httplegacy_test.go} (93%) create mode 100644 internal/netxlite/httpwrap.go create mode 100644 internal/netxlite/httpwrap_test.go diff --git a/internal/netxlite/http.go b/internal/netxlite/httplegacy.go similarity index 89% rename from internal/netxlite/http.go rename to internal/netxlite/httplegacy.go index 38c736d865..2d30da2a99 100644 --- a/internal/netxlite/http.go +++ b/internal/netxlite/httplegacy.go @@ -1,7 +1,7 @@ package netxlite // -// HTTP/1.1 and HTTP2 code +// Legacy HTTP code and behavior assumed by ./legacy/netx 😅 // import ( @@ -91,17 +91,6 @@ func newOOHTTPBaseTransport(dialer model.Dialer, tlsDialer model.TLSDialer) mode } } -// WrapHTTPTransport creates an HTTPTransport using the given logger -// and guarantees that returned errors are wrapped. -// -// This is a low level factory. Consider not using it directly. -func WrapHTTPTransport(logger model.DebugLogger, txp model.HTTPTransport) model.HTTPTransport { - return &httpTransportLogger{ - HTTPTransport: &httpTransportErrWrapper{txp}, - Logger: logger, - } -} - // NewHTTPTransportStdlib creates a new HTTPTransport using // the stdlib for DNS resolutions and TLS. // @@ -139,8 +128,3 @@ func NewHTTPClientWithResolver(logger model.Logger, reso model.Resolver) model.H func NewHTTPClient(txp model.HTTPTransport) model.HTTPClient { return WrapHTTPClient(&http.Client{Transport: txp}) } - -// WrapHTTPClient wraps an HTTP client to add error wrapping capabilities. -func WrapHTTPClient(clnt model.HTTPClient) model.HTTPClient { - return &httpClientErrWrapper{clnt} -} diff --git a/internal/netxlite/http_test.go b/internal/netxlite/httplegacy_test.go similarity index 93% rename from internal/netxlite/http_test.go rename to internal/netxlite/httplegacy_test.go index e5d29a8c8d..3043293e4e 100644 --- a/internal/netxlite/http_test.go +++ b/internal/netxlite/httplegacy_test.go @@ -157,13 +157,3 @@ func TestNewHTTPClientWithResolver(t *testing.T) { t.Fatal("invalid resolver") } } - -func TestWrapHTTPClient(t *testing.T) { - origClient := &http.Client{} - wrapped := WrapHTTPClient(origClient) - errWrapper := wrapped.(*httpClientErrWrapper) - innerClient := errWrapper.HTTPClient.(*http.Client) - if innerClient != origClient { - t.Fatal("not the inner client we expected") - } -} diff --git a/internal/netxlite/httpwrap.go b/internal/netxlite/httpwrap.go new file mode 100644 index 0000000000..fade2f4b9a --- /dev/null +++ b/internal/netxlite/httpwrap.go @@ -0,0 +1,27 @@ +package netxlite + +// +// Wrappers for already constructed types. +// + +import ( + "github.com/ooni/probe-cli/v3/internal/model" +) + +// WrapHTTPTransport creates an HTTPTransport using the given logger +// and guarantees that returned errors are wrapped. +// +// This is a low level factory. Consider not using it directly. +func WrapHTTPTransport(logger model.DebugLogger, txp model.HTTPTransport) model.HTTPTransport { + return &httpTransportLogger{ + HTTPTransport: &httpTransportErrWrapper{txp}, + Logger: logger, + } +} + +// WrapHTTPClient wraps an HTTP client to add error wrapping capabilities. +// +// This is a low level factory. Consider not using it directly. +func WrapHTTPClient(clnt model.HTTPClient) model.HTTPClient { + return &httpClientErrWrapper{clnt} +} diff --git a/internal/netxlite/httpwrap_test.go b/internal/netxlite/httpwrap_test.go new file mode 100644 index 0000000000..eb200c9735 --- /dev/null +++ b/internal/netxlite/httpwrap_test.go @@ -0,0 +1,16 @@ +package netxlite + +import ( + "net/http" + "testing" +) + +func TestWrapHTTPClient(t *testing.T) { + origClient := &http.Client{} + wrapped := WrapHTTPClient(origClient) + errWrapper := wrapped.(*httpClientErrWrapper) + innerClient := errWrapper.HTTPClient.(*http.Client) + if innerClient != origClient { + t.Fatal("not the inner client we expected") + } +}