diff --git a/plugin/caddyfile/merger.go b/plugin/caddyfile/merge.go similarity index 84% rename from plugin/caddyfile/merger.go rename to plugin/caddyfile/merge.go index 352682ab..a2214eb5 100644 --- a/plugin/caddyfile/merger.go +++ b/plugin/caddyfile/merge.go @@ -8,8 +8,8 @@ OuterLoop: for _, blockB := range containerB.Children { firstKey := blockB.GetFirstKey() for _, blockA := range containerA.GetAllByFirstKey(firstKey) { - if firstKey == "reverse_proxy" && getMatcher(blockA) == getMatcher(blockB) { - mergeReverseProxy(blockA, blockB) + if (firstKey == "reverse_proxy" || firstKey == "php_fastcgi") && getMatcher(blockA) == getMatcher(blockB) { + mergeReverseProxyLike(blockA, blockB) continue OuterLoop } else if blocksAreEqual(blockA, blockB) { blockA.Container.Merge(blockB.Container) @@ -20,7 +20,7 @@ OuterLoop: } } -func mergeReverseProxy(blockA *Block, blockB *Block) { +func mergeReverseProxyLike(blockA *Block, blockB *Block) { for index, key := range blockB.Keys[1:] { if index > 0 || !isMatcher(key) { blockA.AddKeys(key) diff --git a/plugin/caddyfile/merge_test.go b/plugin/caddyfile/merge_test.go new file mode 100644 index 00000000..00e8deda --- /dev/null +++ b/plugin/caddyfile/merge_test.go @@ -0,0 +1,60 @@ +package caddyfile + +import ( + "io/ioutil" + "regexp" + "strings" + "testing" + + _ "github.com/caddyserver/caddy/v2/modules/standard" // plug standard HTTP modules + "github.com/stretchr/testify/assert" +) + +func TestMerge(t *testing.T) { + const folder = "./testdata/merge" + + // load the list of test files from the dir + files, err := ioutil.ReadDir(folder) + if err != nil { + t.Errorf("failed to read process dir: %s", err) + } + + // prep a regexp to fix strings on windows + winNewlines := regexp.MustCompile(`\r?\n`) + + for _, f := range files { + if f.IsDir() { + continue + } + + // read the test file + filename := f.Name() + + t.Run(filename, func(t *testing.T) { + data, err := ioutil.ReadFile(folder + "/" + filename) + if err != nil { + t.Errorf("failed to read %s dir: %s", filename, err) + } + + // replace windows newlines in the json with unix newlines + content := winNewlines.ReplaceAllString(string(data), "\n") + + // split two Caddyfile parts + parts := strings.Split(content, "----------\n") + caddyfile1, caddyfile2, expectedCaddyfile := parts[0], parts[1], parts[2] + + container1, _ := Unmarshal([]byte(caddyfile1)) + container2, _ := Unmarshal([]byte(caddyfile2)) + + container1.Merge(container2) + + result := string(container1.Marshal()) + + actualCaddyfile := string(result) + + // compare the actual and expected Caddyfiles + assert.Equal(t, expectedCaddyfile, actualCaddyfile, + "failed to process in %s", filename) + }) + } +} diff --git a/plugin/caddyfile/testdata/merge/php_fastcgi_different_matcher.txt b/plugin/caddyfile/testdata/merge/php_fastcgi_different_matcher.txt new file mode 100644 index 00000000..ce23f216 --- /dev/null +++ b/plugin/caddyfile/testdata/merge/php_fastcgi_different_matcher.txt @@ -0,0 +1,12 @@ +example.com { + php_fastcgi /a service-a:80 +} +---------- +example.com { + php_fastcgi /b service-b:81 +} +---------- +example.com { + php_fastcgi /a service-a:80 + php_fastcgi /b service-b:81 +} diff --git a/plugin/caddyfile/testdata/merge/php_fastcgi_no_matcher.txt b/plugin/caddyfile/testdata/merge/php_fastcgi_no_matcher.txt new file mode 100644 index 00000000..ebf01faa --- /dev/null +++ b/plugin/caddyfile/testdata/merge/php_fastcgi_no_matcher.txt @@ -0,0 +1,11 @@ +example.com { + php_fastcgi service-a:80 +} +---------- +example.com { + php_fastcgi service-b:81 +} +---------- +example.com { + php_fastcgi service-a:80 service-b:81 +} diff --git a/plugin/caddyfile/testdata/merge/php_fastcgi_same_matcher.txt b/plugin/caddyfile/testdata/merge/php_fastcgi_same_matcher.txt new file mode 100644 index 00000000..2897a801 --- /dev/null +++ b/plugin/caddyfile/testdata/merge/php_fastcgi_same_matcher.txt @@ -0,0 +1,11 @@ +example.com { + php_fastcgi /path service-a:80 +} +---------- +example.com { + php_fastcgi /path service-b:81 +} +---------- +example.com { + php_fastcgi /path service-a:80 service-b:81 +} diff --git a/plugin/caddyfile/testdata/merge/reverse_proxy_different_matcher.txt b/plugin/caddyfile/testdata/merge/reverse_proxy_different_matcher.txt new file mode 100644 index 00000000..84a03b3e --- /dev/null +++ b/plugin/caddyfile/testdata/merge/reverse_proxy_different_matcher.txt @@ -0,0 +1,12 @@ +example.com { + reverse_proxy /a service-a:80 +} +---------- +example.com { + reverse_proxy /b service-b:81 +} +---------- +example.com { + reverse_proxy /a service-a:80 + reverse_proxy /b service-b:81 +} diff --git a/plugin/caddyfile/testdata/merge/reverse_proxy_no_matcher.txt b/plugin/caddyfile/testdata/merge/reverse_proxy_no_matcher.txt new file mode 100644 index 00000000..f692b4b3 --- /dev/null +++ b/plugin/caddyfile/testdata/merge/reverse_proxy_no_matcher.txt @@ -0,0 +1,11 @@ +example.com { + reverse_proxy service-a:80 +} +---------- +example.com { + reverse_proxy service-b:81 +} +---------- +example.com { + reverse_proxy service-a:80 service-b:81 +} diff --git a/plugin/caddyfile/testdata/merge/reverse_proxy_same_matcher.txt b/plugin/caddyfile/testdata/merge/reverse_proxy_same_matcher.txt new file mode 100644 index 00000000..fbbcefb5 --- /dev/null +++ b/plugin/caddyfile/testdata/merge/reverse_proxy_same_matcher.txt @@ -0,0 +1,11 @@ +example.com { + reverse_proxy /path service-a:80 +} +---------- +example.com { + reverse_proxy /path service-b:81 +} +---------- +example.com { + reverse_proxy /path service-a:80 service-b:81 +}