2020-05-16 05:57:16 +08:00
|
|
|
package httpcaddyfile
|
|
|
|
|
|
|
|
import (
|
2023-02-17 08:08:36 +08:00
|
|
|
"strings"
|
2020-05-16 05:57:16 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
|
|
_ "github.com/caddyserver/caddy/v2/modules/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogDirectiveSyntax(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
2021-03-13 04:00:02 +08:00
|
|
|
output string
|
2020-05-16 05:57:16 +08:00
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
log
|
|
|
|
}
|
|
|
|
`,
|
2021-03-13 04:00:02 +08:00
|
|
|
output: `{"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{}}}}}}`,
|
2020-05-16 05:57:16 +08:00
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
log {
|
2024-06-10 23:03:24 +08:00
|
|
|
core mock
|
2020-05-16 05:57:16 +08:00
|
|
|
output file foo.log
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2024-06-10 23:03:24 +08:00
|
|
|
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"writer":{"filename":"foo.log","output":"file"},"core":{"module":"mock"},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
|
2020-05-16 05:57:16 +08:00
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
2021-03-13 04:00:02 +08:00
|
|
|
log {
|
|
|
|
format filter {
|
|
|
|
wrap console
|
|
|
|
fields {
|
2021-11-29 14:18:35 +08:00
|
|
|
request>remote_ip ip_mask {
|
2021-03-13 04:00:02 +08:00
|
|
|
ipv4 24
|
|
|
|
ipv6 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2021-11-29 14:18:35 +08:00
|
|
|
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.log0"]},"log0":{"encoder":{"fields":{"request\u003eremote_ip":{"filter":"ip_mask","ipv4_cidr":24,"ipv6_cidr":32}},"format":"filter","wrap":{"format":"console"}},"include":["http.log.access.log0"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"log0"}}}}}}`,
|
2021-03-13 04:00:02 +08:00
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
2023-08-02 15:13:46 +08:00
|
|
|
log name-override {
|
2024-06-10 23:03:24 +08:00
|
|
|
core mock
|
2020-05-16 05:57:16 +08:00
|
|
|
output file foo.log
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2024-06-10 23:03:24 +08:00
|
|
|
output: `{"logging":{"logs":{"default":{"exclude":["http.log.access.name-override"]},"name-override":{"writer":{"filename":"foo.log","output":"file"},"core":{"module":"mock"},"include":["http.log.access.name-override"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":8080"],"logs":{"default_logger_name":"name-override"}}}}}}`,
|
2023-08-02 15:13:46 +08:00
|
|
|
expectError: false,
|
2020-05-16 05:57:16 +08:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
|
|
|
adapter := caddyfile.Adapter{
|
|
|
|
ServerType: ServerType{},
|
|
|
|
}
|
|
|
|
|
2021-03-13 04:00:02 +08:00
|
|
|
out, _, err := adapter.Adapt([]byte(tc.input), nil)
|
2020-05-16 05:57:16 +08:00
|
|
|
|
|
|
|
if err != nil != tc.expectError {
|
|
|
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-13 04:00:02 +08:00
|
|
|
|
|
|
|
if string(out) != tc.output {
|
|
|
|
t.Errorf("Test %d error output mismatch Expected: %s, got %s", i, tc.output, out)
|
|
|
|
}
|
2020-05-16 05:57:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 03:59:50 +08:00
|
|
|
|
|
|
|
func TestRedirDirectiveSyntax(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir :8081
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir * :8081
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /api/* :8081 300
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir :8081 300
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /api/* :8081 399
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir :8081 399
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /old.html /new.html
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /old.html /new.html temporary
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir https://example.com{uri} permanent
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /old.html /new.html permanent
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /old.html /new.html html
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
2022-05-06 22:50:26 +08:00
|
|
|
// this is now allowed so a Location header
|
|
|
|
// can be written and consumed by JS
|
|
|
|
// in the case of XHR requests
|
2021-01-29 03:59:50 +08:00
|
|
|
input: `:8080 {
|
2022-05-06 22:50:26 +08:00
|
|
|
redir * :8081 401
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir * :8081 402
|
2021-01-29 03:59:50 +08:00
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
2022-05-06 22:50:26 +08:00
|
|
|
redir * :8081 {http.reverse_proxy.status_code}
|
|
|
|
}`,
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir /old.html /new.html htlm
|
2021-01-29 03:59:50 +08:00
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
2022-05-06 22:50:26 +08:00
|
|
|
redir * :8081 200
|
2021-01-29 03:59:50 +08:00
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir * :8081 temp
|
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir * :8081 perm
|
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `:8080 {
|
|
|
|
redir * :8081 php
|
|
|
|
}`,
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
|
|
|
|
adapter := caddyfile.Adapter{
|
|
|
|
ServerType: ServerType{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
|
|
|
|
|
|
|
if err != nil != tc.expectError {
|
|
|
|
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 08:08:36 +08:00
|
|
|
|
|
|
|
func TestImportErrorLine(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
|
|
|
errorFunc func(err error) bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `(t1) {
|
|
|
|
abort {args[:]}
|
|
|
|
}
|
|
|
|
:8080 {
|
|
|
|
import t1
|
|
|
|
import t1 true
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
2023-05-26 03:05:00 +08:00
|
|
|
return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1)")
|
2023-02-17 08:08:36 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `(t1) {
|
|
|
|
abort {args[:]}
|
|
|
|
}
|
|
|
|
:8080 {
|
|
|
|
import t1 true
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
2023-05-26 03:05:00 +08:00
|
|
|
return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1)")
|
2023-02-17 08:08:36 +08:00
|
|
|
},
|
|
|
|
},
|
2023-05-23 05:36:55 +08:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
import testdata/import_variadic_snippet.txt
|
|
|
|
:8080 {
|
|
|
|
import t1 true
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
|
|
|
return err == nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
import testdata/import_variadic_with_import.txt
|
|
|
|
:8080 {
|
|
|
|
import t1 true
|
|
|
|
import t2 true
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
|
|
|
return err == nil
|
|
|
|
},
|
|
|
|
},
|
2023-02-17 08:08:36 +08:00
|
|
|
} {
|
|
|
|
adapter := caddyfile.Adapter{
|
|
|
|
ServerType: ServerType{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
|
|
|
|
|
|
|
if !tc.errorFunc(err) {
|
|
|
|
t.Errorf("Test %d error expectation failed, got %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-13 04:32:22 +08:00
|
|
|
|
|
|
|
func TestNestedImport(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
input string
|
|
|
|
errorFunc func(err error) bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: `(t1) {
|
|
|
|
respond {args[0]} {args[1]}
|
|
|
|
}
|
|
|
|
|
|
|
|
(t2) {
|
|
|
|
import t1 {args[0]} 202
|
|
|
|
}
|
|
|
|
|
|
|
|
:8080 {
|
|
|
|
handle {
|
|
|
|
import t2 "foobar"
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
|
|
|
return err == nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `(t1) {
|
|
|
|
respond {args[:]}
|
|
|
|
}
|
|
|
|
|
|
|
|
(t2) {
|
|
|
|
import t1 {args[0]} {args[1]}
|
|
|
|
}
|
|
|
|
|
|
|
|
:8080 {
|
|
|
|
handle {
|
|
|
|
import t2 "foobar" 202
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
|
|
|
return err == nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `(t1) {
|
|
|
|
respond {args[0]} {args[1]}
|
|
|
|
}
|
|
|
|
|
|
|
|
(t2) {
|
|
|
|
import t1 {args[:]}
|
|
|
|
}
|
|
|
|
|
|
|
|
:8080 {
|
|
|
|
handle {
|
|
|
|
import t2 "foobar" 202
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
errorFunc: func(err error) bool {
|
|
|
|
return err == nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
adapter := caddyfile.Adapter{
|
|
|
|
ServerType: ServerType{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := adapter.Adapt([]byte(tc.input), nil)
|
|
|
|
|
|
|
|
if !tc.errorFunc(err) {
|
|
|
|
t.Errorf("Test %d error expectation failed, got %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|