From 9b85f4ca60c92d007e601de7e075b2f1cf78d8f3 Mon Sep 17 00:00:00 2001 From: Lachlan Barclay Date: Wed, 19 Aug 2020 01:50:59 +1000 Subject: [PATCH] Fixed breadcrumb links for packages with build metadata (#8157) * Fixed breadcrumb links for packages with build metadata #8136 * added null reference check * added unit test * removed extraneous newlines * fixed test as links now have normalised version numbers on the end Address https://github.com/NuGet/NuGetGallery/issues/8136 --- src/NuGetGallery/UrlHelperExtensions.cs | 4 +++- .../Controllers/ApiControllerFacts.cs | 4 ++-- .../UrlHelperExtensionsFacts.cs | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/NuGetGallery/UrlHelperExtensions.cs b/src/NuGetGallery/UrlHelperExtensions.cs index f841cde23e..82afea5df0 100644 --- a/src/NuGetGallery/UrlHelperExtensions.cs +++ b/src/NuGetGallery/UrlHelperExtensions.cs @@ -228,6 +228,8 @@ public static string Package( string version, bool relativeUrl = true) { + string normalized = (version != null) ? NuGetVersionFormatter.Normalize(version) : version; + string result = GetRouteLink( url, RouteName.DisplayPackage, @@ -235,7 +237,7 @@ public static string Package( routeValues: new RouteValueDictionary { { "id", id }, - { "version", version } + { "version", normalized } }); // Ensure trailing slashes for versionless package URLs, as a fix for package filenames that look like known file extensions diff --git a/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs b/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs index 70a6689bd4..813d2011f0 100644 --- a/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs +++ b/tests/NuGetGallery.Facts/Controllers/ApiControllerFacts.cs @@ -2856,8 +2856,8 @@ public async Task VerifyRecentPopularityStatsDownloads() JArray result = JArray.Parse(contentResult.Content); - Assert.True((string)result[3]["Gallery"] == "/packages/B/1.1", "unexpected content result[3].Gallery"); - Assert.True((int)result[2]["Downloads"] == 5, "unexpected content result[2].Downloads"); + Assert.Equal("/packages/B/1.1.0", (string)result[3]["Gallery"]); + Assert.Equal(5, (int)result[2]["Downloads"]); } [Fact] diff --git a/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs b/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs index fe923abe0f..d4c86ffb6e 100644 --- a/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs +++ b/tests/NuGetGallery.Facts/UrlHelperExtensionsFacts.cs @@ -30,6 +30,29 @@ public void PropagatesNull() Assert.Null(fixedUrl); } } + + public class ThePackageBaseHelperMethod + : TestContainer + { + [Fact] + public void UsesNormalizedVersionInUrls() + { + var package = new Package + { + PackageRegistration = new PackageRegistration + { + Id = "TestPackageId" + }, + NormalizedVersion = "1.0.0-alpha.1", + Version = "1.0.0-alpha.1+metadata" + }; + + string fixedUrl = UrlHelperExtensions.Package(TestUtility.MockUrlHelper(), package.Id, package.Version); + + Assert.DoesNotContain("metadata", fixedUrl); + Assert.EndsWith(package.NormalizedVersion, fixedUrl); + } + } public class ThePackageHelperMethod : TestContainer