public async Task Query_For_Resource_With_NoCompatibleVersion() { // Arrange var source = "https://some-site.org/test.json"; var content = CreateVersionedTestIndex(); var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); var resource = result.Item2 as ServiceIndexResourceV3; Assert.NotNull(resource); var endpoints = resource.GetServiceEntryUris(NuGetVersion.Parse("0.0.0-beta"), "A"); Assert.True(endpoints.Count == 0); }
public async Task Query_For_Resource_ReturnAllOfSameTypeVersion() { // Arrange var source = "https://some-site.org/test.json"; var content = CreateVersionedTestIndex3(); var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); var resource = result.Item2 as ServiceIndexResourceV3; Assert.NotNull(resource); var endpoints = resource.GetServiceEntryUris(new NuGetVersion(5, 0, 0), "A", "B"); Assert.True(endpoints.Count == 2); var endpointSet = new HashSet <string>(endpoints.Select(u => u.AbsoluteUri)); Assert.True(endpointSet.Contains("http://tempuri.org/A/5.0.0/1")); Assert.True(endpointSet.Contains("http://tempuri.org/A/5.0.0/2")); }
public async Task TryCreate_WhenOnlyOneResourceIsPresent_ReturnsThatResource(string resourceUrl, string resourceType) { var serviceEntry = new ServiceIndexEntry(new Uri(resourceUrl), resourceType, _defaultVersion); var resourceProviders = new ResourceProvider[] { CreateServiceIndexResourceV3Provider(serviceEntry), StaticHttpSource.CreateHttpSource( new Dictionary <string, string>() { { serviceEntry.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(resourceUrl) } }), _repositorySignatureResourceProvider }; var sourceRepository = new SourceRepository(_packageSource, resourceProviders); var result = await _repositorySignatureResourceProvider.TryCreate(sourceRepository, CancellationToken.None); Assert.True(result.Item1); var resource = result.Item2 as RepositorySignatureResource; Assert.NotNull(resource); Assert.Equal(_packageSource.Source, resource.Source); Assert.Single(resource.RepositoryCertificateInfos); }
public async Task Query_For_Particular_Multi_Value_Resource() { // Arrange var source = "https://some-site.org/test.json"; var content = CreateTestIndex(); var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); var resource = result.Item2 as ServiceIndexResourceV3; Assert.NotNull(resource); var endpoints = resource.GetServiceEntryUris(new NuGetVersion(4, 0, 0), "Fruit"); Assert.True(endpoints.Count == 3); var endpointSet = new HashSet <string>(endpoints.Select(u => u.AbsoluteUri)); Assert.True(endpointSet.Contains("http://tempuri.org/banana")); Assert.True(endpointSet.Contains("http://tempuri.org/apple")); Assert.True(endpointSet.Contains("http://tempuri.org/orange")); }
public async Task TryCreate_WhenResourceIsPresent_CreatesVersionedHttpCacheEntry(string resourceUrl, string resourceType, string expectedCacheKey) { var serviceEntry = new ServiceIndexEntry(new Uri(resourceUrl), resourceType, _defaultVersion); var responses = new Dictionary <string, string>() { { serviceEntry.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(resourceUrl) } }; var httpSource = new TestHttpSource(_packageSource, responses); var resourceProviders = new ResourceProvider[] { CreateServiceIndexResourceV3Provider(serviceEntry), StaticHttpSource.CreateHttpSource(responses, httpSource: httpSource), _repositorySignatureResourceProvider }; var sourceRepository = new SourceRepository(_packageSource, resourceProviders); string actualCacheKey = null; httpSource.HttpSourceCachedRequestInspector = request => { actualCacheKey = request.CacheKey; }; var result = await _repositorySignatureResourceProvider.TryCreate(sourceRepository, CancellationToken.None); Assert.True(result.Item1); Assert.Equal(expectedCacheKey, actualCacheKey); }
public async Task TryCreate_WhenMultipleResourcesArePresent_Returns500Resource() { var serviceEntry470 = new ServiceIndexEntry(new Uri(_resourceUri470), _resourceType470, _defaultVersion); var serviceEntry490 = new ServiceIndexEntry(new Uri(_resourceUri490), _resourceType490, _defaultVersion); var serviceEntry500 = new ServiceIndexEntry(new Uri(_resourceUri500), _resourceType500, _defaultVersion); var resourceProviders = new ResourceProvider[] { CreateServiceIndexResourceV3Provider(serviceEntry470, serviceEntry490, serviceEntry500), StaticHttpSource.CreateHttpSource( new Dictionary <string, string>() { { serviceEntry470.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(serviceEntry470.Uri.AbsoluteUri) }, { serviceEntry490.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(serviceEntry490.Uri.AbsoluteUri) }, { serviceEntry500.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(serviceEntry500.Uri.AbsoluteUri) } }), _repositorySignatureResourceProvider }; var sourceRepository = new SourceRepository(_packageSource, resourceProviders); var result = await _repositorySignatureResourceProvider.TryCreate(sourceRepository, CancellationToken.None); Assert.True(result.Item1); var resource = result.Item2 as RepositorySignatureResource; Assert.NotNull(resource); Assert.Equal(_packageSource.Source, resource.Source); Assert.Single(resource.RepositoryCertificateInfos); Assert.StartsWith(serviceEntry500.Uri.AbsoluteUri, resource.RepositoryCertificateInfos.Single().ContentUrl); }
public async Task Query_For_Resource_With_Precedence() { // Arrange var source = "https://some-site.org/test.json"; var content = CreateTestIndex(); var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); var resource = result.Item2 as ServiceIndexResourceV3; Assert.NotNull(resource); var endpoints = resource[new string[] { "Chocolate", "Vegetable" }]; Assert.True(endpoints.Count == 1); var endpointSet = new HashSet <string>(endpoints.Select(u => u.AbsoluteUri)); Assert.True(endpointSet.Contains("http://tempuri.org/chocolate")); }
public async Task TryCreate_CancellationThrows() { // Arrange var source = $"https://some-site-{new Guid().ToString()}.org/test.json"; var content = @"{ version: '3.1.0-beta' }"; //Create an HTTP provider that will cancel the token. var httpProvider = StaticHttpSource.CreateHttpSource( new Dictionary <string, string> { { source, content } }, errorContent: string.Empty, httpSource: null, throwOperationCancelledException: true); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { Task task() => provider.TryCreate(sourceRepository, tokenSource.Token); // Assert await Assert.ThrowsAsync <OperationCanceledException>(task); } }
public async Task Query_For_Resource_With_VersionPrecedence_NoFallbackBetweenTypes() { // Arrange var source = $"https://some-site-{new Guid().ToString()}.org/test.json"; var content = CreateVersionedTestIndex2(); var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); var resource = result.Item2 as ServiceIndexResourceV3; Assert.NotNull(resource); var endpoints = resource.GetServiceEntryUris(new NuGetVersion(4, 1, 0), "A", "B"); Assert.True(endpoints.Count == 1); var endpointSet = new HashSet <string>(endpoints.Select(u => u.AbsoluteUri)); Assert.True(endpointSet.Contains("http://tempuri.org/B")); }
public async Task TryCreate_ReturnsTrue_IfSourceLocationReturnsValidJson() { // Arrange var source = "https://some-site.org/test.json"; var content = @"{ version: '3.1.0-beta' }"; var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); // Assert Assert.True(result.Item1); }
public async Task TryCreate_Throws_IfSourceLocationDoesNotReturnValidJson(string content) { // Arrange var source = "https://fake.server/users.json"; var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act and assert var exception = await Assert.ThrowsAsync <FatalProtocolException>(async() => { var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); }); Assert.IsType <InvalidDataException>(exception.InnerException); Assert.IsType <JsonReaderException>(exception.InnerException.InnerException); }
public async Task TryCreate_Throws_IfSourceLocationReturnsFailureCode() { // Arrange var source = "https://does-not-exist.server/does-not-exist.json"; // This will return a 404 - NotFound. var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, string.Empty } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var exception = await Assert.ThrowsAsync <FatalProtocolException>(async() => { var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); }); Assert.IsType <HttpRequestException>(exception.InnerException); }
[InlineData("{ version: { value: 3 } } ")] // version is not a string public async Task TryCreate_Throws_IfNoVersionInJson(string content) { // Arrange var source = "https://fake.server/users.json"; var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var exception = await Assert.ThrowsAsync <FatalProtocolException>(async() => { var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); }); // Assert Assert.Equal("The source does not have the 'version' property.", exception.InnerException.Message); }
public async Task TryCreate_WhenUrlIsInvalid_Throws(string resourceUrl, string resourceType) { var serviceEntry = new ServiceIndexEntry(new Uri(resourceUrl), resourceType, _defaultVersion); var resourceProviders = new ResourceProvider[] { CreateServiceIndexResourceV3Provider(serviceEntry), StaticHttpSource.CreateHttpSource( new Dictionary <string, string>() { { serviceEntry.Uri.AbsoluteUri, GetRepositorySignaturesResourceJson(resourceUrl) } }), _repositorySignatureResourceProvider }; var sourceRepository = new SourceRepository(_packageSource, resourceProviders); var exception = await Assert.ThrowsAsync <FatalProtocolException>( () => _repositorySignatureResourceProvider.TryCreate(sourceRepository, CancellationToken.None)); Assert.Equal($"Repository Signatures resouce must be served over HTTPS. Source: {_packageSource.Source}", exception.Message); }
[InlineData("{ version: \"3.0.0.0\" } ")] // not strict semver public async Task TryCreate_Throws_IfInvalidVersionInJson(string content) { // Arrange var source = $"https://fake.server-{new Guid().ToString()}/users.json"; var httpProvider = StaticHttpSource.CreateHttpSource(new Dictionary <string, string> { { source, content } }); var provider = new ServiceIndexResourceV3Provider(); var sourceRepository = new SourceRepository(new PackageSource(source), new INuGetResourceProvider[] { httpProvider, provider }); // Act var exception = await Assert.ThrowsAsync <FatalProtocolException>(async() => { var result = await provider.TryCreate(sourceRepository, default(CancellationToken)); }); // Assert Assert.StartsWith("The source version is not supported", exception.InnerException.Message); }