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