From fe690fd13ce97845d4d42d0d5bf7ed7864c97a8f Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Sun, 8 Mar 2020 18:33:28 -0700 Subject: [PATCH] Fix regex being too greedy --- Ooui/UI.cs | 2 +- Tests/UITests.cs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Ooui/UI.cs b/Ooui/UI.cs index 8b35b96..eaac8d4 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -97,7 +97,7 @@ namespace Ooui public PublishedPath (string path, RequestHandler handler) { Path = path; - RegexPath = new Regex (path); + RegexPath = new Regex ("^" + path + "$"); Handler = handler; } } diff --git a/Tests/UITests.cs b/Tests/UITests.cs index de2425d..8639cdd 100644 --- a/Tests/UITests.cs +++ b/Tests/UITests.cs @@ -107,6 +107,26 @@ namespace Tests Assert.AreEqual ("\"nvirueh4\"", r); } + [TestMethod] + public void PatternUrlCompleteMatch () + { + bool gotRequest = false; + UI.PublishJson ("/", x => { + throw new Exception ("Pattern match failed to /"); + }); + UI.PublishJson ("/patter", x => { + throw new Exception ("Pattern match failed to /patter"); + }); + UI.PublishJson ("/pattern/(?[a-z0-9]+)", x => { + gotRequest = true; + Assert.AreEqual ("nvirueh4", x["id"]); + return x["id"]; + }); + var r = DownloadUI ("/pattern/nvirueh4"); + Assert.IsTrue (gotRequest); + Assert.AreEqual ("\"nvirueh4\"", r); + } + [TestMethod] public void PublishEmptyFile () {