Files
Charon/backend/internal/caddy/validator_additional_test.go
GitHub Actions af8384046c chore: implement instruction compliance remediation
- Replace Go interface{} with any (Go 1.18+ standard)
- Add database indexes to frequently queried model fields
- Add JSDoc documentation to frontend API client methods
- Remove deprecated docker-compose version keys
- Add concurrency groups to all 25 GitHub Actions workflows
- Add YAML front matter and fix H1→H2 headings in docs

Coverage: Backend 85.5%, Frontend 87.73%
Security: No vulnerabilities detected

Refs: docs/plans/instruction_compliance_spec.md
2025-12-21 04:08:42 +00:00

85 lines
2.3 KiB
Go

package caddy
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestValidate_NilConfig(t *testing.T) {
err := Validate(nil)
require.Error(t, err)
require.Contains(t, err.Error(), "config cannot be nil")
}
func TestValidateHandler_MissingHandlerField(t *testing.T) {
// Handler without a 'handler' key
h := Handler{"foo": "bar"}
err := validateHandler(h)
require.Error(t, err)
require.Contains(t, err.Error(), "missing 'handler' field")
}
func TestValidateHandler_UnknownHandlerAllowed(t *testing.T) {
// Unknown handler type should be allowed
h := Handler{"handler": "custom_handler"}
err := validateHandler(h)
require.NoError(t, err)
}
func TestValidateHandler_FileServerAndStaticResponseAllowed(t *testing.T) {
h1 := Handler{"handler": "file_server"}
err := validateHandler(h1)
require.NoError(t, err)
h2 := Handler{"handler": "static_response"}
err = validateHandler(h2)
require.NoError(t, err)
}
func TestValidateRoute_InvalidHandler(t *testing.T) {
config := &Config{
Apps: Apps{
HTTP: &HTTPApp{
Servers: map[string]*Server{
"srv": {
Listen: []string{":80"},
Routes: []*Route{{
Match: []Match{{Host: []string{"test.invalid"}}},
Handle: []Handler{{"foo": "bar"}},
}},
},
},
},
},
}
err := Validate(config)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid handler")
}
func TestValidateListenAddr_InvalidHostName(t *testing.T) {
err := validateListenAddr("example.com:80")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid IP address")
}
func TestValidateListenAddr_InvalidPortNonNumeric(t *testing.T) {
err := validateListenAddr(":abc")
require.Error(t, err)
require.Contains(t, err.Error(), "invalid port")
}
func TestValidate_MarshalError(t *testing.T) {
// stub jsonMarshalValidate to cause Marshal error
orig := jsonMarshalValidate
jsonMarshalValidate = func(v any) ([]byte, error) { return nil, fmt.Errorf("marshal error") }
defer func() { jsonMarshalValidate = orig }()
cfg := &Config{Apps: Apps{HTTP: &HTTPApp{Servers: map[string]*Server{"srv": {Listen: []string{":80"}, Routes: []*Route{{Match: []Match{{Host: []string{"x.com"}}}, Handle: []Handler{{"handler": "file_server"}}}}}}}}}
err := Validate(cfg)
require.Error(t, err)
require.Contains(t, err.Error(), "config cannot be marshalled")
}