Skip to content

Commit

Permalink
fix(tests): simplify Dial function with mock Dialer
Browse files Browse the repository at this point in the history
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
ryanbekhen committed Dec 8, 2024
1 parent 113b1f6 commit 3deb135
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions pkg/tor/dial_test.go
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")
}

0 comments on commit 3deb135

Please sign in to comment.