-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxy_test.go
147 lines (114 loc) · 2.62 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
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
package gpac_test
import (
"io"
"io/ioutil"
"log"
"net/http"
"testing"
"github.com/darren/gpac"
)
func init() {
var mux http.ServeMux
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Example")
})
go func() {
log.Fatal(http.ListenAndServe("127.0.0.1:8080", &mux))
}()
go func() {
log.Fatal(http.ListenAndServe("127.0.0.1:8081", &mux))
}()
}
func TestParseProxy(t *testing.T) {
proxy := "PROXY 127.0.0.1:8080; SOCKs 127.0.0.1:1080; Direct"
proxies := gpac.ParseProxy(proxy)
if len(proxies) != 3 {
t.Error("Parse failed")
return
}
if proxies[1].Type != "SOCKS" {
t.Error("Should be SOCKS5")
}
if !proxies[1].IsSOCKS() {
t.Error("Should be SOCKS5")
}
if !proxies[2].IsDirect() {
t.Error("Should be direct")
}
}
func TestParseSOCKS(t *testing.T) {
proxy := "SOCKS5 127.0.0.1:1080"
proxies := gpac.ParseProxy(proxy)
if len(proxies) != 1 {
t.Error("Parse failed")
return
}
if !proxies[0].IsSOCKS() {
t.Error("Should be SOCKS5")
}
if proxies[0].IsDirect() {
t.Error("Should be direct")
}
}
func readBodyAndClose(resp *http.Response) string {
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
return string(buf)
}
func testProxyGet(t *testing.T, typ string) {
t.Logf("Test proxy type: %s", typ)
var p = gpac.Proxy{Type: typ, Address: "127.0.0.1:8080"}
client := http.Client{
Transport: &http.Transport{
Proxy: p.Proxy(),
},
}
resp, err := client.Get("http://127.0.0.1:8081")
if err != nil {
t.Fatal(err)
}
body := readBodyAndClose(resp)
if body != "Example" {
t.Errorf("Response not expected: %s", body)
}
}
func testClientGet(t *testing.T, typ string) {
t.Logf("Test Client proxy type: %s", typ)
var p = gpac.Proxy{Type: typ, Address: "127.0.0.1:8080"}
resp, err := p.Get("http://127.0.0.1:8081")
if err != nil {
t.Fatal(err)
}
body := readBodyAndClose(resp)
if body != "Example" {
t.Errorf("Response not expected: %s", body)
}
}
func testTransport(t *testing.T, typ string) {
t.Logf("Test Transport proxy type: %s", typ)
var p = gpac.Proxy{Type: typ, Address: "127.0.0.1:8080"}
req, err := http.NewRequest("GET", "http://127.0.0.1:8081", nil)
if err != nil {
t.Fatal(err)
}
resp, err := p.Transport().RoundTrip(req)
if err != nil {
t.Fatal(err)
}
body := readBodyAndClose(resp)
if body != "Example" {
t.Errorf("Response not expected: %s", body)
}
}
func TestMultiProxyGet(t *testing.T) {
//BUG: SOCKS5 seems not work
knownTypes := []string{"DIRECT", "HTTP"}
for _, typ := range knownTypes {
testProxyGet(t, typ)
testClientGet(t, typ)
testTransport(t, typ)
}
}