Skip to content

Commit 6dadc64

Browse files
committed
tests: add tests for Validate
This also attempts to make the host name regex a bit more readable.
1 parent 31ab0ac commit 6dadc64

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

plugin.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
var hostRegex *regexp.Regexp
21-
var cacheKey string = "hosts"
21+
22+
const cacheKey = "hosts"
2223

2324
func init() {
2425
caddy.RegisterModule(MatchRemoteHost{})
@@ -88,7 +89,15 @@ func (m *MatchRemoteHost) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
8889
func (m *MatchRemoteHost) Provision(ctx caddy.Context) (err error) {
8990
m.logger = ctx.Logger()
9091
m.cache = cache.New(1*time.Minute, 2*time.Minute)
91-
hostRegex, err = regexp.Compile(`^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))$`)
92+
93+
const (
94+
alnum = "[a-zA-Z0-9]"
95+
alnumDash = "[-a-zA-Z0-9]"
96+
label = "(" + alnum + "|" + alnum + alnumDash + "*" + alnum + ")"
97+
hostname = "^(" + label + `\.)*` + label + "$"
98+
)
99+
100+
hostRegex, err = regexp.Compile(hostname)
92101
return err
93102
}
94103

plugin_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package caddy_remote_host_test
44
// methods see package caddy_remote_host.
55

66
import (
7+
"fmt"
78
"testing"
89

10+
"github.com/caddyserver/caddy/v2"
911
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
1012
plugin "github.com/muety/caddy-remote-host"
1113
"github.com/stretchr/testify/assert"
@@ -114,3 +116,51 @@ func TestMatchRemoteHost_UnmarshalCaddyfile_invalid(t *testing.T) {
114116
})
115117
}
116118
}
119+
120+
func TestMatchRemoteHost_Validate(t *testing.T) {
121+
for name, hosts := range map[string][]string{
122+
"single": {"example"},
123+
"simple": {"example.com"},
124+
"multiple": {"example.com", "example.org"},
125+
"subdomain": {"sub.example.com"},
126+
"hyphens": {"ex-am-ple.com"},
127+
"digits": {"example24.com"},
128+
"leading digits": {"42example.org"},
129+
"only digits": {"42.example"},
130+
} {
131+
hosts := hosts
132+
t.Run(name, func(t *testing.T) {
133+
t.Parallel()
134+
135+
subject := plugin.MatchRemoteHost{Hosts: hosts}
136+
require.NoError(t, subject.Provision(caddy.Context{}))
137+
assert.NoError(t, subject.Validate())
138+
})
139+
}
140+
}
141+
142+
func TestMatchRemoteHost_Validate_invalid(t *testing.T) {
143+
require := require.New(t)
144+
assert := assert.New(t)
145+
146+
for name, host := range map[string]string{
147+
"dot": ".",
148+
"double dot": "example..com",
149+
"leading dot": ".example.org",
150+
"leading dash": "-example.org",
151+
"trailing dash": "example-.org",
152+
"underscore": "_http.example",
153+
"non-acsii": "ëxample.com",
154+
"wildcard": "*.example.com",
155+
} {
156+
host := host
157+
t.Run(name, func(t *testing.T) {
158+
t.Parallel()
159+
160+
subject := plugin.MatchRemoteHost{Hosts: []string{host}}
161+
require.NoError(subject.Provision(caddy.Context{}))
162+
assert.EqualError(subject.Validate(),
163+
fmt.Sprintf("'%s' is not a valid host name", host))
164+
})
165+
}
166+
}

0 commit comments

Comments
 (0)