refactor(netxlite): more abstract proxy-enabled dialer construction (#812)
This will help with https://github.com/ooni/probe/issues/2135
This commit is contained in:
parent
bf7ea423d3
commit
1a706e47bc
4 changed files with 42 additions and 33 deletions
|
|
@ -12,28 +12,34 @@ import (
|
|||
)
|
||||
|
||||
func TestMaybeProxyDialer(t *testing.T) {
|
||||
t.Run("DialContext", func(t *testing.T) {
|
||||
t.Run("missing proxy URL", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
d := &MaybeProxyDialer{
|
||||
Dialer: &mocks.Dialer{MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
return nil, expected
|
||||
}},
|
||||
ProxyURL: nil,
|
||||
}
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if conn != nil {
|
||||
t.Fatal("conn is not nil")
|
||||
t.Run("MaybeWrapWithProxyDialer", func(t *testing.T) {
|
||||
t.Run("without a proxy URL", func(t *testing.T) {
|
||||
underlying := &mocks.Dialer{}
|
||||
dialer := MaybeWrapWithProxyDialer(underlying, nil)
|
||||
if dialer != underlying {
|
||||
t.Fatal("should not have wrapped")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with a proxy URL", func(t *testing.T) {
|
||||
URL := &url.URL{}
|
||||
underlying := &mocks.Dialer{}
|
||||
dialer := MaybeWrapWithProxyDialer(underlying, URL)
|
||||
real := dialer.(*proxyDialer)
|
||||
if real.Dialer != underlying {
|
||||
t.Fatal("did not wrap correctly")
|
||||
}
|
||||
if real.ProxyURL != URL {
|
||||
t.Fatal("invalid URL")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("DialContext", func(t *testing.T) {
|
||||
t.Run("invalid scheme", func(t *testing.T) {
|
||||
child := &mocks.Dialer{}
|
||||
URL := &url.URL{Scheme: "antani"}
|
||||
d := NewMaybeProxyDialer(child, URL)
|
||||
d := MaybeWrapWithProxyDialer(child, URL)
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
|
||||
if !errors.Is(err, ErrProxyUnsupportedScheme) {
|
||||
t.Fatal("not the error we expected")
|
||||
|
|
@ -45,7 +51,7 @@ func TestMaybeProxyDialer(t *testing.T) {
|
|||
|
||||
t.Run("underlying dial fails with EOF", func(t *testing.T) {
|
||||
const expect = "10.0.0.1:9050"
|
||||
d := &MaybeProxyDialer{
|
||||
d := &proxyDialer{
|
||||
Dialer: &mocks.Dialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
if address != expect {
|
||||
|
|
@ -77,7 +83,7 @@ func TestMaybeProxyDialer(t *testing.T) {
|
|||
},
|
||||
}
|
||||
URL := &url.URL{}
|
||||
dialer := NewMaybeProxyDialer(child, URL)
|
||||
dialer := MaybeWrapWithProxyDialer(child, URL)
|
||||
dialer.CloseIdleConnections()
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
|
|
|
|||
Loading…
Reference in a new issue