diff --git a/src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs b/src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs index 80971cf9088..a8adc9e0cf3 100644 --- a/src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs +++ b/src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs @@ -18,13 +18,13 @@ public static class ServiceTypes public static readonly string Version510 = "/5.1.0"; internal const string Version670 = "/6.7.0"; internal const string Version6110 = "/6.11.0"; - internal const string Version6120 = "/6.12.0"; + internal const string Version6130 = "/6.13.0"; public static readonly string[] SearchQueryService = { "SearchQueryService" + Versioned, "SearchQueryService" + Version340, "SearchQueryService" + Version300beta }; public static readonly string[] RegistrationsBaseUrl = { $"RegistrationsBaseUrl{Versioned}", $"RegistrationsBaseUrl{Version360}", $"RegistrationsBaseUrl{Version340}", $"RegistrationsBaseUrl{Version300rc}", $"RegistrationsBaseUrl{Version300beta}", "RegistrationsBaseUrl" }; public static readonly string[] SearchAutocompleteService = { "SearchAutocompleteService" + Versioned, "SearchAutocompleteService" + Version300beta }; public static readonly string[] ReportAbuse = { "ReportAbuseUriTemplate" + Versioned, "ReportAbuseUriTemplate" + Version300 }; - internal static readonly string[] ReadmeFileUrl = { "ReadmeUriTemplate" + Versioned, "ReadmeUriTemplate" + Version6120 }; + internal static readonly string[] ReadmeFileUrl = { "ReadmeUriTemplate" + Versioned, "ReadmeUriTemplate" + Version6130 }; public static readonly string[] PackageDetailsUriTemplate = { "PackageDetailsUriTemplate" + Version510 }; public static readonly string[] LegacyGallery = { "LegacyGallery" + Versioned, "LegacyGallery" + Version200 }; public static readonly string[] PackagePublish = { "PackagePublish" + Versioned, "PackagePublish" + Version200 }; diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Providers/ReadmeUriTemplateResourceProviderTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Providers/ReadmeUriTemplateResourceProviderTests.cs new file mode 100644 index 00000000000..cc2953dbaf4 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Providers/ReadmeUriTemplateResourceProviderTests.cs @@ -0,0 +1,114 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Moq; +using Newtonsoft.Json.Linq; +using NuGet.Configuration; +using NuGet.Protocol.Core.Types; +using NuGet.Versioning; +using Xunit; + +namespace NuGet.Protocol.Tests.Providers +{ + public class ReadmeUriTemplateResourceProviderTests + { + private const string ResourceType = "ReadmeUriTemplate/6.13.0"; + + private readonly PackageSource _packageSource; + private readonly ReadmeUriTemplateResourceProvider _target; + + public ReadmeUriTemplateResourceProviderTests() + { + _packageSource = new PackageSource("https://unit.test"); + _target = new ReadmeUriTemplateResourceProvider(); + } + + [Fact] + public async Task TryCreate_WhenResourceDoesNotExist_ReturnsNull() + { + var resourceProviders = new ResourceProvider[] + { + CreateServiceIndexResourceV3Provider(), + _target + }; + var sourceRepository = new SourceRepository(_packageSource, resourceProviders); + + Tuple result = await _target.TryCreate(sourceRepository, CancellationToken.None); + + Assert.False(result.Item1); + Assert.Null(result.Item2); + } + + [Fact] + public async Task TryCreate_WhenResourceExists_ReturnsValidResourceAsync() + { + var serviceEntry = new RawServiceIndexEntry("https://unit.test/packages/{lower_id}/{lower_version}/readme", ResourceType); + var resourceProviders = new ResourceProvider[] + { + CreateServiceIndexResourceV3Provider(serviceEntry), + _target + }; + var sourceRepository = new SourceRepository(_packageSource, resourceProviders); + + Tuple result = await _target.TryCreate(sourceRepository, CancellationToken.None); + + Assert.True(result.Item1); + Assert.IsType(result.Item2); + Assert.Equal( + "https://unit.test/packages/mypackage/1.0.0/readme", + ((ReadmeUriTemplateResource)result.Item2).GetReadmeUrl("MyPackage", NuGetVersion.Parse("1.0.0"))); + } + + private static ServiceIndexResourceV3Provider CreateServiceIndexResourceV3Provider(params RawServiceIndexEntry[] entries) + { + var provider = new Mock(); + + provider.Setup(x => x.Name) + .Returns(nameof(ServiceIndexResourceV3Provider)); + provider.Setup(x => x.ResourceType) + .Returns(typeof(ServiceIndexResourceV3)); + + var resources = new JArray(); + + foreach (var entry in entries) + { + resources.Add( + new JObject( + new JProperty("@id", entry.Uri), + new JProperty("@type", entry.Type))); + } + + var index = new JObject(); + + index.Add("version", "3.0.0"); + index.Add("resources", resources); + index.Add("@context", + new JObject( + new JProperty("@vocab", "http://schema.nuget.org/schema#"), + new JProperty("comment", "http://www.w3.org/2000/01/rdf-schema#comment"))); + + var serviceIndexResource = new ServiceIndexResourceV3(index, DateTime.UtcNow); + var tryCreateResult = new Tuple(true, serviceIndexResource); + + provider.Setup(x => x.TryCreate(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(tryCreateResult)); + + return provider.Object; + } + + private class RawServiceIndexEntry + { + public RawServiceIndexEntry(string uri, string type) + { + Uri = uri; + Type = type ?? throw new ArgumentNullException(nameof(type)); + } + + public string Uri { get; } + public string Type { get; } + } + } +} diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/ReadmeUriTemplateResourceTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/ReadmeUriTemplateResourceTests.cs new file mode 100644 index 00000000000..4fc29638c39 --- /dev/null +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/ReadmeUriTemplateResourceTests.cs @@ -0,0 +1,36 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using NuGet.Versioning; +using Xunit; + +namespace NuGet.Protocol.Tests +{ + public class ReadmeUriTemplateResourceTests + { + [Theory] + [InlineData("")] + [InlineData(null)] + public void ReadmeUriTemplateResource_GetReadmeUrl_BlankTemplate_ReturnsEmptyString(string uriTemplate) + { + string expectedResult = string.Empty; + var resource = new ReadmeUriTemplateResource(uriTemplate); + + var actual = resource.GetReadmeUrl("TestPackage", NuGetVersion.Parse("1.0.0")); + + Assert.Equal(expectedResult, actual); + } + + [Fact] + public void ReadmeUriTemplateResource_GetReadmeUrl_ReturnsFormedUrl() + { + const string uriTemplate = "https://test.nuget.org/{lower_id}/{lower_version}/readme"; + const string expectedResult = "https://test.nuget.org/testpackage/1.0.0/readme"; + var resource = new ReadmeUriTemplateResource(uriTemplate); + + var actual = resource.GetReadmeUrl("TestPackage", NuGetVersion.Parse("1.0.0")); + + Assert.Equal(expectedResult, actual.ToString()); + } + } +}