package httpcaddyfile import ( "testing" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) func TestMatcherSyntax(t *testing.T) { for i, tc := range []struct { input string expectError bool }{ { input: `http://localhost @debug { query showdebug=1 } `, expectError: false, }, { input: `http://localhost @debug { query bad format } `, expectError: true, }, { input: `http://localhost @debug { not { path /somepath* } } `, expectError: false, }, { input: `http://localhost @debug { not path /somepath* } `, expectError: false, }, { input: `http://localhost @debug not path /somepath* `, expectError: false, }, { input: `@matcher { path /matcher-not-allowed/outside-of-site-block/* } http://localhost `, 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 } } } func TestSpecificity(t *testing.T) { for i, tc := range []struct { input string expect int }{ {"", 0}, {"*", 0}, {"*.*", 1}, {"{placeholder}", 0}, {"/{placeholder}", 1}, {"foo", 3}, {"example.com", 11}, {"a.example.com", 13}, {"*.example.com", 12}, {"/foo", 4}, {"/foo*", 4}, {"{placeholder}.example.com", 12}, {"{placeholder.example.com", 24}, {"}.", 2}, {"}{", 2}, {"{}", 0}, {"{{{}}", 1}, } { actual := specificity(tc.input) if actual != tc.expect { t.Errorf("Test %d (%s): Expected %d but got %d", i, tc.input, tc.expect, actual) } } } func TestGlobalOptions(t *testing.T) { for i, tc := range []struct { input string expectError bool }{ { input: ` { email test@example.com } :80 `, expectError: false, }, { input: ` { admin off } :80 `, expectError: false, }, { input: ` { admin 127.0.0.1:2020 } :80 `, expectError: false, }, { input: ` { admin { disabled false } } :80 `, expectError: true, }, { input: ` { admin { enforce_origin origins 192.168.1.1:2020 127.0.0.1:2020 } } :80 `, expectError: false, }, { input: ` { admin 127.0.0.1:2020 { enforce_origin origins 192.168.1.1:2020 127.0.0.1:2020 } } :80 `, expectError: false, }, { input: ` { admin 192.168.1.1:2020 127.0.0.1:2020 { enforce_origin origins 192.168.1.1:2020 127.0.0.1:2020 } } :80 `, expectError: true, }, { input: ` { admin off { enforce_origin origins 192.168.1.1:2020 127.0.0.1:2020 } } :80 `, 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 } } }