forked from getlantern/http-proxy-archived
/
main_test.go
143 lines (121 loc) · 3.32 KB
/
main_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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
"testing"
"github.com/getlantern/testify/assert"
)
func TestCONNECT(t *testing.T) {
connectReq := "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n"
tunneledReq := "GET / HTTP/1.1\r\n\r\n"
connectResp := "HTTP/1.1 200 OK\r\n"
tunneledResp := "holla!\n"
defer stopMockServers()
site, _ := newMockServer("holla!")
u, _ := url.Parse(site)
log.Debugf("Started target site at %s", u)
addr, _ := startServer(t)
log.Debugf("Started proxy server at %s", addr)
con, err := net.Dial("tcp", addr.String())
if !assert.NoError(t, err, "should dial proxy server") {
return
}
defer func() {
assert.NoError(t, con.Close(), "should close connection")
}()
req := fmt.Sprintf(connectReq, u.Host, u.Host)
_, err = con.Write([]byte(req))
if !assert.NoError(t, err, "should write CONNECT request") {
return
}
var buf [400]byte
_, err = con.Read(buf[:])
if !assert.Contains(t, string(buf[:]), connectResp, "should get 200 OK for CONNECT") {
return
}
_, err = con.Write([]byte(tunneledReq))
if !assert.NoError(t, err, "should write tunneled data") {
return
}
_, err = con.Read(buf[:])
assert.Contains(t, string(buf[:]), tunneledResp, "should read tunneled response")
}
func TestGET(t *testing.T) {
log.Debugf("TestGET")
reqTemplate := "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n"
resp := "HTTP/1.1 200 OK\r\n"
content := "holla!\n"
defer stopMockServers()
site, _ := newMockServer(content)
u, _ := url.Parse(site)
log.Debugf("Started target site at %s", u)
addr, _ := startServer(t)
log.Debugf("Started proxy server at %s", addr)
con, err := net.Dial("tcp", addr.String())
if !assert.NoError(t, err, "should dial proxy server") {
return
}
defer func() {
assert.NoError(t, con.Close(), "should close connection")
}()
req := fmt.Sprintf(reqTemplate, u, u.Host)
_, err = con.Write([]byte(req))
if !assert.NoError(t, err, "should write request") {
return
}
var buf [400]byte
_, err = con.Read(buf[:])
if !assert.Contains(t, string(buf[:]), resp, "should get 200 OK for GET") {
return
}
assert.Contains(t, string(buf[:]), content, "should read content")
}
func TestAuth(t *testing.T) {
log.Debugf("TestGET")
reqTemplate := "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n"
resp := "HTTP/1.1 404 Not Found\r\n"
content := "holla!\n"
defer stopMockServers()
site, _ := newMockServer(content)
u, _ := url.Parse(site)
log.Debugf("Started target site at %s", u)
addr, s := startServer(t)
s.Checker = func(req *http.Request) error {
return errors.New("dsafda")
}
log.Debugf("Started proxy server at %s", addr)
con, err := net.Dial("tcp", addr.String())
if !assert.NoError(t, err, "should dial proxy server") {
return
}
defer func() {
assert.NoError(t, con.Close(), "should close connection")
}()
req := fmt.Sprintf(reqTemplate, u, u.Host)
_, err = con.Write([]byte(req))
if !assert.NoError(t, err, "should write request") {
return
}
var buf [400]byte
_, err = con.Read(buf[:])
assert.Contains(t, string(buf[:]), resp, "should get 404 Not Found")
}
func startServer(t *testing.T) (addr net.Addr, s *Server) {
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Unable to listen: %s", err)
}
s = &Server{
Dial: net.Dial,
}
go func() {
err := s.Serve(l)
if err != nil {
t.Fatalf("Unable to serve: %s", err)
}
}()
return l.Addr(), s
}