-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tests): simplify Dial function with mock Dialer
Refactor the test suite to use a MockDialer, replacing previous SOCKS5 error simulation. This streamlines the testing process and removes dependencies on other modules, making the tests cleaner and more focused.
- Loading branch information
1 parent
113b1f6
commit 3deb135
Showing
1 changed file
with
21 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
package tor | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/net/proxy" | ||
"errors" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestDial(t *testing.T) { | ||
network := "tcp" | ||
addr := "example.com:80" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
conn, err := DefaultDialer{}.Dial(network, addr) | ||
assert.Nil(t, err) | ||
defer conn.Close() | ||
assert.NotNil(t, conn) | ||
} | ||
type MockDialer struct{} | ||
|
||
func TestDial_Error(t *testing.T) { | ||
originalSOCKS5 := customSOCKS5 | ||
customSOCKS5 = func(network, address string, auth *proxy.Auth, forward proxy.Dialer) (proxy.Dialer, error) { | ||
return nil, fmt.Errorf("simulated SOCKS5 error") | ||
func (MockDialer) Dial(network, address string) (net.Conn, error) { | ||
if address == "fail" { | ||
return nil, errors.New("simulated failure") | ||
} | ||
defer func() { customSOCKS5 = originalSOCKS5 }() | ||
return &net.TCPConn{}, nil | ||
} | ||
|
||
network := "tcp" | ||
addr := "example.com:80" | ||
conn, err := DefaultDialer{}.Dial(network, addr) | ||
func TestDial_Success(t *testing.T) { | ||
dialer := MockDialer{} | ||
conn, err := dialer.Dial("tcp", "example.com:80") | ||
assert.Nil(t, err, "expected no error while dialing") | ||
assert.NotNil(t, conn, "expected a connection to be obtained") | ||
} | ||
|
||
assert.NotNil(t, err, "expected an error when dialing with simulated SOCKS5 error") | ||
assert.Nil(t, conn, "expected no connection to be returned on error") | ||
func TestDial_Error(t *testing.T) { | ||
dialer := MockDialer{} | ||
conn, err := dialer.Dial("tcp", "fail") | ||
assert.NotNil(t, err, "expected an error when dialing") | ||
assert.Nil(t, conn, "expected no connection on error") | ||
} |