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
caddytest: Update Caddyfile tests for formatting, HTTP-only blocks
Previous commit improved the Caddyfile adapter so it doesn't unnecessarily add names to "skip" in "auto_https" when the server is already HTTP-only. This commit updates the tests to reflect that change, while also fixing the Caddyfile formatting in many of the tests. We also print the line number of the divergence between input and formatted version in Caddyfile adapt warnings - very useful for finding initial formatting problems.
Matthew Holt committed
Jan 19, 2021 at 14:21 UTC
160d19999982c4facd32c4bddced5a7dc91e8a40
18 files changed
+103
-100
caddyconfig/caddyfile/adapter.go
+29
-7
@@ -53,13 +53,9 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c
53
}
54
55
// lint check: see if input was properly formatted; sometimes messy files files parse
56
- // successfully but result in logical errors because the Caddyfile is a bad format
57
- // TODO: also perform this check on imported files
58
- if !bytes.Equal(Format(body), body) {
59
- warnings = append(warnings, caddyconfig.Warning{
60
- File: filename,
61
- Message: "file is not formatted with 'caddy fmt'",
62
- })
56
+ // successfully but result in logical errors (the Caddyfile is a bad format, I'm sorry)
57
+ if warning, different := formattingDifference(filename, body); different {
58
+ warnings = append(warnings, warning)
59
}
60
61
result, err := json.Marshal(cfg)
@@ -67,6 +63,32 @@ func (a Adapter) Adapt(body []byte, options map[string]interface{}) ([]byte, []c
63
return result, warnings, err
64
}
65
66
+// formattingDifference returns a warning and true if the formatted version
67
+// is any different from the input; empty warning and false otherwise.
68
+// TODO: also perform this check on imported files
69
+func formattingDifference(filename string, body []byte) (caddyconfig.Warning, bool) {
70
+ formatted := Format(body)
71
+ if bytes.Equal(formatted, body) {
72
+ return caddyconfig.Warning{}, false
73
+ }
74
+
75
+ // find where the difference is
76
+ line := 1
77
+ for i, ch := range body {
78
+ if i >= len(formatted) || ch != formatted[i] {
79
+ break
80
+ }
81
+ if ch == '\n' {
82
+ line++
83
+ }
84
+ }
85
+ return caddyconfig.Warning{
86
+ File: filename,
87
+ Line: line,
88
+ Message: "input is not formatted with 'caddy fmt'",
89
+ }, true
90
+}
91
+
92
// Unmarshaler is a type that can unmarshal
93
// Caddyfile tokens to set itself up for a
94
// JSON encoding. The goal of an unmarshaler
caddytest/caddytest.go
+3
-3
@@ -327,7 +327,7 @@ func (tc *Tester) AssertRedirect(requestURI string, expectedToLocation string, e
327
}
328
329
// CompareAdapt adapts a config and then compares it against an expected result
330
-func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) bool {
330
+func CompareAdapt(t *testing.T, filename, rawConfig string, adapterName string, expectedResponse string) bool {
331
332
cfgAdapter := caddyconfig.GetAdapter(adapterName)
333
if cfgAdapter == nil {
@@ -353,7 +353,7 @@ func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedRe
353
354
if len(warnings) > 0 {
355
for _, w := range warnings {
356
- t.Logf("warning: directive: %s : %s", w.Directive, w.Message)
356
+ t.Logf("warning: %s:%d: %s: %s", filename, w.Line, w.Directive, w.Message)
357
}
358
}
359
@@ -388,7 +388,7 @@ func CompareAdapt(t *testing.T, rawConfig string, adapterName string, expectedRe
388
389
// AssertAdapt adapts a config and then tests it against an expected result
390
func AssertAdapt(t *testing.T, rawConfig string, adapterName string, expectedResponse string) {
391
- ok := CompareAdapt(t, rawConfig, adapterName, expectedResponse)
391
+ ok := CompareAdapt(t, "Caddyfile", rawConfig, adapterName, expectedResponse)
392
if !ok {
393
t.Fail()
394
}
caddytest/integration/caddyfile_adapt/global_options_acme.txt
+1
-1
@@ -9,7 +9,7 @@
9
}
10
acme_ca https://example.com
11
acme_eab {
12
- key_id 4K2scIVbBpNd-78scadB2g
12
+ key_id 4K2scIVbBpNd-78scadB2g
13
mac_key abcdefghijklmnopqrstuvwx-abcdefghijklnopqrstuvwxyz12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh
14
}
15
acme_ca_root /path/to/ca.crt
caddytest/integration/caddyfile_adapt/global_server_options_multi.txt
+2
-7
@@ -16,7 +16,7 @@
16
}
17
}
18
19
-foo.com {
19
+foo.com {
20
}
21
22
http://bar.com {
@@ -64,12 +64,7 @@ http://bar.com {
64
],
65
"terminal": true
66
}
67
- ],
68
- "automatic_https": {
69
- "skip": [
70
- "bar.com"
71
- ]
72
- }
67
+ ]
68
},
69
"srv2": {
70
"listen": [
caddytest/integration/caddyfile_adapt/global_server_options_single.txt
+1
-1
@@ -18,7 +18,7 @@
18
}
19
}
20
21
-foo.com {
21
+foo.com {
22
}
23
24
----------
caddytest/integration/caddyfile_adapt/header.txt
+1
-1
@@ -4,7 +4,7 @@
4
header ?John "von Neumann"
5
header -Wolfram
6
header {
7
- Grace: "Hopper" # some users habitually suffix field names with a colon
7
+ Grace: "Hopper" # some users habitually suffix field names with a colon
8
+Ray "Solomonoff"
9
?Tim "Berners-Lee"
10
defer
caddytest/integration/caddyfile_adapt/http_only_on_domain.txt
+1
-6
@@ -46,12 +46,7 @@ http://a.caddy.localhost {
46
],
47
"terminal": true
48
}
49
- ],
50
- "automatic_https": {
51
- "skip": [
52
- "a.caddy.localhost"
53
- ]
54
- }
49
+ ]
50
}
51
}
52
}
caddytest/integration/caddyfile_adapt/matchers_in_route.txt
+6
-6
@@ -1,10 +1,10 @@
1
:80 {
2
- route {
3
- # unused matchers should not panic
4
- # see https://github.com/caddyserver/caddy/issues/3745
5
- @matcher1 path /path1
6
- @matcher2 path /path2
7
- }
2
+ route {
3
+ # unused matchers should not panic
4
+ # see https://github.com/caddyserver/caddy/issues/3745
5
+ @matcher1 path /path1
6
+ @matcher2 path /path2
7
+ }
8
}
9
----------
10
{
caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.txt
+8
-8
@@ -1,15 +1,15 @@
1
:8884
2
3
php_fastcgi localhost:9000 {
4
- # some php_fastcgi-specific subdirectives
5
- split .php .php5
6
- env VAR1 value1
7
- env VAR2 value2
8
- root /var/www
9
- index index.php5
4
+ # some php_fastcgi-specific subdirectives
5
+ split .php .php5
6
+ env VAR1 value1
7
+ env VAR2 value2
8
+ root /var/www
9
+ index index.php5
10
11
- # passed through to reverse_proxy (directive order doesn't matter!)
12
- lb_policy random
11
+ # passed through to reverse_proxy (directive order doesn't matter!)
12
+ lb_policy random
13
}
14
----------
15
{
caddytest/integration/caddyfile_adapt/request_body.txt
+2
-1
@@ -1,6 +1,7 @@
1
localhost
2
+
3
request_body {
3
- max_size 1MB
4
+ max_size 1MB
5
}
6
----------
7
{
caddytest/integration/caddyfile_adapt/reverse_proxy_empty_non_http_transport.txt
+1
-1
@@ -1,7 +1,7 @@
1
:8884
2
3
reverse_proxy 127.0.0.1:65535 {
4
- transport fastcgi
4
+ transport fastcgi
5
}
6
----------
7
{
caddytest/integration/caddyfile_adapt/reverse_proxy_h2c_shorthand.txt
+37
-37
@@ -1,38 +1,38 @@
1
-:8884
2
-
3
-reverse_proxy h2c://localhost:8080
4
-----------
5
-{
6
- "apps": {
7
- "http": {
8
- "servers": {
9
- "srv0": {
10
- "listen": [
11
- ":8884"
12
- ],
13
- "routes": [
14
- {
15
- "handle": [
16
- {
17
- "handler": "reverse_proxy",
18
- "transport": {
19
- "protocol": "http",
20
- "versions": [
21
- "h2c",
22
- "2"
23
- ]
24
- },
25
- "upstreams": [
26
- {
27
- "dial": "localhost:8080"
28
- }
29
- ]
30
- }
31
- ]
32
- }
33
- ]
34
- }
35
- }
36
- }
37
- }
1
+:8884
2
+
3
+reverse_proxy h2c://localhost:8080
4
+----------
5
+{
6
+ "apps": {
7
+ "http": {
8
+ "servers": {
9
+ "srv0": {
10
+ "listen": [
11
+ ":8884"
12
+ ],
13
+ "routes": [
14
+ {
15
+ "handle": [
16
+ {
17
+ "handler": "reverse_proxy",
18
+ "transport": {
19
+ "protocol": "http",
20
+ "versions": [
21
+ "h2c",
22
+ "2"
23
+ ]
24
+ },
25
+ "upstreams": [
26
+ {
27
+ "dial": "localhost:8080"
28
+ }
29
+ ]
30
+ }
31
+ ]
32
+ }
33
+ ]
34
+ }
35
+ }
36
+ }
37
+ }
38
}
\ No newline at end of file
caddytest/integration/caddyfile_adapt/tls_automation_policies_2.txt
+1
-7
@@ -1,5 +1,4 @@
1
# issue #3953
2
-
2
{
3
cert_issuer zerossl api_key
4
}
@@ -58,12 +57,7 @@ http://example.net {
57
],
58
"terminal": true
59
}
61
- ],
62
- "automatic_https": {
63
- "skip": [
64
- "example.net"
65
- ]
66
- }
60
+ ]
61
}
62
}
63
},
caddytest/integration/caddyfile_adapt/tls_client_auth_cert_file.txt
+1
-1
@@ -3,7 +3,7 @@ localhost
3
respond "hello from localhost"
4
tls {
5
client_auth {
6
- mode request
6
+ mode request
7
trusted_ca_cert_file ../caddy.ca.cer
8
}
9
}
caddytest/integration/caddyfile_adapt/tls_client_auth_inline_cert.txt
+2
-2
@@ -3,8 +3,8 @@ localhost
3
respond "hello from localhost"
4
tls {
5
client_auth {
6
- mode request
7
- trusted_ca_cert MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2G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pBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
8
}
9
}
10
----------
caddytest/integration/caddyfile_adapt/tls_conn_policy_consolidate.txt
+1
-6
@@ -75,12 +75,7 @@ http://b.b https://b.b:8443 {
75
],
76
"terminal": true
77
}
78
- ],
79
- "automatic_https": {
80
- "skip": [
81
- "b.b"
82
- ]
83
- }
78
+ ]
79
},
80
"srv2": {
81
"listen": [
caddytest/integration/caddyfile_adapt_test.go
+3
-2
@@ -32,14 +32,15 @@ func TestCaddyfileAdaptToJSON(t *testing.T) {
32
}
33
34
// split the Caddyfile (first) and JSON (second) parts
35
+ // (append newline to Caddyfile to match formatter expectations)
36
parts := strings.Split(string(data), "----------")
36
- caddyfile, json := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
37
+ caddyfile, json := strings.TrimSpace(parts[0])+"\n", strings.TrimSpace(parts[1])
38
39
// replace windows newlines in the json with unix newlines
40
json = winNewlines.ReplaceAllString(json, "\n")
41
42
// run the test
42
- ok := caddytest.CompareAdapt(t, caddyfile, "caddyfile", json)
43
+ ok := caddytest.CompareAdapt(t, filename, caddyfile, "caddyfile", json)
44
if !ok {
45
t.Errorf("failed to adapt %s", filename)
46
}
cmd/commandfuncs.go
+3
-3
@@ -529,6 +529,9 @@ func cmdAdaptConfig(fl Flags) (int, error) {
529
adaptedConfig = prettyBuf.Bytes()
530
}
531
532
+ // print result to stdout
533
+ fmt.Println(string(adaptedConfig))
534
+
535
// print warnings to stderr
536
for _, warn := range warnings {
537
msg := warn.Message
@@ -538,9 +541,6 @@ func cmdAdaptConfig(fl Flags) (int, error) {
541
fmt.Fprintf(os.Stderr, "[WARNING][%s] %s:%d: %s\n", adaptCmdAdapterFlag, warn.File, warn.Line, msg)
542
}
543
541
- // print result to stdout
542
- fmt.Println(string(adaptedConfig))
543
-
544
// validate output if requested
545
if adaptCmdValidateFlag {
546
var cfg *caddy.Config