-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxymanager_test.go
209 lines (185 loc) · 4.66 KB
/
proxymanager_test.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package proxymanager_test
import (
"testing"
"fmt"
"github.com/s4l1h/proxymanager"
)
func ExampleProxy_String() {
p := proxymanager.Proxy{
Type: "http",
Host: "10.0.0.1",
Port: "1080",
Username: "username",
Password: "password",
}
fmt.Println(p)
// Output: http://username:password@10.0.0.1:1080
}
func ExampleManager_Has() {
u := "http://1.1.1.1:1010"
p := proxymanager.Proxy{
Host: "10.0.0.1",
Port: "1080",
}
plist := proxymanager.New(3)
plist.AddFromURL(u)
if plist.Has(u) == true {
fmt.Printf("%s Exists", u)
}
if plist.Has("http://proxyhost:333") == true {
fmt.Println("http://proxyhost:333 Exists")
}
if plist.Has(p) == true {
fmt.Printf("Exists %s:%s", p.Host, p.Port)
}
// Output: http://1.1.1.1:1010 Exists
}
func TestAddFromURL(t *testing.T) {
u := "socks5://username:password@host:1020"
plist := proxymanager.New(3)
plist.AddFromURL(u)
url := plist.GiveMeProxyURL().String()
if url != u {
t.Errorf("AddFromURL error %s!=%s", url, u)
}
}
func ExampleManager_AddFromURL() {
plist := proxymanager.New(3)
plist.AddFromURL("http://username:password@192.168.1.11:1020")
proxy := plist.GiveMeProxy()
fmt.Printf("Host:%s Port:%s Username:%s Password:%s Type:%s", proxy.Host, proxy.Port, proxy.Username, proxy.Password, proxy.Type)
// Output: Host:192.168.1.11 Port:1020 Username:username Password:password Type:http
}
func ExampleManager_GiveMeProxyURL() {
plist := proxymanager.New(3)
plist.Add(proxymanager.Proxy{
Host: "192.168.1.11",
Port: "1000",
Username: "user",
Password: "pass",
})
url := plist.GiveMeProxyURL()
fmt.Println(url)
// Output: http://user:pass@192.168.1.11:1000
}
func ExampleManager_Add() {
plist := proxymanager.New(3)
plist.Add(proxymanager.Proxy{
Host: "192.168.1.11",
Port: "1000",
Username: "none",
Password: "none",
})
proxy := plist.GiveMeProxy()
fmt.Println(proxy.Host)
// Output: 192.168.1.11
}
func ExampleManager_Remove() {
plist := proxymanager.New(2)
plist.Add(proxymanager.Proxy{
Host: "192.168.1.10",
Port: "1000",
Username: "none",
Password: "none",
})
plist.Add(proxymanager.Proxy{
Host: "192.168.1.11",
Port: "1000",
Username: "none",
Password: "none",
})
plist.Add(proxymanager.Proxy{
Host: "192.168.1.12",
Port: "1000",
Username: "none",
Password: "none",
})
plist.Remove("192.168.1.12")
plist.Remove(proxymanager.Proxy{Host: "192.168.11"})
}
func TestProxy(t *testing.T) {
example := []string{
"192.168.1.100",
"192.168.1.101",
"192.168.1.102",
}
total := len(example)
limit := 3
plist := proxymanager.New(limit)
for _, p := range example {
plist.Add(
proxymanager.Proxy{
Host: p,
Port: "1000",
Username: "none",
Password: "none",
})
}
if plist.WriteIndex != total {
t.Errorf("WriteIndex Error: %d!=%d", plist.WriteIndex, total)
}
if len(plist.List) != total {
t.Errorf("List size error")
}
if plist.Limit != limit {
t.Errorf("Limit Error")
}
if plist.StepIndex != 0 {
t.Errorf("StepIndex error")
}
if plist.ReadIndex != 0 {
t.Errorf("ReadIndex error")
}
proxy := plist.GiveMeProxy()
if proxy.Host != example[0] {
t.Errorf("GiveMeProxy return wrong proxy")
}
if plist.ReadIndex != 0 {
t.Errorf("ReadIndex error %d", plist.ReadIndex)
}
if plist.StepIndex != 1 {
t.Errorf("StepIndex error %d", plist.StepIndex)
}
proxy = plist.GiveMeProxy()
proxy = plist.GiveMeProxy()
proxy = plist.GiveMeProxy() // index 1 step 1
if proxy.Host != example[1] {
t.Errorf("GiveMeProxy return wrong proxt")
}
if plist.ReadIndex != 1 {
t.Errorf("ReadIndex error %d", plist.ReadIndex)
}
if plist.StepIndex != 1 {
t.Errorf("StepIndex error %d", plist.StepIndex)
}
proxy = plist.GiveMeProxy() // index 1 step 2
proxy = plist.GiveMeProxy() // index 1 step 3
proxy = plist.GiveMeProxy() // index 2 step 1
if proxy.Host != example[2] {
t.Errorf("GiveMeProxy return wrong proxt")
}
if plist.ReadIndex != 2 {
t.Errorf("ReadIndex error %d", plist.ReadIndex)
}
if plist.StepIndex != 1 {
t.Errorf("StepIndex error %d", plist.StepIndex)
}
proxy = plist.GiveMeProxy() // index 2 step 2
proxy = plist.GiveMeProxy() // index 2 step 3
if plist.ReadIndex != plist.WriteIndex {
t.Errorf("ReadIndex error %d", plist.ReadIndex)
t.Errorf("WriteIndex error %d", plist.WriteIndex)
}
proxy = plist.GiveMeProxy() // index 0 step 1
if plist.ReadIndex != 0 {
t.Errorf("ReadIndex error %d", plist.ReadIndex)
}
plist.Remove("192.168.1.100")
if plist.WriteIndex != 2 {
t.Errorf("WriteIndex error %d", plist.WriteIndex)
}
plist.Remove(proxymanager.Proxy{Host: "192.168.1.101"})
if plist.WriteIndex != 1 {
t.Errorf("WriteIndex error %d", plist.WriteIndex)
}
}