myxce/src/frontend/apiserver/middleware/whitelist_test.go

33 lines
969 B
Go

package middleware
import "testing"
func TestPathMatchesPattern(t *testing.T) {
cases := []struct {
pattern string
path string
want bool
}{
// wildcard patterns
{"/xce/v1/hosts/*", "/xce/v1/hosts/host1", true},
{"/xce/v1/jobs/*", "/xce/v1/jobs/123/control", true},
{"/xce/v1/roles/*", "/xce/v1/roles/cluster_admin", true},
{"/xce/v1/users/*", "/xce/v1/users/current", true},
{"/xce/v1/users/*", "/xce/v1/users/admin/basic_user", true},
// exact patterns
{"/xce/v1/hosts", "/xce/v1/hosts", true},
{"/xce/v1/hosts", "/xce/v1/host", false},
{"/xce/v1/roles", "/xce/v1/roles", true},
{"/xce/v1/roles", "/xce/v1/role", false},
{"/xce/v1/clusters", "/xce/v1/clusters", true},
{"/ping", "/ping", true},
{"/ping", "/pin", false},
}
for _, tc := range cases {
got := pathMatchesPattern(tc.path, tc.pattern)
if got != tc.want {
t.Errorf("pathMatchesPattern(%q, %q) = %v, want %v", tc.path, tc.pattern, got, tc.want)
}
}
}