Skip to content

Commit 3b85c07

Browse files
committed
Add unit tests for upstream.go
Signed-off-by: MJ Kim <mjkim610@gmail.com>
1 parent edef8a2 commit 3b85c07

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

upstream_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package proxify
2+
3+
import (
4+
"net/url"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestNewHTTPProxyRoundRobinDialer(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
upstreamProxies []string
13+
shouldThrowErr bool
14+
}{
15+
{
16+
name: "empty",
17+
upstreamProxies: []string{},
18+
shouldThrowErr: true,
19+
},
20+
{
21+
name: "one",
22+
upstreamProxies: []string{"http://localhost:7777"},
23+
shouldThrowErr: false,
24+
},
25+
{
26+
name: "multiple",
27+
upstreamProxies: []string{"http://localhost:7777", "http://localhost:9999"},
28+
shouldThrowErr: false,
29+
},
30+
{
31+
name: "invalid",
32+
upstreamProxies: []string{"http://:invalid"},
33+
shouldThrowErr: true,
34+
},
35+
}
36+
for _, test := range tests {
37+
t.Run(test.name, func(t *testing.T) {
38+
actual, actualErr := newHTTPProxyRoundRobinDialer(test.upstreamProxies)
39+
if (actualErr != nil) != test.shouldThrowErr {
40+
t.Errorf("newHTTPProxyRoundRobinDialer() actualErr = %v, shouldThrowErr = %v", actualErr, test.shouldThrowErr)
41+
return
42+
}
43+
if !test.shouldThrowErr && actual == nil {
44+
t.Errorf("newHTTPProxyRoundRobinDialer() actual = %v, expected non-nil", actual)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestNewSOCKS5ProxyRoundRobinDialer(t *testing.T) {
51+
tests := []struct {
52+
name string
53+
upstreamProxies []string
54+
shouldThrowErr bool
55+
}{
56+
{
57+
name: "empty",
58+
upstreamProxies: []string{},
59+
shouldThrowErr: true,
60+
},
61+
{
62+
name: "one",
63+
upstreamProxies: []string{"socks5://localhost:10070"},
64+
shouldThrowErr: false,
65+
},
66+
{
67+
name: "multiple",
68+
upstreamProxies: []string{"socks5://localhost:10070", "socks5://localhost:10090"},
69+
shouldThrowErr: false,
70+
},
71+
{
72+
name: "invalid",
73+
upstreamProxies: []string{"socks5://:invalid"},
74+
shouldThrowErr: true,
75+
},
76+
{
77+
name: "auth",
78+
upstreamProxies: []string{"socks5://user:pass@localhost:10070"},
79+
shouldThrowErr: false,
80+
},
81+
}
82+
for _, test := range tests {
83+
t.Run(test.name, func(t *testing.T) {
84+
actual, actualErr := newSOCKS5ProxyRoundRobinDialer(test.upstreamProxies)
85+
if (actualErr != nil) != test.shouldThrowErr {
86+
t.Errorf("newSOCKS5ProxyRoundRobinDialer() actualErr = %v, shouldThrowErr = %v", actualErr, test.shouldThrowErr)
87+
return
88+
}
89+
if !test.shouldThrowErr && actual == nil {
90+
t.Errorf("newSOCKS5ProxyRoundRobinDialer() actual = %v, expected non-nil", actual)
91+
}
92+
})
93+
}
94+
}
95+
96+
func TestToStringSlice(t *testing.T) {
97+
tests := []struct {
98+
name string
99+
urls []*url.URL
100+
expected []string
101+
}{
102+
{
103+
name: "single",
104+
urls: []*url.URL{{Scheme: "http", Host: "localhost:8080"}},
105+
expected: []string{"http://localhost:8080"},
106+
},
107+
{
108+
name: "multiple",
109+
urls: []*url.URL{
110+
{Scheme: "http", Host: "localhost:8080"},
111+
{Scheme: "socks5", User: url.UserPassword("user", "pass"), Host: "localhost:8081"},
112+
},
113+
expected: []string{"http://localhost:8080", "socks5://user:pass@localhost:8081"},
114+
},
115+
{
116+
name: "empty",
117+
urls: []*url.URL{},
118+
expected: []string{},
119+
},
120+
}
121+
for _, test := range tests {
122+
t.Run(test.name, func(t *testing.T) {
123+
if actual := toStringSlice(test.urls); !reflect.DeepEqual(actual, test.expected) {
124+
t.Errorf("toStringSlice() actual = %v, expected = %v", actual, test.expected)
125+
}
126+
})
127+
}
128+
}

0 commit comments

Comments
 (0)