fix: "zip" Content-Type resulting in null Stream for Artifacts on Blob Storage (#2905)

This commit is contained in:
Tom Longhurst
2024-04-15 15:28:10 +01:00
committed by GitHub
parent 889bf25979
commit 4ca8f1cd2c
2 changed files with 30 additions and 2 deletions

View File

@@ -177,6 +177,27 @@ namespace Octokit.Tests.Http
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

View File

@@ -81,7 +81,6 @@ namespace Octokit.Internal
AcceptHeaders.RawContentMediaType,
"application/zip" ,
"application/x-gzip" ,
"zip" , // Not a standard MIME type but see issue #2898
"application/octet-stream"};
var content = responseMessage.Content;
@@ -169,10 +168,18 @@ namespace Octokit.Internal
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;
}