-
Notifications
You must be signed in to change notification settings - Fork 748
Create OwnerDetailsUriTemplateResourceV3 and provider #5763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fe8167
Create Owner Details URI Template resources
donnie-msft 7469b33
Remove public ctor
donnie-msft ab7626e
Add tests for OwnerDetailsUriResourceV3 and Provider
donnie-msft f711821
Remove empty Uri test
donnie-msft 0b5db64
Use _template in test
donnie-msft 0e21635
Make resource throw if not found instead of null
donnie-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/NuGet.Core/NuGet.Protocol/Providers/OwnerDetailsUriResourceV3Provider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // 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. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using NuGet.Protocol.Core.Types; | ||
| using NuGet.Protocol.Resources; | ||
|
|
||
| namespace NuGet.Protocol.Providers | ||
| { | ||
| /// <summary>NuGet.Protocol resource provider for <see cref="OwnerDetailsUriTemplateResourceV3"/> in V3 HTTP feeds.</summary> | ||
| /// <remarks>When successful, returns an instance of <see cref="OwnerDetailsUriTemplateResourceV3"/>.</remarks> | ||
| public class OwnerDetailsUriResourceV3Provider : ResourceProvider | ||
| { | ||
| public OwnerDetailsUriResourceV3Provider() | ||
| : base(typeof(OwnerDetailsUriTemplateResourceV3), | ||
| nameof(OwnerDetailsUriTemplateResourceV3), | ||
| NuGetResourceProviderPositions.Last) | ||
| { | ||
| } | ||
|
|
||
| /// <inheritdoc cref="ResourceProvider.TryCreate(SourceRepository, CancellationToken)"/> | ||
| public override async Task<Tuple<bool, INuGetResource?>> TryCreate(SourceRepository source, CancellationToken token) | ||
| { | ||
| OwnerDetailsUriTemplateResourceV3? resource = null; | ||
| ServiceIndexResourceV3? serviceIndex = await source.GetResourceAsync<ServiceIndexResourceV3>(token); | ||
| if (serviceIndex != null) | ||
| { | ||
| Uri? uriTemplate = serviceIndex.GetServiceEntryUri(ServiceTypes.OwnerDetailsUriTemplate); | ||
|
|
||
| if (uriTemplate != null) | ||
| { | ||
| resource = OwnerDetailsUriTemplateResourceV3.CreateOrNull(uriTemplate); | ||
| } | ||
| } | ||
|
|
||
| return new Tuple<bool, INuGetResource?>(resource != null, resource); | ||
| } | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
src/NuGet.Core/NuGet.Protocol/PublicAPI/net472/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/NuGet.Core/NuGet.Protocol/PublicAPI/netcoreapp5.0/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/NuGet.Core/NuGet.Protocol/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/NuGet.Core/NuGet.Protocol/Resources/OwnerDetailsUriTemplateResourceV3.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // 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. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using NuGet.Protocol.Core.Types; | ||
|
|
||
| namespace NuGet.Protocol.Resources | ||
| { | ||
| /// <summary>Owner Details Uri Template for NuGet V3 HTTP feeds.</summary> | ||
| /// <remarks>Not intended to be created directly. Use <see cref="SourceRepository.GetResourceAsync{T}(CancellationToken)"/> | ||
| /// with <see cref="OwnerDetailsUriTemplateResourceV3"/> for T, and typecast to this class. | ||
| public class OwnerDetailsUriTemplateResourceV3 : INuGetResource | ||
| { | ||
| private readonly string _template; | ||
|
|
||
| private OwnerDetailsUriTemplateResourceV3(string template) | ||
| { | ||
| _template = template ?? throw new ArgumentNullException(nameof(template)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates the specified Owner Details Uri template provided by the server if it exists and is valid. | ||
| /// </summary> | ||
| /// <param name="uriTemplate">The Absolute Uri template provided by the server.</param> | ||
| /// <returns>A valid Owner Details Uri template, or null.</returns> | ||
| public static OwnerDetailsUriTemplateResourceV3? CreateOrNull(Uri uriTemplate) | ||
| { | ||
| if (uriTemplate is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(uriTemplate)); | ||
| } | ||
|
|
||
| if (uriTemplate.OriginalString.Length == 0 | ||
| || !uriTemplate.IsAbsoluteUri | ||
| || !uriTemplate.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return new OwnerDetailsUriTemplateResourceV3(uriTemplate.OriginalString); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a URL for viewing package Owner URL outside of Visual Studio. The URL will not be verified to exist. | ||
| /// </summary> | ||
| /// <param name="owner">The owner username.</param> | ||
| /// <returns>The first URL from the resource, with the URI template applied.</returns> | ||
| public Uri GetUri(string owner) | ||
|
donnie-msft marked this conversation as resolved.
|
||
| { | ||
| var uriString = _template | ||
| #if NETCOREAPP | ||
| .Replace("{owner}", owner, StringComparison.OrdinalIgnoreCase); | ||
|
kartheekp-ms marked this conversation as resolved.
|
||
| #else | ||
| .Replace("{owner}", owner); | ||
| #endif | ||
|
|
||
| return new Uri(uriString); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...NuGet.Core.Tests/NuGet.Protocol.Tests/Providers/OwnerDetailsUriResourceV3ProviderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // 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. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using FluentAssertions; | ||
| using NuGet.Configuration; | ||
| using NuGet.Packaging; | ||
| using NuGet.Protocol.Core.Types; | ||
| using NuGet.Protocol.Providers; | ||
| using Xunit; | ||
|
|
||
| namespace NuGet.Protocol.Tests.Providers | ||
| { | ||
| public class OwnerDetailsUriResourceV3ProviderTests | ||
| { | ||
| [Fact] | ||
| public async Task TryCreate_NoResourceInServiceIndex_ReturnsFalseAsync() | ||
| { | ||
| // Arrange | ||
| var serviceIndexProvider = MockServiceIndexResourceV3Provider.Create(); | ||
| var target = new OwnerDetailsUriResourceV3Provider(); | ||
| var providers = new INuGetResourceProvider[] { serviceIndexProvider, target }; | ||
|
|
||
| PackageSource packageSource = new PackageSource("https://nuget.test/v3/index.json"); | ||
| SourceRepository sourceRepository = new SourceRepository(packageSource, providers); | ||
|
|
||
| // Act | ||
| Tuple<bool, INuGetResource?> result = await target.TryCreate(sourceRepository, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| bool providerHandlesInputSource = result.Item1; | ||
| INuGetResource? resource = result.Item2; | ||
|
|
||
| providerHandlesInputSource.Should().BeFalse(); | ||
| resource.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TryCreate_ResourceInServiceIndex_ReturnsTrueAsync() | ||
| { | ||
| // Arrange | ||
| var ownerDetailsResourceEntry = new ServiceIndexEntry(new Uri("https://nuget.test/profiles/{owner}?_src=template"), ServiceTypes.OwnerDetailsUriTemplate[0], MinClientVersionUtility.GetNuGetClientVersion()); | ||
| var serviceIndexProvider = MockServiceIndexResourceV3Provider.Create(); | ||
| var target = new OwnerDetailsUriResourceV3Provider(); | ||
| var providers = new INuGetResourceProvider[] { serviceIndexProvider, target }; | ||
|
|
||
| PackageSource packageSource = new PackageSource("https://nuget.test/v3/index.json"); | ||
| SourceRepository sourceRepository = new SourceRepository(packageSource, providers); | ||
|
|
||
| // Act | ||
| Tuple<bool, INuGetResource?> result = await target.TryCreate(sourceRepository, CancellationToken.None); | ||
|
|
||
| // Assert | ||
| bool providerHandlesInputSource = result.Item1; | ||
| INuGetResource? resource = result.Item2; | ||
|
|
||
| providerHandlesInputSource.Should().BeFalse(); | ||
| resource.Should().BeNull(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...NuGet.Core.Tests/NuGet.Protocol.Tests/Resources/OwnerDetailsUriTemplateResourceV3Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // 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. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using FluentAssertions; | ||
| using NuGet.Protocol.Resources; | ||
| using Xunit; | ||
|
|
||
| namespace NuGet.Protocol.Tests.Resources | ||
| { | ||
| public class OwnerDetailsUriTemplateResourceV3Tests | ||
| { | ||
| private readonly Uri _template = new Uri("https://nuget.test/profiles/{owner}?_src=template"); | ||
|
donnie-msft marked this conversation as resolved.
|
||
|
|
||
| [Fact] | ||
| public void CreateOrNull_WhenNullTemplate_Throws() | ||
| { | ||
| // Arrange | ||
| Uri? template = null; | ||
|
|
||
| // Act & Assert | ||
| Assert.Throws<ArgumentNullException>(() => OwnerDetailsUriTemplateResourceV3.CreateOrNull(template!)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOrNull_WhenTemplateNotAbsoluteUri_CreatesNullResource() | ||
| { | ||
| // Arrange | ||
| var template = new Uri("/owner/profile", UriKind.Relative); | ||
|
|
||
| // Act | ||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(template); | ||
|
|
||
| // Assert | ||
| target.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOrNull_WhenTemplateNotHttps_CreatesNullResource() | ||
| { | ||
| // Arrange | ||
| var template = new Uri("http://nuget.test/profiles/{owner}?_src=template"); | ||
|
donnie-msft marked this conversation as resolved.
|
||
|
|
||
| // Act | ||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(template); | ||
|
|
||
| // Assert | ||
| target.Should().BeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CreateOrNull_WhenValidTemplateHttps_CreatesResource() | ||
| { | ||
| // Arrange & Act | ||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(_template); | ||
|
|
||
| // Assert | ||
| target.Should().NotBeNull(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetUri_WithSpacesInOwnerParameter_CreatesValidOwnerUriWithEncoding() | ||
| { | ||
| // Arrange | ||
| string owner = "Microsoft Microsoft Microsoft"; | ||
| string formattedOwner = "Microsoft%20Microsoft%20Microsoft"; | ||
|
|
||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(_template); | ||
|
|
||
| // Act | ||
| Uri ownerUri = target!.GetUri(owner); | ||
|
|
||
| // Assert | ||
| ownerUri.Should().NotBeNull(); | ||
| ownerUri.IsAbsoluteUri.Should().BeTrue(); | ||
| ownerUri.AbsoluteUri.Should().Be($"https://nuget.test/profiles/{formattedOwner}?_src=template"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("microsoft")] | ||
| [InlineData("MiCroSoFT")] | ||
| public void GetUri_WithValidOwnerParameter_CreatesValidOwnerUriWithSameCasing(string owner) | ||
| { | ||
| // Arrange | ||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(_template); | ||
|
|
||
| // Act | ||
| Uri ownerUri = target!.GetUri(owner); | ||
|
|
||
| // Assert | ||
| ownerUri.Should().NotBeNull(); | ||
| ownerUri.IsAbsoluteUri.Should().BeTrue(); | ||
| ownerUri.AbsoluteUri.Should().Be($"https://nuget.test/profiles/{owner}?_src=template"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| public void GetUri_WithInvalidOwnerParameter_ReturnsOriginalTemplate(string owner) | ||
| { | ||
| // Arrange | ||
| var target = OwnerDetailsUriTemplateResourceV3.CreateOrNull(_template); | ||
| string templateWithoutOwner = "https://nuget.test/profiles/?_src=template"; | ||
|
|
||
| // Act | ||
| Uri ownerUri = target!.GetUri(owner); | ||
|
|
||
| // Assert | ||
| ownerUri.Should().NotBeNull(); | ||
| ownerUri.IsAbsoluteUri.Should().BeTrue(); | ||
| ownerUri.AbsoluteUri.Should().Be(templateWithoutOwner); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.