forked from lindenlab/caddy-s3-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaddyfile_test.go
221 lines (215 loc) · 5.07 KB
/
caddyfile_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
package caddys3proxy
import (
"reflect"
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
type testCase struct {
desc string
input string
shouldErr bool
errString string
obj S3Proxy
}
func TestParseCaddyfile(t *testing.T) {
testCases := []testCase{
testCase{
desc: "bad sub directive",
input: `s3proxy {
foo
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: foo not a valid s3proxy option",
},
testCase{
desc: "bucket bad # args",
input: `s3proxy {
bucket
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'bucket'",
},
testCase{
desc: "bucket empty string",
input: `s3proxy {
bucket ""
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: bucket must be set and not empty",
},
testCase{
desc: "bucket missing",
input: `s3proxy {
region foo
}`,
shouldErr: true,
errString: "Testfile:3 - Error during parsing: bucket must be set and not empty",
},
testCase{
desc: "endpoint bad # args",
input: `s3proxy {
endpoint
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'endpoint'",
},
testCase{
desc: "region bad # args",
input: `s3proxy {
region one two
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'one'",
},
testCase{
desc: "root bad # args",
input: `s3proxy {
root one two
}`,
shouldErr: true,
errString: "Testfile:2 - Error during parsing: Wrong argument count or unexpected line ending after 'one'",
},
testCase{
desc: "errors on invalid HTTP status for errors",
input: `s3proxy {
bucket mybucket
errors invalid "path/to/404.html"
}`,
shouldErr: true,
errString: "Testfile:3 - Error during parsing: 'invalid' is not a valid HTTP status code",
},
testCase{
desc: "errors on too many arguments for errors",
input: `s3proxy {
bucket mybucket
errors 403 "path/to/404.html" "what's this?"
}`,
shouldErr: true,
errString: "Testfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'what's this?'",
},
testCase{
desc: "endpoint gets set",
input: `s3proxy {
bucket mybucket
endpoint myvalue
region myregion
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
Endpoint: "myvalue",
Region: "myregion",
},
},
testCase{
desc: "enable pu",
input: `s3proxy {
bucket mybucket
enable_put
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
EnablePut: true,
},
},
testCase{
desc: "enable delete",
input: `s3proxy {
bucket mybucket
enable_delete
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
EnableDelete: true,
},
},
testCase{
desc: "enable error pages",
input: `s3proxy {
bucket mybucket
errors 404 "path/to/404.html"
errors 403 "path/to/403.html"
errors "path/to/default_error.html"
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
ErrorPages: map[int]string{
403: "path/to/403.html",
404: "path/to/404.html",
},
DefaultErrorPage: "path/to/default_error.html",
},
},
testCase{
desc: "hide files",
input: `s3proxy {
bucket mybucket
hide foo.txt _*
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
Hide: []string{"foo.txt", "_*"},
},
},
testCase{
desc: "hide files - missing arg",
input: `s3proxy {
bucket mybucket
hide
}`,
shouldErr: true,
errString: "Testfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'hide'",
},
testCase{
desc: "index test",
input: `s3proxy {
bucket mybucket
index i.htm i.html
}`,
shouldErr: false,
obj: S3Proxy{
Bucket: "mybucket",
IndexNames: []string{"i.htm", "i.html"},
},
},
testCase{
desc: "index - missing arg",
input: `s3proxy {
bucket mybucket
index
}`,
shouldErr: true,
errString: "Testfile:3 - Error during parsing: Wrong argument count or unexpected line ending after 'index'",
},
}
for _, tc := range testCases {
d := caddyfile.NewTestDispenser(tc.input)
prox, err := parseCaddyfileWithDispenser(d)
if tc.shouldErr {
if err == nil {
t.Errorf("Test case '%s' expected an err and did not get one", tc.desc)
}
if prox != nil {
t.Errorf("Test case '%s' expected an nil obj but it was not nil", tc.desc)
}
if err.Error() != tc.errString {
t.Errorf("Test case '%s' expected err '%s' but got '%s'", tc.desc, tc.errString, err.Error())
}
} else {
if err != nil {
t.Errorf("Test case '%s' unexpected err '%s'", tc.desc, err.Error())
}
if prox == nil {
t.Errorf("Test case '%s' return object was nil", tc.desc)
continue
}
if !reflect.DeepEqual(*prox, tc.obj) {
t.Errorf("Test case '%s' expected Endpoint of '%#v' but got '%#v'", tc.desc, tc.obj, prox)
}
}
}
}