Files
Charon/backend/internal/caddy/importer_additional_test.go
GitHub Actions 63cebf07ab Refactor services and improve error handling
- Updated file permissions in certificate_service_test.go and log_service_test.go to use octal notation.
- Added a new doc.go file to document the services package.
- Enhanced error handling in docker_service.go, log_service.go, notification_service.go, proxyhost_service.go, remoteserver_service.go, update_service.go, and uptime_service.go by logging errors when closing resources.
- Improved log_service.go to simplify log file processing and deduplication.
- Introduced CRUD tests for notification templates in notification_service_template_test.go.
- Removed the obsolete python_compile_check.sh script.
- Updated notification_service.go to improve template management functions.
- Added tests for uptime service notifications in uptime_service_notification_test.go.
2025-12-08 05:55:17 +00:00

63 lines
2.0 KiB
Go

package caddy
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestImporter_ExtractHosts_DialWithoutPortDefaultsTo80(t *testing.T) {
importer := NewImporter("caddy")
rawJSON := []byte("{\"apps\":{\"http\":{\"servers\":{\"srv0\":{\"routes\":[{\"match\":[{\"host\":[\"nop.example.com\"]}],\"handle\":[{\"handler\":\"reverse_proxy\",\"upstreams\":[{\"dial\":\"example.com\"}]}]}]}}}}}")
res, err := importer.ExtractHosts(rawJSON)
assert.NoError(t, err)
assert.Len(t, res.Hosts, 1)
host := res.Hosts[0]
assert.Equal(t, "example.com", host.ForwardHost)
assert.Equal(t, 80, host.ForwardPort)
}
func TestImporter_ExtractHosts_DetectsWebsocketFromHeaders(t *testing.T) {
importer := NewImporter("caddy")
rawJSON := []byte("{\"apps\":{\"http\":{\"servers\":{\"srv0\":{\"routes\":[{\"match\":[{\"host\":[\"ws.example.com\"]}],\"handle\":[{\"handler\":\"reverse_proxy\",\"upstreams\":[{\"dial\":\"127.0.0.1:8080\"}],\"headers\":{\"Upgrade\":[\"websocket\"]}}]}]}}}}}")
res, err := importer.ExtractHosts(rawJSON)
assert.NoError(t, err)
assert.Len(t, res.Hosts, 1)
host := res.Hosts[0]
assert.True(t, host.WebsocketSupport)
}
func TestImporter_ImportFile_ParseOutputInvalidJSON(t *testing.T) {
importer := NewImporter("caddy")
mockExecutor := &MockExecutor{Output: []byte("{invalid"), Err: nil}
importer.executor = mockExecutor
// Create a dummy file
tmpFile := filepath.Join(t.TempDir(), "Caddyfile")
err := os.WriteFile(tmpFile, []byte("foo"), 0o644)
assert.NoError(t, err)
_, err = importer.ImportFile(tmpFile)
assert.Error(t, err)
}
func TestImporter_ImportFile_ExecutorError(t *testing.T) {
importer := NewImporter("caddy")
mockExecutor := &MockExecutor{Output: []byte(""), Err: assert.AnError}
importer.executor = mockExecutor
// Create a dummy file
tmpFile := filepath.Join(t.TempDir(), "Caddyfile")
err := os.WriteFile(tmpFile, []byte("foo"), 0o644)
assert.NoError(t, err)
_, err = importer.ImportFile(tmpFile)
assert.Error(t, err)
}