test(matcher): update test callers for []byte Match/FindLongestPrefix

This commit is contained in:
xfy 2026-06-04 11:06:09 +08:00
parent 1eeab88c98
commit bd97c05d0d
5 changed files with 29 additions and 29 deletions

View File

@ -47,7 +47,7 @@ func TestPrefixPriorityNotRegex(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
result := engine.Match("/images/logo.png") result := engine.Match([]byte("/images/logo.png"))
if result == nil { if result == nil {
t.Error("^~ should match prefix") t.Error("^~ should match prefix")
} }

View File

@ -20,13 +20,13 @@ func TestLocationEngine_NginxPriority(t *testing.T) {
engine.MarkInitialized() engine.MarkInitialized()
// 测试精确匹配优先 // 测试精确匹配优先
result := engine.Match("/api") result := engine.Match([]byte("/api"))
if result.LocationType != "exact" { if result.LocationType != "exact" {
t.Errorf("expected exact, got %s", result.LocationType) t.Errorf("expected exact, got %s", result.LocationType)
} }
// 测试 ^~ 阻止正则 // 测试 ^~ 阻止正则
result = engine.Match("/api/test.php") result = engine.Match([]byte("/api/test.php"))
if result.LocationType != "prefix_priority" { if result.LocationType != "prefix_priority" {
t.Errorf("^~ should block regex, got %s", result.LocationType) t.Errorf("^~ should block regex, got %s", result.LocationType)
} }
@ -42,7 +42,7 @@ func TestLocationEngine_RegexMatch(t *testing.T) {
engine.MarkInitialized() engine.MarkInitialized()
// 正则匹配(^~ 不匹配 /index.php // 正则匹配(^~ 不匹配 /index.php
result := engine.Match("/index.php") result := engine.Match([]byte("/index.php"))
if result.LocationType != "regex" { if result.LocationType != "regex" {
t.Errorf("expected regex for /index.php, got %s", result.LocationType) t.Errorf("expected regex for /index.php, got %s", result.LocationType)
} }
@ -55,7 +55,7 @@ func TestLocationEngine_PrefixFallback(t *testing.T) {
engine.AddPrefix("/", handler, false) engine.AddPrefix("/", handler, false)
engine.MarkInitialized() engine.MarkInitialized()
result := engine.Match("/any/path") result := engine.Match([]byte("/any/path"))
if result == nil || result.LocationType != "prefix" { if result == nil || result.LocationType != "prefix" {
t.Errorf("expected prefix match, got %v", result) t.Errorf("expected prefix match, got %v", result)
} }
@ -65,7 +65,7 @@ func TestLocationEngine_NoMatch(t *testing.T) {
engine := NewLocationEngine() engine := NewLocationEngine()
engine.MarkInitialized() engine.MarkInitialized()
result := engine.Match("/nonexistent") result := engine.Match([]byte("/nonexistent"))
if result != nil { if result != nil {
t.Errorf("expected nil for no match, got %+v", result) t.Errorf("expected nil for no match, got %+v", result)
} }
@ -78,7 +78,7 @@ func TestLocationEngine_RegexCaptures(t *testing.T) {
engine.AddRegex(`^/user/(?P<id>[0-9]+)$`, handler, false, false) engine.AddRegex(`^/user/(?P<id>[0-9]+)$`, handler, false, false)
engine.MarkInitialized() engine.MarkInitialized()
result := engine.Match("/user/42") result := engine.Match([]byte("/user/42"))
if result.LocationType != "regex" { if result.LocationType != "regex" {
t.Errorf("expected regex, got %s", result.LocationType) t.Errorf("expected regex, got %s", result.LocationType)
} }

View File

@ -37,7 +37,7 @@ func TestLocationEngine_AddExact(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/api") result := engine.Match([]byte("/api"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -80,7 +80,7 @@ func TestLocationEngine_AddPrefixPriority(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/static/css/style.css") result := engine.Match([]byte("/static/css/style.css"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -112,7 +112,7 @@ func TestLocationEngine_AddPrefix(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/api/users") result := engine.Match([]byte("/api/users"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -141,7 +141,7 @@ func TestLocationEngine_AddRegex(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/index.php") result := engine.Match([]byte("/index.php"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -159,7 +159,7 @@ func TestLocationEngine_AddRegex_CaseInsensitive(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/index.PHP") result := engine.Match([]byte("/index.PHP"))
if result == nil { if result == nil {
t.Fatal("expected match for case insensitive") t.Fatal("expected match for case insensitive")
} }
@ -187,7 +187,7 @@ func TestLocationEngine_AddRegex_Captures(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
result := engine.Match("/user/123") result := engine.Match([]byte("/user/123"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -213,7 +213,7 @@ func TestLocationEngine_Match_PriorityOrder(t *testing.T) {
engine.AddPrefix("/api/path", hPrefix, false) engine.AddPrefix("/api/path", hPrefix, false)
// Exact should win (priority 1) // Exact should win (priority 1)
result := engine.Match("/api/path") result := engine.Match([]byte("/api/path"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -232,7 +232,7 @@ func TestLocationEngine_Match_PrefixPriorityBeatsRegex(t *testing.T) {
engine.AddRegex(`\.css$`, hRegex, false, false) engine.AddRegex(`\.css$`, hRegex, false, false)
// ^~ prefix priority should beat regex // ^~ prefix priority should beat regex
result := engine.Match("/static/style.css") result := engine.Match([]byte("/static/style.css"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -250,7 +250,7 @@ func TestLocationEngine_Match_RegexBeatsPrefix(t *testing.T) {
engine.AddPrefix("/", hPrefix, false) engine.AddPrefix("/", hPrefix, false)
// Regex should win over plain prefix // Regex should win over plain prefix
result := engine.Match("/index.php") result := engine.Match([]byte("/index.php"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -265,7 +265,7 @@ func TestLocationEngine_Match_FallbackToPrefix(t *testing.T) {
engine.AddPrefix("/api", hPrefix, false) engine.AddPrefix("/api", hPrefix, false)
result := engine.Match("/api/users") result := engine.Match([]byte("/api/users"))
if result == nil { if result == nil {
t.Fatal("expected prefix match") t.Fatal("expected prefix match")
} }
@ -280,7 +280,7 @@ func TestLocationEngine_Match_NoMatch(t *testing.T) {
engine.AddPrefix("/api", hPrefix, false) engine.AddPrefix("/api", hPrefix, false)
result := engine.Match("/other") result := engine.Match([]byte("/other"))
if result != nil { if result != nil {
t.Errorf("expected no match, got %+v", result) t.Errorf("expected no match, got %+v", result)
} }
@ -292,7 +292,7 @@ func TestLocationEngine_Match_EmptyString(t *testing.T) {
engine.AddPrefix("/api", hPrefix, false) engine.AddPrefix("/api", hPrefix, false)
result := engine.Match("") result := engine.Match([]byte(""))
if result != nil { if result != nil {
t.Errorf("expected no match for empty string, got %+v", result) t.Errorf("expected no match for empty string, got %+v", result)
} }
@ -304,7 +304,7 @@ func TestLocationEngine_Match_UnicodePath(t *testing.T) {
engine.AddPrefixPriority("/文档", handler, false) engine.AddPrefixPriority("/文档", handler, false)
result := engine.Match("/文档/报告") result := engine.Match([]byte("/文档/报告"))
if result == nil { if result == nil {
t.Fatal("expected unicode prefix match") t.Fatal("expected unicode prefix match")
} }

View File

@ -18,7 +18,7 @@ func BenchmarkRadixTreeFindLongestPrefix(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for b.Loop() { for b.Loop() {
result := tree.FindLongestPrefix("/api/v1/users") result := tree.FindLongestPrefix([]byte("/api/v1/users"))
ReleaseMatchResult(result) ReleaseMatchResult(result)
} }
} }
@ -36,7 +36,7 @@ func BenchmarkRadixTreeFindLongestPrefixParallel(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
result := tree.FindLongestPrefix("/api/v1/users") result := tree.FindLongestPrefix([]byte("/api/v1/users"))
ReleaseMatchResult(result) ReleaseMatchResult(result)
} }
}) })

View File

@ -16,7 +16,7 @@ func TestRadixTree_Insert_EmptyNode(t *testing.T) {
t.Fatalf("insert failed: %v", err) t.Fatalf("insert failed: %v", err)
} }
result := tree.FindLongestPrefix("/api") result := tree.FindLongestPrefix([]byte("/api"))
if result == nil { if result == nil {
t.Error("should find inserted path") t.Error("should find inserted path")
} }
@ -34,7 +34,7 @@ func TestRadixTree_Insert_CommonPrefix(t *testing.T) {
tree.Insert("/api", handler1, 1, "prefix", false) tree.Insert("/api", handler1, 1, "prefix", false)
tree.Insert("/api/users", handler2, 2, "prefix", false) tree.Insert("/api/users", handler2, 2, "prefix", false)
result := tree.FindLongestPrefix("/api/users") result := tree.FindLongestPrefix([]byte("/api/users"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -57,7 +57,7 @@ func TestRadixTree_Insert_NodeSplit(t *testing.T) {
tree.Insert("/abx", handler2, 2, "prefix", false) tree.Insert("/abx", handler2, 2, "prefix", false)
// 应该正确分割 /ab 公共前缀 // 应该正确分割 /ab 公共前缀
result := tree.FindLongestPrefix("/abc") result := tree.FindLongestPrefix([]byte("/abc"))
if result == nil { if result == nil {
t.Error("should find /abc after split") t.Error("should find /abc after split")
} }
@ -73,7 +73,7 @@ func TestRadixTree_FindLongestPrefix(t *testing.T) {
// "/" has priority 1 (wins), "/api" has 2, "/api/v1" has 3 // "/" has priority 1 (wins), "/api" has 2, "/api/v1" has 3
// Lower number = higher priority // Lower number = higher priority
result := tree.FindLongestPrefix("/api/v1/users") result := tree.FindLongestPrefix([]byte("/api/v1/users"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -112,7 +112,7 @@ func TestRadixTree_FindLongestPrefix_NoMatch(t *testing.T) {
tree.Insert("/api", handler, 1, "prefix", false) tree.Insert("/api", handler, 1, "prefix", false)
result := tree.FindLongestPrefix("/other") result := tree.FindLongestPrefix([]byte("/other"))
if result != nil { if result != nil {
t.Errorf("expected nil for no match, got %+v", result) t.Errorf("expected nil for no match, got %+v", result)
} }
@ -127,7 +127,7 @@ func TestRadixTree_PriorityComparison(t *testing.T) {
tree.Insert("/api/users", h2, 2, "prefix", false) tree.Insert("/api/users", h2, 2, "prefix", false)
// Lower priority number wins // Lower priority number wins
result := tree.FindLongestPrefix("/api/users") result := tree.FindLongestPrefix([]byte("/api/users"))
if result == nil { if result == nil {
t.Fatal("expected match") t.Fatal("expected match")
} }
@ -151,7 +151,7 @@ func TestRadixTree_Insert_ExactMatch(t *testing.T) {
} }
// 验证原 handler 未被覆盖 // 验证原 handler 未被覆盖
result := tree.FindLongestPrefix("/api") result := tree.FindLongestPrefix([]byte("/api"))
if result == nil || result.Priority != 1 { if result == nil || result.Priority != 1 {
t.Errorf("original handler should not be overwritten, got priority %d", result.Priority) t.Errorf("original handler should not be overwritten, got priority %d", result.Priority)
} }