public async Task DownloadExistingPackageReadme() { var pkgService = new Mock <IPackageService>(MockBehavior.Strict); pkgService.Setup(p => p.ExistsAsync(It.IsAny <PackageIdentity>())).ReturnsAsync(true); var pkgStorageService = new Mock <IPackageStorageService>(MockBehavior.Strict); pkgStorageService.Setup(p => p.GetReadmeStreamAsync(It.IsAny <PackageIdentity>())) .ReturnsAsync(new MemoryStream(Encoding.UTF8.GetBytes("readme content"))); using (TestServer server = TestServerBuilder.Create() .TraceToTestOutputHelper(Helper, LogLevel.Error) .WithMock(typeof(IPackageStorageService), pkgStorageService) .WithMock(typeof(IPackageService), pkgService) .Build()) { var services = server.Host.Services; Assert.Equal(pkgStorageService.Object, services.GetRequiredService <IPackageStorageService>()); //no docs for this. var response = await server.CreateClient().GetAsync(string.Format("v3/package/{0}/{1}/readme", "abc", "1.2.3")); Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.Equal("text/markdown", response.Content.Headers.ContentType.MediaType); var readmeContent = await response.Content.ReadAsStringAsync(); Assert.Equal("readme content", readmeContent); pkgStorageService.Verify(p => p.GetReadmeStreamAsync(It.Is <PackageIdentity>(pi => pi.Id == "abc" && pi.Version.Equals(NuGetVersion.Parse("1.2.3")))), Times.Once()); } }
public async Task DownloadExistingPackage() { var pkgService = new Mock <IPackageService>(MockBehavior.Strict); pkgService.Setup(p => p.IncrementDownloadCountAsync(It.IsAny <PackageIdentity>())).ReturnsAsync(true); var pkgStorageService = new Mock <IPackageStorageService>(MockBehavior.Strict); pkgStorageService.Setup(p => p.GetPackageStreamAsync(It.IsAny <PackageIdentity>())) .ReturnsAsync(new MemoryStream(new byte[10])); using (TestServer server = TestServerBuilder.Create() .TraceToTestOutputHelper(Helper, LogLevel.Error) .WithMock(typeof(IPackageStorageService), pkgStorageService) .WithMock(typeof(IPackageService), pkgService) .Build()) { var services = server.Host.Services; Assert.Equal(pkgStorageService.Object, services.GetRequiredService <IPackageStorageService>()); // https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-content-nupkg var response = await server.CreateClient().GetAsync(string.Format("v3/package/{0}/{1}/{0}.{1}.nupkg", "abc", "1.2.3")); Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.Equal("application/octet-stream", response.Content.Headers.ContentType.MediaType); var byteArray = await response.Content.ReadAsByteArrayAsync(); Assert.Equal(10, byteArray.Length); pkgStorageService.Verify(p => p.GetPackageStreamAsync(It.Is <PackageIdentity>(pi => pi.Id == "abc" && pi.Version.Equals(NuGetVersion.Parse("1.2.3")))), Times.Once()); } }
public async Task DownloadExistingPackageManifest() { var pkgService = new Mock <IPackageService>(MockBehavior.Strict); pkgService.Setup(p => p.ExistsAsync(It.IsAny <PackageIdentity>())).ReturnsAsync(true); var pkgStorageService = new Mock <IPackageStorageService>(MockBehavior.Strict); pkgStorageService.Setup(p => p.GetNuspecStreamAsync(It.IsAny <PackageIdentity>())) .ReturnsAsync(new MemoryStream(Encoding.UTF8.GetBytes(exampleNuSpec))); using (TestServer server = TestServerBuilder.Create() .TraceToTestOutputHelper(Helper, LogLevel.Error) .WithMock(typeof(IPackageStorageService), pkgStorageService) .WithMock(typeof(IPackageService), pkgService) .Build()) { var services = server.Host.Services; Assert.Equal(pkgStorageService.Object, services.GetRequiredService <IPackageStorageService>()); // https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-manifest-nuspec var response = await server.CreateClient().GetAsync(string.Format("v3/package/{0}/{1}/{0}.nuspec", "abc", "1.2.3")); Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.Equal("text/xml", response.Content.Headers.ContentType.MediaType); var nuspecContent = await response.Content.ReadAsStringAsync(); Assert.Equal(exampleNuSpec, nuspecContent); pkgStorageService.Verify(p => p.GetNuspecStreamAsync(It.Is <PackageIdentity>(pi => pi.Id == "abc" && pi.Version.Equals(NuGetVersion.Parse("1.2.3")))), Times.Once()); } }
public async Task AskEmptyServer() { using (TestServer server = TestServerBuilder.Create().Build()) { var response = await server.CreateClient().GetAsync(EntryUrl); Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); } }
public async Task AskEmptyServerForNotExistingPackageID(string packageID) { using (TestServer server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build()) { //Ask Empty Storage for a not existings ID var response = await server.CreateClient().GetAsync(string.Format(IndexUrlFormatString, packageID)); Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); } }
public PackagesV2ModuleTest(ITestOutputHelper helper) { _helper = helper ?? throw new ArgumentNullException(nameof(helper)); packageRepo = new Mock <IPackageService>(MockBehavior.Strict); storageRepo = new Mock <IPackageStorageService>(MockBehavior.Strict); serverBuilder = TestServerBuilder.Create() .TraceToTestOutputHelper(_helper, LogLevel.Error) .WithMock(typeof(IPackageService), packageRepo) .WithMock(typeof(IPackageStorageService), storageRepo); }
public NuGetClientIntegrationTest(ITestOutputHelper helper) { Helper = helper ?? throw new ArgumentNullException(nameof(helper)); server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build(); providers = new List <Lazy <INuGetResourceProvider> >(); providers.AddRange(Repository.Provider.GetCoreV3()); providers.Add(new Lazy <INuGetResourceProvider>(() => new PackageMetadataResourceV3Provider())); _httpClient = server.CreateClient(); providers.Insert(0, new Lazy <INuGetResourceProvider>(() => new HttpSourceResourceProviderTestHost(_httpClient))); tempDir = new TempDir(); }
public async Task DownloadNonExistingPackage() { var pkgService = new Mock <IPackageService>(MockBehavior.Strict); pkgService.Setup(p => p.IncrementDownloadCountAsync(It.IsAny <PackageIdentity>())).ReturnsAsync(false); using (TestServer server = TestServerBuilder.Create() .TraceToTestOutputHelper(Helper, LogLevel.Error) .WithMock(typeof(IPackageService), pkgService) .Build()) { var response = await server.CreateClient().GetAsync(string.Format("v3/package/{0}/{1}/{0}.{1}.nupkg", "abc", "1.2.3")); Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode); } }
public NuGetClientIntegrationTest(ITestOutputHelper helper) { Helper = helper ?? throw new ArgumentNullException(nameof(helper)); server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build(); var providers = new List <Lazy <INuGetResourceProvider> >(); providers.AddRange(Repository.Provider.GetCoreV3()); providers.Add(new Lazy <INuGetResourceProvider>(() => new PackageMetadataResourceV3Provider())); _httpClient = server.CreateClient(); providers.Insert(0, new Lazy <INuGetResourceProvider>(() => new HttpSourceResourceProviderTestHost(_httpClient))); indexUrl = new Uri(server.BaseAddress, IndexUrlString).AbsoluteUri; var packageSource = new PackageSource(indexUrl); _sourceRepository = new SourceRepository(packageSource, providers); _cacheContext = new SourceCacheContext() { NoCache = true, MaxAge = new DateTimeOffset(), DirectDownload = true }; _httpSource = _sourceRepository.GetResource <HttpSourceResource>(); Assert.IsType <HttpSourceTestHost>(_httpSource.HttpSource); }
public async Task AskServerForVersionsOfExistingPackageID() { var pkgService = new Mock <IPackageService>(MockBehavior.Strict); pkgService.Setup(p => p.FindAsync("abc", false, false)).ReturnsAsync(new Package[] { new Package() { Id = "abc", VersionString = "1.2.3" }, new Package() { Id = "abc", VersionString = "1.4.7" } }); using (TestServer server = TestServerBuilder.Create() .TraceToTestOutputHelper(Helper, LogLevel.Error) .WithMock(typeof(IPackageService), pkgService) .Build()) { var services = server.Host.Services; Assert.Equal(pkgService.Object, services.GetRequiredService <IPackageService>()); // https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#enumerate-package-versions var response = await server.CreateClient().GetAsync(string.Format(IndexUrlFormatString, "abc")); Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); var jsonString = await response.Content.ReadAsStringAsync(); var actual = JObject.Parse(jsonString); var expected = new JObject { { "versions", new JArray( "1.2.3", "1.4.7") } }; Assert.True(JToken.DeepEquals(expected, actual)); } }