From 48e02e01f7c6665921a8bfc4243a5c3fe37d3041 Mon Sep 17 00:00:00 2001 From: AndersHGP Date: Fri, 18 Oct 2024 05:27:47 +0200 Subject: [PATCH] Fix bug when new url is missing trailing slash --- .../Redirects/CustomRedirectCollection.cs | 2 +- .../CustomRedirectCollectionTests.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Geta.NotFoundHandler/Core/Redirects/CustomRedirectCollection.cs b/src/Geta.NotFoundHandler/Core/Redirects/CustomRedirectCollection.cs index c1f84c5a..adff85fe 100644 --- a/src/Geta.NotFoundHandler/Core/Redirects/CustomRedirectCollection.cs +++ b/src/Geta.NotFoundHandler/Core/Redirects/CustomRedirectCollection.cs @@ -222,7 +222,7 @@ private static void BuildPath( builder.Append(path); - if (hasAppendSegment && !pathHasTrailingSlash) + if (hasAppendSegment && !path.EndsWith("/")) { builder.Append('/'); } diff --git a/tests/Geta.NotFoundHandler.Tests/CustomRedirectCollectionTests.cs b/tests/Geta.NotFoundHandler.Tests/CustomRedirectCollectionTests.cs index 5e52ba0f..9071569d 100644 --- a/tests/Geta.NotFoundHandler.Tests/CustomRedirectCollectionTests.cs +++ b/tests/Geta.NotFoundHandler.Tests/CustomRedirectCollectionTests.cs @@ -399,5 +399,24 @@ public void Find_decodes_plus_sign_correctly() Assert.Equal(expected, actual.NewUrl); } + + /// + /// https://github.com/Geta/geta-notfoundhandler/issues/137 + /// + [Fact] + public void Find_does_add_slash_when_new_url_is_missing_trailing_slash() + { + var collection = new CustomRedirectCollection + { + new CustomRedirect("/content", "/legacy"), + }; + + var urlToFind = "/content/file/"; + var expected = "/legacy/file/"; + + var actual = collection.Find(urlToFind.ToUri()); + + Assert.Equal(expected, actual.NewUrl); + } } }