diff --git a/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs b/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs index 5f280b37..920f9ad5 100644 --- a/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/ReleasesClientTests.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; +using System.IO.Compression; using System.Linq; +using System.Text; using System.Threading.Tasks; using Octokit; using Octokit.Tests.Integration; @@ -188,8 +191,45 @@ public class ReleasesClientTests var response = await _github.Connection.Get(new Uri(asset.Url), new Dictionary(), "application/octet-stream"); - Assert.Equal("This is a plain text file.", response.Body); + Assert.Contains("This is a plain text file.", Encoding.ASCII.GetString((byte[]) response.Body)); } + [IntegrationTest] + public async Task CanDownloadBinaryAsset() + { + var releaseWithNoUpdate = new NewRelease("0.1") { Draft = true }; + var release = await _releaseClient.Create(_context.RepositoryOwner, _context.RepositoryName, releaseWithNoUpdate); + + var stream = Helper.LoadFixture("hello-world.zip"); + + var newAsset = new ReleaseAssetUpload("hello-world.zip" + , "application/octet-stream" + , stream + , null); + + var result = await _releaseClient.UploadAsset(release, newAsset); + + Assert.True(result.Id > 0); + + var asset = await _releaseClient.GetAsset(_context.RepositoryOwner, _context.RepositoryName, result.Id); + + Assert.Equal(result.Id, asset.Id); + + var response = await _github.Connection.Get(new Uri(asset.Url), new Dictionary(), "application/octet-stream"); + + var textContent = String.Empty; + + using (var zipstream = new MemoryStream((byte[]) response.Body)) + using(var archive = new ZipArchive(zipstream)) + { + var enttry = archive.Entries[0]; + var data = new byte[enttry.Length]; + await enttry.Open().ReadAsync(data, 0, data.Length); + textContent = Encoding.ASCII.GetString(data); + } + + Assert.Contains("This is a plain text file.",textContent ); + } + public void Dispose() { diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index 7398edbe..f4697936 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -38,6 +38,8 @@ + + False @@ -144,6 +146,7 @@ + Designer diff --git a/Octokit.Tests.Integration/fixtures/hello-world.zip b/Octokit.Tests.Integration/fixtures/hello-world.zip new file mode 100644 index 00000000..54ec245f Binary files /dev/null and b/Octokit.Tests.Integration/fixtures/hello-world.zip differ diff --git a/Octokit/Http/HttpClientAdapter.cs b/Octokit/Http/HttpClientAdapter.cs index 59dd87c0..248bef7c 100644 --- a/Octokit/Http/HttpClientAdapter.cs +++ b/Octokit/Http/HttpClientAdapter.cs @@ -70,16 +70,22 @@ namespace Octokit.Internal object responseBody = null; string contentType = null; + + // We added support for downloading images,zip-files and application/octet-stream. + // Let's constrain this appropriately. + var binaryContentTypes = new[] {"application/zip" + ,"application/x-gzip", + "application/octet-stream"}; + using (var content = responseMessage.Content) { if (content != null) { contentType = GetContentMediaType(responseMessage.Content); - // We added support for downloading images and zip-files. Let's constrain this appropriately. - if (contentType != null && (contentType.StartsWith("image/") - || contentType.Equals("application/zip", StringComparison.OrdinalIgnoreCase) - || contentType.Equals("application/x-gzip", StringComparison.OrdinalIgnoreCase))) + if (contentType != null && (contentType.StartsWith("image/") || + binaryContentTypes + .Any(item => item.Equals(contentType,StringComparison.OrdinalIgnoreCase)))) { responseBody = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false); }