Introduce cmd/lint to statically verify operator templates vs routes (internal/lint). Replace interpolated hx-* URL strings with operatorURL calls in partials, register operatorURL in the template FuncMap, add server/operator_url.go with unit tests, and update go.mod. Add routeURL helper and template linter
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package web
|
|
|
|
import "testing"
|
|
|
|
func TestRouteURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pattern string
|
|
args []any
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "no placeholders",
|
|
pattern: "/partials/operator/products",
|
|
want: "/partials/operator/products",
|
|
},
|
|
{
|
|
name: "single placeholder",
|
|
pattern: "/partials/operator/products/{productID}",
|
|
args: []any{42},
|
|
want: "/partials/operator/products/42",
|
|
},
|
|
{
|
|
name: "two placeholders",
|
|
pattern: "/partials/operator/organizations/{orgID}/pools/{poolID}/grant",
|
|
args: []any{"org-abc", "pool-xyz"},
|
|
want: "/partials/operator/organizations/org-abc/pools/pool-xyz/grant",
|
|
},
|
|
{
|
|
name: "value needs path escape",
|
|
pattern: "/partials/operator/org-types/{orgType}/default-plan",
|
|
args: []any{"co-op/personal"},
|
|
want: "/partials/operator/org-types/co-op%2Fpersonal/default-plan",
|
|
},
|
|
{
|
|
name: "trailing wildcard placeholder treated like normal",
|
|
pattern: "/foo/{rest...}",
|
|
args: []any{"a/b"},
|
|
want: "/foo/a%2Fb",
|
|
},
|
|
{
|
|
name: "too few args",
|
|
pattern: "/foo/{a}/bar/{b}",
|
|
args: []any{"x"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "too many args",
|
|
pattern: "/foo/{a}",
|
|
args: []any{"x", "y"},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "unclosed placeholder",
|
|
pattern: "/foo/{a",
|
|
args: []any{"x"},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := RouteURL(tt.pattern, tt.args...)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Fatalf("RouteURL() err=%v wantErr=%v", err, tt.wantErr)
|
|
}
|
|
if !tt.wantErr && got != tt.want {
|
|
t.Errorf("RouteURL() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|