-
Notifications
You must be signed in to change notification settings - Fork 9
/
proxy_test.go
99 lines (93 loc) · 2.15 KB
/
proxy_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
package scrapemate_test
import (
"testing"
"github.com/gosom/scrapemate"
"github.com/stretchr/testify/require"
)
func TestNewProxy(t *testing.T) {
tests := []struct {
name string
input string
expected scrapemate.Proxy
expectError bool
}{
{
name: "full socks5 url with credentials",
input: "socks5://user:[email protected]:1080",
expected: scrapemate.Proxy{
URL: "socks5://example.com:1080",
Username: "user",
Password: "pass",
},
expectError: false,
},
{
name: "http proxy without credentials",
input: "http://example.com:8080",
expected: scrapemate.Proxy{
URL: "http://example.com:8080",
Username: "",
Password: "",
},
expectError: false,
},
{
name: "default to socks5 when no scheme",
input: "user:[email protected]:1080",
expected: scrapemate.Proxy{
URL: "socks5://example.com:1080",
Username: "user",
Password: "pass",
},
expectError: false,
},
{
name: "only host and port defaults to socks5",
input: "example.com:1080",
expected: scrapemate.Proxy{
URL: "socks5://example.com:1080",
Username: "",
Password: "",
},
expectError: false,
},
{
name: "username only without password",
input: "socks5://[email protected]:1080",
expected: scrapemate.Proxy{
URL: "socks5://example.com:1080",
Username: "user",
Password: "",
},
expectError: false,
},
{
name: "empty password after colon",
input: "socks5://user:@example.com:1080",
expected: scrapemate.Proxy{
URL: "socks5://example.com:1080",
Username: "user",
Password: "",
},
expectError: false,
},
{
name: "invalid scheme",
input: "ftp://user:[email protected]:1080",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proxy, err := scrapemate.NewProxy(tt.input)
if tt.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.expected.URL, proxy.URL)
require.Equal(t, tt.expected.Username, proxy.Username)
require.Equal(t, tt.expected.Password, proxy.Password)
})
}
}