91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
package rules
|
|
|
|
import "testing"
|
|
|
|
func TestMatchesPattern(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pattern string
|
|
value string
|
|
expected bool
|
|
}{
|
|
// Exact matches
|
|
{
|
|
name: "exact match",
|
|
pattern: "text/html",
|
|
value: "text/html",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "exact match no match",
|
|
pattern: "text/html",
|
|
value: "text/plain",
|
|
expected: false,
|
|
},
|
|
// Wildcard matches
|
|
{
|
|
name: "wildcard prefix",
|
|
pattern: "application/*",
|
|
value: "application/pdf",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "wildcard prefix no match",
|
|
pattern: "application/*",
|
|
value: "text/html",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "wildcard suffix",
|
|
pattern: "*.github.com",
|
|
value: "api.github.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "wildcard suffix no match",
|
|
pattern: "*.github.com",
|
|
value: "api.gitlab.com",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "wildcard middle",
|
|
pattern: "api.*.com",
|
|
value: "api.github.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "wildcard middle no match",
|
|
pattern: "api.*.com",
|
|
value: "api.github.org",
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "wildcard only",
|
|
pattern: "*",
|
|
value: "anything",
|
|
expected: true,
|
|
},
|
|
// Partial matches (contains)
|
|
{
|
|
name: "partial match",
|
|
pattern: "github",
|
|
value: "api.github.com",
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "partial match no match",
|
|
pattern: "github",
|
|
value: "api.gitlab.com",
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := matchesPattern(tt.pattern, tt.value)
|
|
if result != tt.expected {
|
|
t.Errorf("matchesPattern(%q, %q) = %v, want %v", tt.pattern, tt.value, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|