Skip to content

Commit

Permalink
Fixes for Downloading ReleaseAsset zip File octokit#854
Browse files Browse the repository at this point in the history
This commit  addressed the `BuildResponse`  wasn't handling
response `content-type` `application/octet-stream` for binary items.
  • Loading branch information
Naveen committed Oct 16, 2015
1 parent 068511b commit 1886629
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
42 changes: 41 additions & 1 deletion Octokit.Tests.Integration/Clients/ReleasesClientTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -188,8 +191,45 @@ public async Task CanDownloadAnAsset()

var response = await _github.Connection.Get<object>(new Uri(asset.Url), new Dictionary<string, string>(), "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<object>(new Uri(asset.Url), new Dictionary<string, string>(), "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()
{
Expand Down
3 changes: 3 additions & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -144,6 +146,7 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<EmbeddedResource Include="fixtures\hello-world.zip" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
Binary file added Octokit.Tests.Integration/fixtures/hello-world.zip
Binary file not shown.
14 changes: 10 additions & 4 deletions Octokit/Http/HttpClientAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,22 @@ protected async virtual Task<IResponse> BuildResponse(HttpResponseMessage respon

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);
}
Expand Down

0 comments on commit 1886629

Please sign in to comment.