Exemplo n.º 1
0
        public async Task CanGatherAndRecreateNuGetPackageCentralDirectory()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                // Discover the .nupkg URL.
                var sourceRepository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
                var serviceIndex     = await sourceRepository.GetResourceAsync <ServiceIndexResourceV3>();

                var packageBaseAddress = serviceIndex.GetServiceEntryUri(ServiceTypes.PackageBaseAddress);

                var id         = "Newtonsoft.Json".ToLowerInvariant();
                var version    = NuGetVersion.Parse("9.0.1").ToNormalizedString().ToLowerInvariant();
                var packageUri = new Uri(packageBaseAddress, $"{id}/{version}/{id}.{version}.nupkg");

                ZipDirectory zipDirectoryA;
                string       mzipPath;
                using (var httpClient = new HttpClient())
                {
                    var httpZipProvider = new HttpZipProvider(httpClient);
                    using (var reader = await httpZipProvider.GetReaderAsync(packageUri))
                    {
                        // Read the ZIP directory from the .nupkg URL.
                        zipDirectoryA = await reader.ReadAsync();

                        // Save the .mzip to the test directory.
                        mzipPath = Path.Combine(testDirectory, $"{id}.{version}.mzip");
                        using (var fileStream = new FileStream(mzipPath, FileMode.Create))
                        {
                            var mzipFormat = new MZipFormat();
                            await mzipFormat.WriteAsync(reader.Stream, fileStream);
                        }
                    }
                }

                // Read the .mzip back from disk.
                ZipDirectory zipDirectoryB;
                using (var fileStream = new FileStream(mzipPath, FileMode.Open))
                {
                    var mzipFormat = new MZipFormat();
                    using (var mzipStream = await mzipFormat.ReadAsync(fileStream))
                        using (var reader = new ZipDirectoryReader(mzipStream))
                        {
                            zipDirectoryB = await reader.ReadAsync();
                        }
                }

                // Compare the results.
                TestUtility.VerifyJsonEquals(zipDirectoryA, zipDirectoryB);
            }
        }
Exemplo n.º 2
0
        public async Task CanGatherAndRecreateNuGetPackageCentralDirectory(string id, string version)
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var packageUri = await NuGetUtility.GetNupkgUrlAsync(id, version);

                ZipDirectory zipDirectoryA;
                string       mzipPath;
                using (var httpClient = new HttpClient())
                {
                    var httpZipProvider = new HttpZipProvider(httpClient)
                    {
                        RequireAcceptRanges = false,
                    };
                    using (var reader = await httpZipProvider.GetReaderAsync(packageUri))
                    {
                        // Read the ZIP directory from the .nupkg URL.
                        zipDirectoryA = await reader.ReadAsync();

                        // Save the .mzip to the test directory.
                        mzipPath = Path.Combine(testDirectory, $"{id}.{version}.mzip");
                        using (var fileStream = new FileStream(mzipPath, FileMode.Create))
                        {
                            var mzipFormat = new MZipFormat();
                            await mzipFormat.WriteAsync(reader.Stream, fileStream);
                        }
                    }
                }

                // Read the .mzip back from disk.
                ZipDirectory zipDirectoryB;
                using (var fileStream = new FileStream(mzipPath, FileMode.Open))
                {
                    var mzipFormat = new MZipFormat();
                    using (var mzipStream = await mzipFormat.ReadAsync(fileStream))
                        using (var reader = new ZipDirectoryReader(mzipStream))
                        {
                            zipDirectoryB = await reader.ReadAsync();
                        }
                }

                // Compare the results.
                TestUtility.VerifyJsonEquals(zipDirectoryA, zipDirectoryB);
            }
        }
Exemplo n.º 3
0
        public async Task CanRoundTripZipFile(string path)
        {
            // Arrange
            var srcStream = TestUtility.BufferTestData(path);
            var expected  = (await TestUtility.ReadWithMiniZipAsync(srcStream)).Data;
            var target    = new MZipFormat();
            var dstStream = new MemoryStream();

            // Act
            await target.WriteAsync(srcStream, dstStream);

            dstStream.Position = 0;
            var mzipStream = await target.ReadAsync(dstStream);

            var actual = (await TestUtility.ReadWithMiniZipAsync(mzipStream)).Data;

            // Assert
            TestUtility.VerifyJsonEquals(expected, actual);
        }