-
Notifications
You must be signed in to change notification settings - Fork 9
/
server_test.go
232 lines (182 loc) · 5.93 KB
/
server_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package httransform_test
import (
"context"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/9seconds/httransform/v2"
"github.com/9seconds/httransform/v2/auth"
"github.com/9seconds/httransform/v2/layers"
"github.com/mccutchen/go-httpbin/httpbin"
"github.com/stretchr/testify/suite"
)
var caCert = []byte(`-----BEGIN CERTIFICATE-----
MIICWzCCAcSgAwIBAgIJAJ34yk7oiKv5MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTgxMjAyMTQyNTAyWhcNMjgxMTI5MTQyNTAyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0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hosi3/txxrc8VmBAkCQQDaS651dpQ3TRE//raZ
s79ZBEdMCMlgXB6CPrZpvLz/3ZPcLih4MJ59oVkeFHCNct7ccQcQu4XHMGNBIRBh
kpvzAkEAlS/AjHC7T0y/O052upJ2jLweBqBtHaj6foFE6qIVDugOYp8BdXw/5s+x
GsrJ22+49Z0pi2mk3jVMUhpmWprNoQJBANdAT0v2XFpXfQ38bTQMYT82j9Myytdg
npjRm++Rs1AdvoIbZb52OqIoqoaVoxJnVchLD6t5LYXnecesAcok1e8CQEKB7ycJ
6yVwnBE3Ua9CHcGmrre6HmEWdPy1Zyb5DQC6duX46zEBzti9oWx0DJIQRZifeCvw
4J45NsSQjuuAAWs=
-----END PRIVATE KEY-----`)
type ServerTestSuite struct {
suite.Suite
httpEndpoint *httptest.Server
tlsEndpoint *httptest.Server
proxy *httransform.Server
ln net.Listener
ctx context.Context
ctxCancel context.CancelFunc
http *http.Client
}
func (suite *ServerTestSuite) SetupSuite() {
httpbinApp := httpbin.NewHTTPBin()
mux := http.NewServeMux()
mux.HandleFunc("/ip", httpbinApp.IP)
suite.httpEndpoint = httptest.NewServer(mux)
suite.tlsEndpoint = httptest.NewTLSServer(mux)
}
func (suite *ServerTestSuite) TearDownSuite() {
suite.httpEndpoint.Close()
suite.tlsEndpoint.Close()
}
func (suite *ServerTestSuite) SetupTest() {
suite.ctx, suite.ctxCancel = context.WithCancel(context.Background())
opts := httransform.ServerOpts{
Authenticator: auth.NewBasicAuth(map[string]string{
"user": "password",
}),
TLSCertCA: caCert,
TLSPrivateKey: caPrivateKey,
Layers: []layers.Layer{
layers.TimeoutLayer{
Timeout: 10 * time.Second,
},
},
}
suite.proxy, _ = httransform.NewServer(suite.ctx, opts)
suite.ln, _ = net.Listen("tcp", "127.0.0.1:0")
go suite.proxy.Serve(suite.ln)
httpProxyURL, _ := url.Parse("http://" + suite.ln.Addr().String())
httpProxyURL.User = url.UserPassword("user", "password")
suite.http = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(httpProxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: time.Second,
}
}
func (suite *ServerTestSuite) TearDownTest() {
suite.ctxCancel()
suite.proxy.Close()
suite.ln.Close()
}
func (suite *ServerTestSuite) TestIncorrectCACert() {
opts := httransform.ServerOpts{
TLSCertCA: []byte{1, 2, 3},
TLSPrivateKey: caPrivateKey,
}
_, err := httransform.NewServer(suite.ctx, opts)
suite.Error(err)
}
func (suite *ServerTestSuite) TestIncorrectPrivateKey() {
opts := httransform.ServerOpts{
TLSCertCA: caCert,
TLSPrivateKey: []byte{1, 2, 3},
}
_, err := httransform.NewServer(suite.ctx, opts)
suite.Error(err)
}
func (suite *ServerTestSuite) TestHTTPRequest() {
resp, err := suite.http.Get(suite.httpEndpoint.URL + "/ip")
defer func() {
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}()
suite.NoError(err)
data, err := ioutil.ReadAll(resp.Body)
suite.NoError(err)
v := map[string]interface{}{}
suite.NoError(json.Unmarshal(data, &v))
}
func (suite *ServerTestSuite) TestHTTPSRequest() {
resp, err := suite.http.Get(suite.tlsEndpoint.URL + "/ip")
defer func() {
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}()
suite.NoError(err)
data, err := ioutil.ReadAll(resp.Body)
suite.NoError(err)
v := map[string]interface{}{}
suite.NoError(json.Unmarshal(data, &v))
}
func (suite *ServerTestSuite) TestHTTPAuthRequired() {
httpProxyURL, _ := url.Parse("http://" + suite.ln.Addr().String())
suite.http.Transport.(*http.Transport).Proxy = http.ProxyURL(httpProxyURL)
resp, err := suite.http.Get(suite.httpEndpoint.URL + "/ip")
defer func() {
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}()
suite.NoError(err)
suite.Equal(http.StatusProxyAuthRequired, resp.StatusCode)
}
func (suite *ServerTestSuite) TestHTTPSAuthRequired() {
httpProxyURL, _ := url.Parse("http://" + suite.ln.Addr().String())
suite.http.Transport.(*http.Transport).Proxy = http.ProxyURL(httpProxyURL)
resp, err := suite.http.Get(suite.tlsEndpoint.URL + "/ip")
defer func() {
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}()
suite.Error(err)
}
func (suite *ServerTestSuite) TestGolangOrg() {
resp, err := suite.http.Get("https://golang.org")
suite.NoError(err)
suite.Equal(http.StatusOK, resp.StatusCode)
}
func TestServer(t *testing.T) {
suite.Run(t, &ServerTestSuite{})
}