From 1305b603b3c1c6c40816a9aac46c5122764d2f0c Mon Sep 17 00:00:00 2001 From: Haacked Date: Wed, 16 Apr 2014 11:45:02 -0700 Subject: [PATCH] Add support for requesting raw bytes. People have their reasons. We're going to use the same `IHttpClient` to request emoji images. --- .../HttpClientAdapterTests.cs | 35 +++++++++++++++++++ .../Octokit.Tests.Integration.csproj | 1 + Octokit.Tests/Http/HttpClientAdapterTests.cs | 17 +++++++++ Octokit/Http/HttpClientAdapter.cs | 12 ++++++- Octokit/Http/IResponse.cs | 1 - 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 Octokit.Tests.Integration/HttpClientAdapterTests.cs diff --git a/Octokit.Tests.Integration/HttpClientAdapterTests.cs b/Octokit.Tests.Integration/HttpClientAdapterTests.cs new file mode 100644 index 00000000..b7bdce39 --- /dev/null +++ b/Octokit.Tests.Integration/HttpClientAdapterTests.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Octokit.Internal; +using Octokit.Tests.Integration; +using Xunit; + +public class HttpClientAdapterTests +{ + public class TheSendAsyncMethod + { + [IntegrationTest] + public async Task CanDownloadImage() + { + var httpClient = new HttpClientAdapter(); + var request = new Request + { + BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute), + Endpoint = new Uri("/images/icons/emoji/poop.png?v=5", UriKind.RelativeOrAbsolute), + AllowAutoRedirect = true, + Method = HttpMethod.Get + }; + + var imageBytes = await httpClient.Send(request, CancellationToken.None); + + // Spot check some of dem bytes. + Assert.Equal(137, imageBytes.BodyAsObject[0]); + Assert.Equal(80, imageBytes.BodyAsObject[1]); + Assert.Equal(78, imageBytes.BodyAsObject[2]); + Assert.Equal(130, imageBytes.BodyAsObject.Last()); + } + } +} diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj index 3165b307..31d36283 100644 --- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj +++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj @@ -77,6 +77,7 @@ + diff --git a/Octokit.Tests/Http/HttpClientAdapterTests.cs b/Octokit.Tests/Http/HttpClientAdapterTests.cs index 612f3811..a514186f 100644 --- a/Octokit.Tests/Http/HttpClientAdapterTests.cs +++ b/Octokit.Tests/Http/HttpClientAdapterTests.cs @@ -133,6 +133,23 @@ namespace Octokit.Tests.Http Assert.Null(response.ContentType); } + [Fact] + public async Task BuildsByteArrayResponseFromResponseMessage() + { + var responseMessage = new HttpResponseMessage + { + StatusCode = HttpStatusCode.OK, + Content = new ByteArrayContent(new byte[] { 0, 1, 1, 0, 1}), + }; + var tester = new HttpClientAdapterTester(); + + var response = await tester.BuildResponseTester(responseMessage); + + Assert.Equal(new byte[] { 0, 1, 1, 0, 1 }, response.BodyAsObject); + Assert.Null(response.Body); + Assert.Null(response.ContentType); + } + public async Task SetsContentType(HttpStatusCode httpStatusCode) { var responseMessage = new HttpResponseMessage diff --git a/Octokit/Http/HttpClientAdapter.cs b/Octokit/Http/HttpClientAdapter.cs index a27bc77e..5d59c212 100644 --- a/Octokit/Http/HttpClientAdapter.cs +++ b/Octokit/Http/HttpClientAdapter.cs @@ -63,12 +63,21 @@ namespace Octokit.Internal Ensure.ArgumentNotNull(responseMessage, "responseMessage"); string responseBody = null; + object bodyAsObject = null; string contentType = null; using (var content = responseMessage.Content) { if (content != null) { - responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + if (typeof(T) != typeof(byte[])) + { + responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + } + else + { + bodyAsObject = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false); + } + contentType = GetContentType(content); } } @@ -76,6 +85,7 @@ namespace Octokit.Internal var response = new ApiResponse { Body = responseBody, + BodyAsObject = (T)bodyAsObject, StatusCode = responseMessage.StatusCode, ContentType = contentType }; diff --git a/Octokit/Http/IResponse.cs b/Octokit/Http/IResponse.cs index 143b0552..89b33fed 100644 --- a/Octokit/Http/IResponse.cs +++ b/Octokit/Http/IResponse.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Net; -using Octokit.Internal; namespace Octokit {