package server
import "testing"
func TestPrefixMatch(t *testing.T) {
m := map[string]bool{
"/": true,
"/readonly/": false,
"/deep/sub/": true,
}
cases := []struct {
name string
want bool
}{
// Root prefix.
{"/", true},
{"/anything", true},
{"/anything/else", true},
// Longest prefix wins.
{"/readonly/x", false},
// filepath.Clean strips the trailing slash from both name and
// prefix, so "/readonly" matches the "/readonly/" entry too.
{"/readonly", false},
{"/deep/sub/x", true},
// Siblings that share the string prefix, but not the path segment,
// don't match it; they get the value from "/" instead.
{"/readonly2", true},
{"/readonly2/x", true},
// Empty name treated as "/".
{"", true},
}
for _, c := range cases {
got := prefixMatch(m, c.name)
if got != c.want {
t.Errorf("prefixMatch(%q) = %v, want %v", c.name, got, c.want)
}
}
// Empty map -> always false.
if prefixMatch(nil, "/anything") {
t.Errorf("prefixMatch(nil, ...) = true, want false")
}
}
// Without a "/" entry, siblings of an enabled prefix must not be enabled.
func TestPrefixMatchSiblings(t *testing.T) {
m := map[string]bool{"/pub/": true}
cases := []struct {
name string
want bool
}{
{"/pub", true},
{"/pub/", true},
{"/pub/x", true},
{"/pub/sub/x", true},
{"/", false},
{"/public", false},
{"/public/secret.txt", false},
{"/pubfoo", false},
{"/pub.txt", false},
{"/other/pub/x", false},
}
for _, c := range cases {
got := prefixMatch(m, c.name)
if got != c.want {
t.Errorf("prefixMatch(%q) = %v, want %v", c.name, got, c.want)
}
}
}