From b0ca037cad068e1a8ab9523faff5fdd2fb1aa6a5 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 20 Apr 2026 16:04:36 +0800 Subject: [PATCH] =?UTF-8?q?test(integration):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=AD=A3=E5=88=99=20location=20=E9=85=8D=E7=BD=AE=E9=9B=86?= =?UTF-8?q?=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 测试 ~ 修饰符大小写敏感匹配 - 测试 ~* 修饰符大小写不敏感匹配 - 测试 ^~ 修饰符非正则前缀优先匹配 Co-Authored-By: Claude Opus 4.7 --- internal/integration/regex_config_test.go | 60 +++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 internal/integration/regex_config_test.go diff --git a/internal/integration/regex_config_test.go b/internal/integration/regex_config_test.go new file mode 100644 index 0000000..590eff5 --- /dev/null +++ b/internal/integration/regex_config_test.go @@ -0,0 +1,60 @@ +package integration + +import ( + "testing" + + "github.com/valyala/fasthttp" + "rua.plus/lolly/internal/matcher" +) + +func TestRegexConfigCaseSensitive(t *testing.T) { + // 测试 ~ 修饰符(case-sensitive) + // 创建 regex matcher,验证只匹配小写 + m, err := matcher.NewRegexMatcher(`\.php$`, nil, 3, false) + if err != nil { + t.Fatal(err) + } + if !m.Match("/test.php") { + t.Error("~ modifier should match lowercase .php") + } + if m.Match("/test.PHP") { + t.Error("~ modifier should NOT match uppercase .PHP") + } +} + +func TestRegexConfigCaseInsensitive(t *testing.T) { + // 测试 ~* 修饰符(case-insensitive) + m, err := matcher.NewRegexMatcher(`(?i)\.php$`, nil, 3, true) + if err != nil { + t.Fatal(err) + } + if !m.Match("/test.php") { + t.Error("~* modifier should match lowercase .php") + } + if !m.Match("/test.PHP") { + t.Error("~* modifier should match uppercase .PHP") + } +} + +func TestPrefixPriorityNotRegex(t *testing.T) { + // 测试 ^~ 修饰符(非正则) + // 需要提供非 nil handler,否则 RadixTree 不会存储该节点 + dummyHandler := func(ctx *fasthttp.RequestCtx) {} + + engine := matcher.NewLocationEngine() + err := engine.AddPrefixPriority("/images", dummyHandler) + if err != nil { + t.Fatal(err) + } + + result := engine.Match("/images/logo.png") + if result == nil { + t.Error("^~ should match prefix") + } + if result.LocationType == matcher.LocationTypeRegex || result.LocationType == matcher.LocationTypeRegexCaseless { + t.Error("^~ should NOT be treated as regex") + } + if result.LocationType != matcher.LocationTypePrefixPriority { + t.Errorf("^~ should have LocationTypePrefixPriority, got: %s", result.LocationType) + } +}