-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection_test.go
175 lines (143 loc) · 4.44 KB
/
connection_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
package main
import (
"io"
"net"
"net/http"
"strings"
"testing"
)
func createHttpServer(address, payload string, code int) *http.Server {
mux := http.NewServeMux()
server := &http.Server{Addr: address, Handler: mux}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Date", "FAKE")
w.WriteHeader(code)
w.Write([]byte(payload))
})
listener, err := net.Listen("tcp", address)
if err != nil {
panic(err)
}
go server.Serve(listener)
return server
}
func resetCredentials() {
config.Credentials = []Credential{}
}
func setCredentials(user, pass string) {
config.Credentials = []Credential{{Username: user, Password: pass}}
}
func basicHttpProxyRequest() string {
return "GET http://httpbin.org/headers HTTP/1.1\r\nHost: httpbin.org\r\n\r\n"
}
func readMessage(reader io.Reader) string {
buffer := make([]byte, 1024)
reader.Read(buffer)
response := strings.TrimRight(string(buffer), "\x000")
return response
}
func TestMain(m *testing.M) {
config = &Config{}
InitNullLogger()
m.Run()
}
func TestInvalidCredentials(t *testing.T) {
setCredentials("test", "hello")
defer resetCredentials()
incoming := NewMockConn()
defer incoming.CloseClient()
conn := NewConnection(incoming)
go conn.Handle()
incoming.ClientWriter.Write([]byte(basicHttpProxyRequest()))
buffer := make([]byte, 100)
incoming.ClientReader.Read(buffer)
response := strings.TrimRight(string(buffer), "\x000")
expected := "HTTP/1.0 407 Proxy authentication required\r\n\r\n"
if response != expected {
t.Fatalf("Expected '%s' but got '%s'", expected, response)
}
}
func TestSampleProxy(t *testing.T) {
server := createHttpServer("localhost:9000", "testing 123", 200)
defer func() {
if err := server.Close(); err != nil {
panic(err)
}
}()
cleanedUp := make(chan bool)
incoming := NewMockConn()
defer incoming.CloseClient()
conn := NewConnection(incoming)
go func() {
conn.Handle()
cleanedUp <- true
}()
request := "GET http://localhost:9000/ HTTP/1.1\r\nHost: localhost\r\n\r\n"
incoming.ClientWriter.Write([]byte(request))
response := readMessage(incoming.ClientReader)
expected_response := "HTTP/1.1 200 OK\r\nDate: FAKE\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n\r\ntesting 123"
if response != expected_response {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}
incoming.CloseClient()
<-cleanedUp
}
func TestSampleProxyWithValidAuthCredentials(t *testing.T) {
server := createHttpServer("localhost:9000", "testing 123", 200)
defer func() {
if err := server.Close(); err != nil {
panic(err)
}
}()
cleanedUp := make(chan bool)
incoming := NewMockConn()
conn := NewConnection(incoming)
go func() {
conn.Handle()
cleanedUp <- true
}()
setCredentials("test", "yolo")
defer resetCredentials()
request := "GET http://localhost:9000/ HTTP/1.1\r\nProxy-Authorization: Basic dGVzdDp5b2xv\r\nHost: localhost\r\n\r\n"
incoming.ClientWriter.Write([]byte(request))
response := readMessage(incoming.ClientReader)
expected_response := "HTTP/1.1 200 OK\r\nDate: FAKE\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n\r\ntesting 123"
if response != expected_response {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}
incoming.CloseClient()
<-cleanedUp
}
func TestSampleProxyViaConnect(t *testing.T) {
server := createHttpServer("localhost:9000", "testing 123", 200)
defer func() {
if err := server.Close(); err != nil {
panic(err)
}
}()
cleanedUp := make(chan bool)
incoming := NewMockConn()
conn := NewConnection(incoming)
go func() {
conn.Handle()
cleanedUp <- true
}()
// Mimic a TLS connect here
connect_request := "CONNECT localhost:9000 HTTP/1.1\r\nnHost: localhost:9000\r\n\r\n"
incoming.ClientWriter.Write([]byte(connect_request))
response := readMessage(incoming.ClientReader)
expected_response := "HTTP/1.0 200 Connection established\r\n\r\n"
if response != expected_response {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}
// Mimic a regular http request
request := "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"
incoming.ClientWriter.Write([]byte(request))
response = readMessage(incoming.ClientReader)
expected_response = "HTTP/1.1 200 OK\r\nDate: FAKE\r\nContent-Length: 11\r\nContent-Type: text/plain; charset=utf-8\r\n\r\ntesting 123"
if response != expected_response {
t.Fatalf("Expected '%s' but got '%s'", expected_response, response)
}
incoming.CloseClient()
<-cleanedUp
}