@marcel91200 / caddy Mirror · read-only / commits / 78717ce5
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

chore: add adapt tests. fix load failure not failing tests (#3222)

* add adaption tests. fix load failure not failing tests * removed unnecessary assignment

Mark Sargent committed Apr 4, 2020 at 16:02 UTC 78717ce5b0a940df0ef91a19e8389b11505a344d
4 files changed +344 -1
caddytest/caddytest.go
+56 -1
@@ -19,6 +19,8 @@ import (
19 "testing"
20 "time"
21
22 + "github.com/aryann/difflib"
23 + "github.com/caddyserver/caddy/v2/caddyconfig"
24 caddycmd "github.com/caddyserver/caddy/v2/cmd"
25
26 // plug in Caddy modules here
@@ -58,7 +60,8 @@ func timeElapsed(start time.Time, name string) {
60 // InitServer this will configure the server with a configurion of a specific
61 // type. The configType must be either "json" or the adapter type.
62 func InitServer(t *testing.T, rawConfig string, configType string) {
61 - if err := initServer(t, rawConfig, configType); errors.Is(err, &configLoadError{}) {
63 +
64 + if err := initServer(t, rawConfig, configType); err != nil {
65 t.Logf("failed to load config: %s", err)
66 t.Fail()
67 }
@@ -316,3 +319,55 @@ func AssertRedirect(t *testing.T, requestURI string, expectedToLocation string,
319
320 return resp
321 }
322 +
323 +// AssertAdapt adapts a config and then tests it against an expected result
324 +func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
325 +
326 + cfgAdapter := caddyconfig.GetAdapter(adapterName)
327 + if cfgAdapter == nil {
328 + t.Errorf("unrecognized config adapter '%s'", adapterName)
329 + return
330 + }
331 +
332 + options := make(map[string]interface{})
333 + options["pretty"] = "true"
334 +
335 + result, warnings, err := cfgAdapter.Adapt([]byte(rawConfig), options)
336 + if err != nil {
337 + t.Errorf("adapting config using %s adapter: %v", adapterName, err)
338 + return
339 + }
340 +
341 + if len(warnings) > 0 {
342 + for _, w := range warnings {
343 + t.Logf("warning: directive: %s : %s", w.Directive, w.Message)
344 + }
345 + }
346 +
347 + diff := difflib.Diff(
348 + strings.Split(expectedResponse, "\n"),
349 + strings.Split(string(result), "\n"))
350 +
351 + // scan for failure
352 + failed := false
353 + for _, d := range diff {
354 + if d.Delta != difflib.Common {
355 + failed = true
356 + break
357 + }
358 + }
359 +
360 + if failed {
361 + for _, d := range diff {
362 + switch d.Delta {
363 + case difflib.Common:
364 + fmt.Printf(" %s\n", d.Payload)
365 + case difflib.LeftOnly:
366 + fmt.Printf(" - %s\n", d.Payload)
367 + case difflib.RightOnly:
368 + fmt.Printf(" + %s\n", d.Payload)
369 + }
370 + }
371 + t.Fail()
372 + }
373 +}
caddytest/integration/caddyfile_adapt_test.go new
+285
@@ -0,0 +1,285 @@
1 +package integration
2 +
3 +import (
4 + "testing"
5 +
6 + "github.com/caddyserver/caddy/v2/caddytest"
7 +)
8 +
9 +func TestHttpOnlyOnLocalhost(t *testing.T) {
10 + caddytest.AssertAdapt(t, `
11 + localhost:80 {
12 + respond /version 200 {
13 + body "hello from localhost"
14 + }
15 + }
16 + `, "caddyfile", `{
17 + "apps": {
18 + "http": {
19 + "servers": {
20 + "srv0": {
21 + "listen": [
22 + ":80"
23 + ],
24 + "routes": [
25 + {
26 + "match": [
27 + {
28 + "host": [
29 + "localhost"
30 + ]
31 + }
32 + ],
33 + "handle": [
34 + {
35 + "handler": "subroute",
36 + "routes": [
37 + {
38 + "handle": [
39 + {
40 + "body": "hello from localhost",
41 + "handler": "static_response",
42 + "status_code": 200
43 + }
44 + ],
45 + "match": [
46 + {
47 + "path": [
48 + "/version"
49 + ]
50 + }
51 + ]
52 + }
53 + ]
54 + }
55 + ],
56 + "terminal": true
57 + }
58 + ]
59 + }
60 + }
61 + }
62 + }
63 +}`)
64 +}
65 +
66 +func TestHttpOnlyOnAnyAddress(t *testing.T) {
67 + caddytest.AssertAdapt(t, `
68 + :80 {
69 + respond /version 200 {
70 + body "hello from localhost"
71 + }
72 + }
73 + `, "caddyfile", `{
74 + "apps": {
75 + "http": {
76 + "servers": {
77 + "srv0": {
78 + "listen": [
79 + ":80"
80 + ],
81 + "routes": [
82 + {
83 + "match": [
84 + {
85 + "path": [
86 + "/version"
87 + ]
88 + }
89 + ],
90 + "handle": [
91 + {
92 + "body": "hello from localhost",
93 + "handler": "static_response",
94 + "status_code": 200
95 + }
96 + ]
97 + }
98 + ]
99 + }
100 + }
101 + }
102 + }
103 +}`)
104 +}
105 +
106 +func TestHttpsOnDomain(t *testing.T) {
107 + caddytest.AssertAdapt(t, `
108 + a.caddy.localhost {
109 + respond /version 200 {
110 + body "hello from localhost"
111 + }
112 + }
113 + `, "caddyfile", `{
114 + "apps": {
115 + "http": {
116 + "servers": {
117 + "srv0": {
118 + "listen": [
119 + ":443"
120 + ],
121 + "routes": [
122 + {
123 + "match": [
124 + {
125 + "host": [
126 + "a.caddy.localhost"
127 + ]
128 + }
129 + ],
130 + "handle": [
131 + {
132 + "handler": "subroute",
133 + "routes": [
134 + {
135 + "handle": [
136 + {
137 + "body": "hello from localhost",
138 + "handler": "static_response",
139 + "status_code": 200
140 + }
141 + ],
142 + "match": [
143 + {
144 + "path": [
145 + "/version"
146 + ]
147 + }
148 + ]
149 + }
150 + ]
151 + }
152 + ],
153 + "terminal": true
154 + }
155 + ]
156 + }
157 + }
158 + }
159 + }
160 +}`)
161 +}
162 +
163 +func TestHttpOnlyOnDomain(t *testing.T) {
164 + caddytest.AssertAdapt(t, `
165 + http://a.caddy.localhost {
166 + respond /version 200 {
167 + body "hello from localhost"
168 + }
169 + }
170 + `, "caddyfile", `{
171 + "apps": {
172 + "http": {
173 + "servers": {
174 + "srv0": {
175 + "listen": [
176 + ":80"
177 + ],
178 + "routes": [
179 + {
180 + "match": [
181 + {
182 + "host": [
183 + "a.caddy.localhost"
184 + ]
185 + }
186 + ],
187 + "handle": [
188 + {
189 + "handler": "subroute",
190 + "routes": [
191 + {
192 + "handle": [
193 + {
194 + "body": "hello from localhost",
195 + "handler": "static_response",
196 + "status_code": 200
197 + }
198 + ],
199 + "match": [
200 + {
201 + "path": [
202 + "/version"
203 + ]
204 + }
205 + ]
206 + }
207 + ]
208 + }
209 + ],
210 + "terminal": true
211 + }
212 + ],
213 + "automatic_https": {
214 + "skip": [
215 + "a.caddy.localhost"
216 + ]
217 + }
218 + }
219 + }
220 + }
221 + }
222 +}`)
223 +}
224 +
225 +func TestHttpOnlyOnNonStandardPort(t *testing.T) {
226 + caddytest.AssertAdapt(t, `
227 + http://a.caddy.localhost:81 {
228 + respond /version 200 {
229 + body "hello from localhost"
230 + }
231 + }
232 + `, "caddyfile", `{
233 + "apps": {
234 + "http": {
235 + "servers": {
236 + "srv0": {
237 + "listen": [
238 + ":81"
239 + ],
240 + "routes": [
241 + {
242 + "match": [
243 + {
244 + "host": [
245 + "a.caddy.localhost"
246 + ]
247 + }
248 + ],
249 + "handle": [
250 + {
251 + "handler": "subroute",
252 + "routes": [
253 + {
254 + "handle": [
255 + {
256 + "body": "hello from localhost",
257 + "handler": "static_response",
258 + "status_code": 200
259 + }
260 + ],
261 + "match": [
262 + {
263 + "path": [
264 + "/version"
265 + ]
266 + }
267 + ]
268 + }
269 + ]
270 + }
271 + ],
272 + "terminal": true
273 + }
274 + ],
275 + "automatic_https": {
276 + "skip": [
277 + "a.caddy.localhost"
278 + ]
279 + }
280 + }
281 + }
282 + }
283 + }
284 +}`)
285 +}
go.mod
+1
@@ -5,6 +5,7 @@ go 1.14
5 require (
6 github.com/Masterminds/sprig/v3 v3.0.2
7 github.com/alecthomas/chroma v0.7.2-0.20200305040604-4f3623dce67a
8 + github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a
9 github.com/caddyserver/certmagic v0.10.10
10 github.com/cenkalti/backoff/v4 v4.0.2 // indirect
11 github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
go.sum
+2
@@ -106,6 +106,8 @@ github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015 h1:StuiJFxQUsxSCzcby6
106 github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
107 github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
108 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
109 +github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a h1:pv34s756C4pEXnjgPfGYgdhg/ZdajGhyOvzx8k+23nw=
110 +github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
111 github.com/asaskevich/govalidator v0.0.0-20180315120708-ccb8e960c48f/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
112 github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
113 github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=