-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdefault_handler_test.go
207 lines (158 loc) · 5.42 KB
/
default_handler_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
package cproxy
import (
"net"
"net/http"
"net/http/httptest"
"testing"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestHandlerFixture(t *testing.T) {
gunit.Run(new(HandlerFixture), t)
}
type HandlerFixture struct {
*gunit.Fixture
handler *defaultHandler
filter *TestFilter
socket *DummySocket
clientConnector *TestClientConnector
serverConnector *TestServerConnector
meter *TestMeter
request *http.Request
response *httptest.ResponseRecorder
}
func (this *HandlerFixture) Setup() {
this.filter = NewTestFilter(true)
this.socket = &DummySocket{}
this.clientConnector = NewTestClientConnector(this.socket)
this.serverConnector = NewTestServerConnector()
this.meter = NewTestMeter()
this.handler = newHandler(this.filter, this.clientConnector, this.serverConnector, this.meter)
this.request = httptest.NewRequest("CONNECT", "domain:443", nil)
this.response = httptest.NewRecorder()
}
//////////////////////////////////////////////////////////////
func (this *HandlerFixture) TestForbidsUnknownMethods() {
this.request.Method = "GET"
this.serveHTTP()
this.shouldHaveResponse(405, "Method Not Allowed")
this.So(this.meter.calls, should.Resemble, []int{MeasurementHTTPRequest, MeasurementBadMethod})
}
func (this *HandlerFixture) TestsDisallowsUnauthorizedRequests() {
this.filter.authorized = false
this.serveHTTP()
this.So(this.filter.request, should.Equal, this.request)
this.So(this.filter.response, should.Equal, this.response)
this.shouldHaveResponse(401, "Unauthorized")
this.So(this.meter.calls, should.Resemble, []int{MeasurementHTTPRequest, MeasurementUnauthorizedRequest})
}
func (this *HandlerFixture) TestRejectClientWhichCannotBeConnected() {
this.clientConnector.socket = nil
this.serveHTTP()
this.So(this.clientConnector.response, should.Equal, this.response)
this.shouldHaveResponse(501, "Not Implemented")
this.So(this.meter.calls, should.Resemble, []int{MeasurementHTTPRequest, MeasurementClientConnectionFailed})
}
func (this *HandlerFixture) TestRejectBadGatewayRequest() {
this.serverConnector.proxy = nil
this.serveHTTP()
this.So(this.serverConnector.socket, should.Equal, this.socket)
this.So(this.serverConnector.address, should.Equal, "domain:443")
this.So(this.socket.String(), should.Equal, "HTTP/1.1 502 Bad Gateway\r\n\r\n")
this.So(this.socket.closed, should.Equal, 1)
this.shouldHaveResponse(200, "") // ResponseRecorder defaults to 200
this.So(this.meter.calls, should.Resemble, []int{MeasurementHTTPRequest, MeasurementServerConnectionFailed})
}
func (this *HandlerFixture) TestProxyInvoked() {
this.serveHTTP()
this.So(this.socket.String(), should.Equal, "HTTP/1.1 200 OK\r\n\r\n")
this.So(this.serverConnector.proxy.calls, should.Equal, 1)
this.shouldHaveResponse(200, "") // ResponseRecorder defaults to 200
this.So(this.meter.calls, should.Resemble, []int{MeasurementHTTPRequest, MeasurementProxyReady, MeasurementProxyComplete})
}
func (this *HandlerFixture) serveHTTP() {
this.handler.ServeHTTP(this.response, this.request)
}
func (this *HandlerFixture) shouldHaveResponse(statusCode int, statusText string) {
this.So(this.response.Code, should.Equal, statusCode)
this.So(this.response.Body.String(), should.EqualTrimSpace, statusText)
}
//////////////////////////////////////////////////////////////
type TestFilter struct {
authorized bool
request *http.Request
response http.ResponseWriter
}
func NewTestFilter(authorized bool) *TestFilter {
return &TestFilter{authorized: authorized}
}
func (this *TestFilter) IsAuthorized(response http.ResponseWriter, request *http.Request) bool {
this.response = response
this.request = request
return this.authorized
}
//////////////////////////////////////////////////////////////
type TestClientConnector struct {
socket Socket
response http.ResponseWriter
}
func NewTestClientConnector(socket Socket) *TestClientConnector {
return &TestClientConnector{socket: socket}
}
func (this *TestClientConnector) Connect(response http.ResponseWriter) Socket {
this.response = response
return this.socket
}
//////////////////////////////////////////////////////////////
type TestServerConnector struct {
socket Socket
address string
proxy *TestProxy
}
func NewTestServerConnector() *TestServerConnector {
return &TestServerConnector{proxy: &TestProxy{}}
}
func (this *TestServerConnector) Connect(socket Socket, address string) proxy {
this.socket = socket
this.address = address
if this.proxy == nil {
return nil // Golang nil != nil issue
}
return this.proxy
}
type TestProxy struct{ calls int }
func (this *TestProxy) Proxy() {
if this != nil {
this.calls++
}
}
//////////////////////////////////////////////////////////////
type DummySocket struct {
written []byte
closed int
}
func (this *DummySocket) Read([]byte) (int, error) {
panic("this test shouldn't read")
}
func (this *DummySocket) Write(buffer []byte) (int, error) {
this.written = append(this.written, buffer...)
return len(buffer), nil
}
func (this *DummySocket) Close() error {
this.closed++
return nil
}
func (this *DummySocket) RemoteAddr() net.Addr {
panic("shouldn't be called")
}
func (this *DummySocket) String() string { return string(this.written) }
//////////////////////////////////////////////////////////////
type TestMeter struct {
calls []int
}
func NewTestMeter() *TestMeter {
return &TestMeter{}
}
func (this *TestMeter) Measure(value int) {
this.calls = append(this.calls, value)
}