120 lines
2.3 KiB
Go
120 lines
2.3 KiB
Go
package rules
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestHostnameRule_Matches(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
rule *HostnameRule
|
|
metadata *URLMetadata
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "exact match",
|
|
rule: &HostnameRule{
|
|
Hostname: "example.com",
|
|
Archivers: []ArchiverConfig{{Key: "obelisk"}},
|
|
},
|
|
metadata: &URLMetadata{
|
|
Domain: "example.com",
|
|
},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "wildcard match",
|
|
rule: &HostnameRule{
|
|
Hostname: "*.github.com",
|
|
Archivers: []ArchiverConfig{{Key: "direct_download"}},
|
|
},
|
|
metadata: &URLMetadata{
|
|
Domain: "api.github.com",
|
|
},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "partial match",
|
|
rule: &HostnameRule{
|
|
Hostname: "github",
|
|
Archivers: []ArchiverConfig{{Key: "direct_download"}},
|
|
},
|
|
metadata: &URLMetadata{
|
|
Domain: "api.github.com",
|
|
},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "no match",
|
|
rule: &HostnameRule{
|
|
Hostname: "example.com",
|
|
Archivers: []ArchiverConfig{{Key: "obelisk"}},
|
|
},
|
|
metadata: &URLMetadata{
|
|
Domain: "other.com",
|
|
},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "empty hostname in rule",
|
|
rule: &HostnameRule{
|
|
Hostname: "",
|
|
Archivers: []ArchiverConfig{{Key: "obelisk"}},
|
|
},
|
|
metadata: &URLMetadata{
|
|
Domain: "example.com",
|
|
},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
result, err := tt.rule.Matches(ctx, tt.metadata)
|
|
if err != nil {
|
|
t.Fatalf("Matches() error = %v", err)
|
|
}
|
|
|
|
matched := result != nil
|
|
if matched != tt.expected {
|
|
t.Errorf("Matches() matched = %v, want %v", matched, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHostnameRule_IsValid(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
rule *HostnameRule
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid rule",
|
|
rule: &HostnameRule{
|
|
Hostname: "example.com",
|
|
Archivers: []ArchiverConfig{{Key: "obelisk"}},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty hostname",
|
|
rule: &HostnameRule{
|
|
Hostname: "",
|
|
Archivers: []ArchiverConfig{{Key: "obelisk"}},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.rule.IsValid()
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("IsValid() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|