Files
CaddyProxyManager/backend/internal/caddy/json_test.go
Pacerino 7ff0949067 [Backend] Add Caddy provider abstraction with Caddyfile and API modes
Introduce a Provider interface selectable via CPM_CADDY_MODE:
- CaddyfileProvider renders per-host snippets (template whitespace fixed)
- APIProvider manages Caddy's JSON config via the admin API
Add configurable reload strategy (systemd/exec/api/none) and a
configurable listen address for the bootstrapped server in API mode.
Includes unit tests for the JSON builder, provider factory and the
API provider (httptest).
2026-06-14 02:34:54 +02:00

68 lines
1.6 KiB
Go

package caddy
import (
"reflect"
"testing"
"github.com/Pacerino/CaddyProxyManager/internal/database"
)
func TestHostRouteID(t *testing.T) {
if got := hostRouteID(42); got != "cpm_host_42" {
t.Fatalf("hostRouteID(42) = %q, want cpm_host_42", got)
}
}
func TestSplitDomains(t *testing.T) {
tests := []struct {
in string
want []string
}{
{"", []string{}},
{" ", []string{}},
{"example.com", []string{"example.com"}},
{"a.com b.com", []string{"a.com", "b.com"}},
{" a.com b.com ", []string{"a.com", "b.com"}},
}
for _, tt := range tests {
if got := splitDomains(tt.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("splitDomains(%q) = %v, want %v", tt.in, got, tt.want)
}
}
}
func TestBuildRoute(t *testing.T) {
h := database.Host{
Domains: "example.com www.example.com",
Upstreams: []database.Upstream{
{Backend: "127.0.0.1:8080"},
{Backend: "127.0.0.1:8081"},
},
}
h.ID = 7
route := buildRoute(h)
if route["@id"] != "cpm_host_7" {
t.Errorf("@id = %v, want cpm_host_7", route["@id"])
}
if route["terminal"] != true {
t.Errorf("terminal = %v, want true", route["terminal"])
}
match := route["match"].([]map[string]any)
hosts := match[0]["host"].([]string)
if !reflect.DeepEqual(hosts, []string{"example.com", "www.example.com"}) {
t.Errorf("hosts = %v", hosts)
}
handle := route["handle"].([]map[string]any)
if handle[0]["handler"] != "reverse_proxy" {
t.Errorf("handler = %v, want reverse_proxy", handle[0]["handler"])
}
ups := handle[0]["upstreams"].([]map[string]any)
if len(ups) != 2 || ups[0]["dial"] != "127.0.0.1:8080" || ups[1]["dial"] != "127.0.0.1:8081" {
t.Errorf("upstreams = %v", ups)
}
}