Skip to content

Commit

Permalink
fix: "zip" Content-Type resulting in null Stream for Artifacts on Blo…
Browse files Browse the repository at this point in the history
…b Storage (#2905)
  • Loading branch information
thomhurst authored Apr 15, 2024
1 parent 889bf25 commit 4ca8f1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Octokit.Tests/Http/HttpClientAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ public async Task SetsContentType()

Assert.Equal("application/json", response.ContentType);
}

// See #2898 for why this is necessary
// Non standard MIME Content-Type coming from Blob Storage when downloading artifacts
[Fact]
public async Task SetsZipContentType()
{
var memoryStream = new MemoryStream();
var streamContent = new StreamContent(memoryStream);
streamContent.Headers.TryAddWithoutValidation("Content-Type", "zip");

var responseMessage = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = streamContent
};
var tester = new HttpClientAdapterTester();

var response = await tester.BuildResponseTester(responseMessage);

Assert.Equal("application/zip", response.ContentType);
}
}

sealed class HttpClientAdapterTester : HttpClientAdapter
Expand Down
11 changes: 9 additions & 2 deletions Octokit/Http/HttpClientAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ protected virtual async Task<IResponse> BuildResponse(HttpResponseMessage respon
AcceptHeaders.RawContentMediaType,
"application/zip" ,
"application/x-gzip" ,
"zip" , // Not a standard MIME type but see issue #2898
"application/octet-stream"};

var content = responseMessage.Content;
Expand Down Expand Up @@ -169,10 +168,18 @@ protected virtual HttpRequestMessage BuildRequestMessage(IRequest request)

static string GetContentMediaType(HttpContent httpContent)
{
if (httpContent.Headers != null && httpContent.Headers.ContentType != null)
if (httpContent.Headers?.ContentType != null)
{
return httpContent.Headers.ContentType.MediaType;
}

// Issue #2898 - Bad "zip" Content-Type coming from Blob Storage for artifacts
if (httpContent.Headers?.TryGetValues("Content-Type", out var contentTypeValues) == true
&& contentTypeValues.FirstOrDefault() == "zip")
{
return "application/zip";
}

return null;
}

Expand Down

0 comments on commit 4ca8f1c

Please sign in to comment.