Skip to content

Commit 4ca8f1c

Browse files
authored
fix: "zip" Content-Type resulting in null Stream for Artifacts on Blob Storage (#2905)
1 parent 889bf25 commit 4ca8f1c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Octokit.Tests/Http/HttpClientAdapterTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ public async Task SetsContentType()
177177

178178
Assert.Equal("application/json", response.ContentType);
179179
}
180+
181+
// See #2898 for why this is necessary
182+
// Non standard MIME Content-Type coming from Blob Storage when downloading artifacts
183+
[Fact]
184+
public async Task SetsZipContentType()
185+
{
186+
var memoryStream = new MemoryStream();
187+
var streamContent = new StreamContent(memoryStream);
188+
streamContent.Headers.TryAddWithoutValidation("Content-Type", "zip");
189+
190+
var responseMessage = new HttpResponseMessage
191+
{
192+
StatusCode = HttpStatusCode.OK,
193+
Content = streamContent
194+
};
195+
var tester = new HttpClientAdapterTester();
196+
197+
var response = await tester.BuildResponseTester(responseMessage);
198+
199+
Assert.Equal("application/zip", response.ContentType);
200+
}
180201
}
181202

182203
sealed class HttpClientAdapterTester : HttpClientAdapter

Octokit/Http/HttpClientAdapter.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ protected virtual async Task<IResponse> BuildResponse(HttpResponseMessage respon
8181
AcceptHeaders.RawContentMediaType,
8282
"application/zip" ,
8383
"application/x-gzip" ,
84-
"zip" , // Not a standard MIME type but see issue #2898
8584
"application/octet-stream"};
8685

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

170169
static string GetContentMediaType(HttpContent httpContent)
171170
{
172-
if (httpContent.Headers != null && httpContent.Headers.ContentType != null)
171+
if (httpContent.Headers?.ContentType != null)
173172
{
174173
return httpContent.Headers.ContentType.MediaType;
175174
}
175+
176+
// Issue #2898 - Bad "zip" Content-Type coming from Blob Storage for artifacts
177+
if (httpContent.Headers?.TryGetValues("Content-Type", out var contentTypeValues) == true
178+
&& contentTypeValues.FirstOrDefault() == "zip")
179+
{
180+
return "application/zip";
181+
}
182+
176183
return null;
177184
}
178185

0 commit comments

Comments
 (0)