-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gondola_test.go
284 lines (266 loc) · 6.12 KB
/
gondola_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package gondola
import (
"crypto/tls"
"errors"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestNewGondola(t *testing.T) {
data := `
proxy:
port: 443
read_header_timeout: 2000
shutdown_timeout: 3000
tls_cert_path: /path/to/cert
tls_key_path: /path/to/key
static_files:
- path: /public/
dir: testdata/public
upstreams:
- host_name: backend1.local
target: http://backend1:8081
- host_name: backend2.local
target: http://backend2:8082
log_level: -4
`
gondola, err := NewGondola(strings.NewReader(data))
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if gondola.config == nil {
t.Errorf("Expected config, got nil")
}
if gondola.server == nil {
t.Errorf("Expected server, got nil")
}
gondola.server.Close()
}
func TestNewGondolaConfigLoadError(t *testing.T) {
r := strings.NewReader("invalid")
_, err := NewGondola(r)
if err == nil {
t.Errorf("Expected error, got nil")
}
var cfgErr *ConfigLoadError
if !errors.As(err, &cfgErr) {
t.Errorf("Expected error, got %v", err)
}
}
func TestNewGondolaProxyServerError(t *testing.T) {
data := `
proxy:
port: 8080
read_header_timeout: 2000
shutdown_timeout: 3000
tls_cert_path: /path/to/cert
tls_key_path: /path/to/key
static_files:
- path: /public/
dir: testdata/public
upstreams:
- host_name: backend1.local
target: "://"
- host_name: backend2.local
target: "://"
log_level: -4
`
_, err := NewGondola(strings.NewReader(data))
if err == nil {
t.Errorf("Expected error, got nil")
}
var psErr *ProxyServerError
if !errors.As(err, &psErr) {
t.Errorf("Expected error, got %v", err)
}
}
func TestRun(t *testing.T) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("backend1"))
}))
defer backend1.Close()
backend2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("backend2"))
}))
defer backend2.Close()
backend1URL, err := url.Parse(backend1.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
backend2URL, err := url.Parse(backend2.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// without TLS config
data := `
proxy:
port: 8080
read_header_timeout: 2000
shutdown_timeout: 3000
static_files:
- path: /public/
dir: testdata/public
upstreams:
- host_name: backend1.local
target: ` + backend1URL.String() + `
- host_name: backend2.local
target: ` + backend2URL.String() + `
log_level: -4
`
gondola, err := NewGondola(strings.NewReader(data))
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
go func() {
gondola.Run()
}()
for _, test := range []struct {
name string
reqPath string
path string
body string
code int
}{
{
name: "request to backend1",
reqPath: "http://backend1.local:8080/",
body: "backend1",
code: http.StatusOK,
},
{
name: "request to backend2",
reqPath: "http://backend2.local:8080/",
body: "backend2",
code: http.StatusOK,
},
{
name: "request to static file",
reqPath: "http://localhost:8080/public/index.html",
body: "test",
code: http.StatusOK,
},
} {
t.Run(test.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, test.reqPath, nil)
if err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != test.code {
t.Errorf("Expected status code %d, got %d", test.code, res.StatusCode)
}
if string(b) != test.body {
t.Errorf("Expected body %s, got %s", test.body, string(b))
}
})
}
gondola.server.Close()
}
func TestRunWithTLS(t *testing.T) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("backend1"))
}))
defer backend1.Close()
backend2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("backend2"))
}))
defer backend2.Close()
backend1URL, err := url.Parse(backend1.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
backend2URL, err := url.Parse(backend2.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// with TLS config
data := `
proxy:
port: 5443
read_header_timeout: 2000
shutdown_timeout: 3000
tls_cert_path: testdata/certificates/cert.pem
tls_key_path: testdata/certificates/key.pem
static_files:
- path: /public/
dir: testdata/public
upstreams:
- host_name: backend1.local
target: ` + backend1URL.String() + `
- host_name: backend2.local
target: ` + backend2URL.String() + `
log_level: -4
`
gondola, err := NewGondola(strings.NewReader(data))
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
go func() {
gondola.Run()
}()
for _, test := range []struct {
name string
reqPath string
path string
body string
code int
}{
{
name: "request to backend1",
reqPath: "https://backend1.local:5443",
body: "backend1",
code: http.StatusOK,
},
{
name: "request to backend2",
reqPath: "https://backend2.local:5443",
body: "backend2",
code: http.StatusOK,
},
{
name: "request to static file",
reqPath: "https://localhost:5443/public/index.html",
body: "test",
code: http.StatusOK,
},
} {
t.Run(test.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, test.reqPath, nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
res, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != test.code {
t.Errorf("Expected status code %d, got %d", test.code, res.StatusCode)
}
if string(b) != test.body {
t.Errorf("Expected body %s, got %s", test.body, string(b))
}
})
}
gondola.server.Close()
}