refactor: migrate apitool from netx to netxlite (#496)

I discovered which transport were used by apitool and made sure he gets the same transports now. While there, I discovered an issue with ooni/oohttp that has been fixed with cba9b1ce5e.

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-09 01:19:17 +02:00 committed by GitHub
commit 1d79d70b43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 149 additions and 102 deletions

View file

@ -44,7 +44,7 @@ type Config struct {
// Bug
//
// This implementation cannot properly account for the bytes that are sent by
// persistent connections, because they strick to the counters set when the
// persistent connections, because they stick to the counters set when the
// connection was established. This typically means we miss the bytes sent and
// received when submitting a measurement. Such bytes are specifically not
// seen by the experiment specific byte counter.

View file

@ -1,19 +1,9 @@
package httptransport
import "net/http"
import (
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// UserAgentTransport is a transport that ensures that we always
// set an OONI specific default User-Agent header.
type UserAgentTransport struct {
RoundTripper
}
// RoundTrip implements RoundTripper.RoundTrip
func (txp UserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "miniooni/0.1.0-dev")
}
return txp.RoundTripper.RoundTrip(req)
}
var _ RoundTripper = UserAgentTransport{}
type UserAgentTransport = netxlite.UserAgentTransport

View file

@ -1,51 +0,0 @@
package httptransport_test
import (
"net/http"
"net/url"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
)
func TestUserAgentWithDefault(t *testing.T) {
txp := httptransport.UserAgentTransport{
RoundTripper: httptransport.FakeTransport{
Resp: &http.Response{StatusCode: 200},
},
}
req := &http.Request{URL: &url.URL{
Scheme: "https",
Host: "www.google.com",
Path: "/",
}}
req.Header = http.Header{}
resp, err := txp.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
if resp.Request.Header.Get("User-Agent") != "miniooni/0.1.0-dev" {
t.Fatal("not the User-Agent we expected")
}
}
func TestUserAgentWithExplicitValue(t *testing.T) {
txp := httptransport.UserAgentTransport{
RoundTripper: httptransport.FakeTransport{
Resp: &http.Response{StatusCode: 200},
},
}
req := &http.Request{URL: &url.URL{
Scheme: "https",
Host: "www.google.com",
Path: "/",
}}
req.Header = http.Header{"User-Agent": []string{"antani-client/0.1.1"}}
resp, err := txp.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
if resp.Request.Header.Get("User-Agent") != "antani-client/0.1.1" {
t.Fatal("not the User-Agent we expected")
}
}

View file

@ -250,7 +250,7 @@ func NewHTTPTransport(config Config) HTTPRoundTripper {
txp = httptransport.SaverTransactionHTTPTransport{
RoundTripper: txp, Saver: config.HTTPSaver}
}
txp = httptransport.UserAgentTransport{RoundTripper: txp}
txp = &httptransport.UserAgentTransport{HTTPTransport: txp}
return txp
}

View file

@ -461,11 +461,11 @@ func TestNewTLSDialerWithNoTLSVerifyAndNoConfig(t *testing.T) {
func TestNewVanilla(t *testing.T) {
txp := netx.NewHTTPTransport(netx.Config{})
uatxp, ok := txp.(httptransport.UserAgentTransport)
uatxp, ok := txp.(*httptransport.UserAgentTransport)
if !ok {
t.Fatal("not the transport we expected")
}
if _, ok := uatxp.RoundTripper.(*http.Transport); !ok {
if _, ok := uatxp.HTTPTransport.(*http.Transport); !ok {
t.Fatal("not the transport we expected")
}
}
@ -518,11 +518,11 @@ func TestNewWithByteCounter(t *testing.T) {
txp := netx.NewHTTPTransport(netx.Config{
ByteCounter: counter,
})
uatxp, ok := txp.(httptransport.UserAgentTransport)
uatxp, ok := txp.(*httptransport.UserAgentTransport)
if !ok {
t.Fatal("not the transport we expected")
}
bctxp, ok := uatxp.RoundTripper.(httptransport.ByteCountingTransport)
bctxp, ok := uatxp.HTTPTransport.(httptransport.ByteCountingTransport)
if !ok {
t.Fatal("not the transport we expected")
}
@ -538,11 +538,11 @@ func TestNewWithLogger(t *testing.T) {
txp := netx.NewHTTPTransport(netx.Config{
Logger: log.Log,
})
uatxp, ok := txp.(httptransport.UserAgentTransport)
uatxp, ok := txp.(*httptransport.UserAgentTransport)
if !ok {
t.Fatal("not the transport we expected")
}
ltxp, ok := uatxp.RoundTripper.(*netxlite.HTTPTransportLogger)
ltxp, ok := uatxp.HTTPTransport.(*netxlite.HTTPTransportLogger)
if !ok {
t.Fatal("not the transport we expected")
}
@ -559,11 +559,11 @@ func TestNewWithSaver(t *testing.T) {
txp := netx.NewHTTPTransport(netx.Config{
HTTPSaver: saver,
})
uatxp, ok := txp.(httptransport.UserAgentTransport)
uatxp, ok := txp.(*httptransport.UserAgentTransport)
if !ok {
t.Fatal("not the transport we expected")
}
stxptxp, ok := uatxp.RoundTripper.(httptransport.SaverTransactionHTTPTransport)
stxptxp, ok := uatxp.HTTPTransport.(httptransport.SaverTransactionHTTPTransport)
if !ok {
t.Fatal("not the transport we expected")
}