refactor: introduce factory for stdlib http transport (#413)
With this factory, we want to construct ourselves the TLS dialer so that we can use a dialer wrapper that always sets timeouts when reading, addressing https://github.com/ooni/probe/issues/1609. As a result, we cannot immediately replace the i/e/netx factory for creating a new HTTP transport, since the functions signatures are not directly compatible. Refactoring is part of https://github.com/ooni/probe/issues/1505.
This commit is contained in:
parent
f59e98fd05
commit
6895946a34
5 changed files with 115 additions and 3 deletions
|
|
@ -2,8 +2,10 @@ package netxlite
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
|
@ -106,3 +108,43 @@ func TestHTTPTransportLoggerCloseIdleConnections(t *testing.T) {
|
|||
t.Fatal("not called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPTransportWorks(t *testing.T) {
|
||||
d := &DialerResolver{
|
||||
Dialer: DefaultDialer,
|
||||
Resolver: &net.Resolver{},
|
||||
}
|
||||
th := &TLSHandshakerConfigurable{}
|
||||
txp := NewHTTPTransport(d, &tls.Config{}, th)
|
||||
client := &http.Client{Transport: txp}
|
||||
resp, err := client.Get("https://www.google.com/robots.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
txp.CloseIdleConnections()
|
||||
}
|
||||
|
||||
func TestHTTPTransportWithFailingDialer(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
d := &DialerResolver{
|
||||
Dialer: &netxmocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context,
|
||||
network, address string) (net.Conn, error) {
|
||||
return nil, expected
|
||||
},
|
||||
},
|
||||
Resolver: &net.Resolver{},
|
||||
}
|
||||
th := &TLSHandshakerConfigurable{}
|
||||
txp := NewHTTPTransport(d, &tls.Config{}, th)
|
||||
client := &http.Client{Transport: txp}
|
||||
resp, err := client.Get("https://www.google.com/robots.txt")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if resp != nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
txp.CloseIdleConnections()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue