From 0c3268c1e127f08aa2f4ab36f49e9c81deb39e22 Mon Sep 17 00:00:00 2001 From: espierre Date: Mon, 8 Jun 2020 15:22:15 -0400 Subject: [PATCH 1/5] Fix Issue-171 by adding a validation to check for querystring parameters before adding the trailing slash --- .../Core/CustomRedirects/CustomRedirectCollection.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs index 2be62a3..be8d311 100644 --- a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs +++ b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs @@ -144,7 +144,16 @@ string RemoveSlash(string s) } var redirCopy = new CustomRedirect(cr); - var newUrl = AppendSlash(redirCopy.NewUrl); + var newUrl = string.Empty; + if (url.IndexOf("?", StringComparison.Ordinal) > 0) + { + newUrl = redirCopy.NewUrl; + } + else + { + newUrl = AppendSlash(redirCopy.NewUrl); + } + var appendSegment = RemoveSlash(url.Substring(oldUrl.Length)); redirCopy.NewUrl = $"{newUrl}{appendSegment}"; return redirCopy; From d9377c84d58d38c49c4c29711ac1c1c5e37af973 Mon Sep 17 00:00:00 2001 From: espierre Date: Tue, 9 Jun 2020 08:37:14 -0400 Subject: [PATCH 2/5] issue-171 - Add unit tests --- .../CustomRedirectCollectionTests.cs | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs b/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs index c124919..b6cb3ef 100644 --- a/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs +++ b/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Web; using BVNetwork.NotFound.Core; @@ -276,5 +276,65 @@ public void Find_gives_nullreferenceexception_with_partial_deleted_hit() Assert.NotNull(actual); } + + /// + /// https://github.com/Geta/404handler/issues/171 + /// + [Fact] + public void Find_does_not_add_slash_when_querystring_parameter() + { + var collection = new CustomRedirectCollection + { + new CustomRedirect("/content/file", "/legacy"), + new CustomRedirect("/content/file/", "/legacy/") + }; + + var urlToFind = "/content/file?doc=filename"; + var expected = "/legacy?doc=filename"; + + var actual = collection.Find(urlToFind.ToUri()); + + Assert.Equal(expected, actual.NewUrl); + } + + /// + /// https://github.com/Geta/404handler/issues/171 + /// + [Fact] + public void Find_does_heep_slash_when_querystring_parameter() + { + var collection = new CustomRedirectCollection + { + new CustomRedirect("/content/file", "/legacy"), + new CustomRedirect("/content/file/", "/legacy/") + }; + + var urlToFind = "/content/file/?doc=filename"; + var expected = "/legacy/?doc=filename"; + + var actual = collection.Find(urlToFind.ToUri()); + + Assert.Equal(expected, actual.NewUrl); + } + + /// + /// https://github.com/Geta/404handler/issues/171 + /// + [Fact] + public void Find_does_not_add_slash_when_querystring_parameters() + { + var collection = new CustomRedirectCollection + { + new CustomRedirect("/content/file", "/legacy"), + new CustomRedirect("/content/file/", "/legacy/") + }; + + var urlToFind = "/content/file?doc=filename¶m2=value"; + var expected = "/legacy?doc=filename¶m2=value"; + + var actual = collection.Find(urlToFind.ToUri()); + + Assert.Equal(expected, actual.NewUrl); + } } -} \ No newline at end of file +} From 39d67e992bf733afacf6d8af08850f1112243777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Krivte=C5=BEs?= Date: Tue, 9 Jun 2020 15:46:26 +0300 Subject: [PATCH 3/5] Update tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs --- tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs b/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs index b6cb3ef..be6cb04 100644 --- a/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs +++ b/tests/Geta.404Handler.Tests/CustomRedirectCollectionTests.cs @@ -301,7 +301,7 @@ public void Find_does_not_add_slash_when_querystring_parameter() /// https://github.com/Geta/404handler/issues/171 /// [Fact] - public void Find_does_heep_slash_when_querystring_parameter() + public void Find_does_keep_slash_when_querystring_parameter() { var collection = new CustomRedirectCollection { From 5fef2a7f0294a04b58ee8894476eb9fb6f3c73ee Mon Sep 17 00:00:00 2001 From: espierre Date: Tue, 9 Jun 2020 11:50:35 -0400 Subject: [PATCH 4/5] issue-171 - Change if statement to ternary operator --- .../Core/CustomRedirects/CustomRedirectCollection.cs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs index be8d311..97f046e 100644 --- a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs +++ b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs @@ -144,16 +144,7 @@ string RemoveSlash(string s) } var redirCopy = new CustomRedirect(cr); - var newUrl = string.Empty; - if (url.IndexOf("?", StringComparison.Ordinal) > 0) - { - newUrl = redirCopy.NewUrl; - } - else - { - newUrl = AppendSlash(redirCopy.NewUrl); - } - + var newUrl = (url.IndexOf("?", StringComparison.Ordinal) > 0) ? redirCopy.NewUrl : AppendSlash(redirCopy.NewUrl); var appendSegment = RemoveSlash(url.Substring(oldUrl.Length)); redirCopy.NewUrl = $"{newUrl}{appendSegment}"; return redirCopy; From 9358e2c04f754a8a297755bde9d9d3dd64dc852f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Krivte=C5=BEs?= Date: Wed, 10 Jun 2020 12:46:29 +0300 Subject: [PATCH 5/5] Update src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs --- .../Core/CustomRedirects/CustomRedirectCollection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs index 97f046e..66a5ba1 100644 --- a/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs +++ b/src/Geta.404Handler/Core/CustomRedirects/CustomRedirectCollection.cs @@ -144,7 +144,7 @@ string RemoveSlash(string s) } var redirCopy = new CustomRedirect(cr); - var newUrl = (url.IndexOf("?", StringComparison.Ordinal) > 0) ? redirCopy.NewUrl : AppendSlash(redirCopy.NewUrl); + var newUrl = url.IndexOf("?", StringComparison.Ordinal) > 0 ? redirCopy.NewUrl : AppendSlash(redirCopy.NewUrl); var appendSegment = RemoveSlash(url.Substring(oldUrl.Length)); redirCopy.NewUrl = $"{newUrl}{appendSegment}"; return redirCopy;