diff --git a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs index 1d6046aa83..c07e33e162 100644 --- a/Octokit.Tests/Clients/RepositoryContentsClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryContentsClientTests.cs @@ -4,6 +4,8 @@ using NSubstitute; using Xunit; using System.Collections.Generic; +using System.Net; +using Octokit.Internal; namespace Octokit.Tests.Clients { @@ -748,7 +750,7 @@ public async Task RequestsCorrectUrl1() const string expectedUri = "repos/org/repo/tarball/"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -762,7 +764,7 @@ public async Task RequestsCorrectUrl1WithRepositoryId() const string expectedUri = "repositories/1/tarball/"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -776,7 +778,7 @@ public async Task RequestsCorrectUrl2() const string expectedUri = "repos/org/repo/zipball/"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -790,7 +792,7 @@ public async Task RequestsCorrectUrl2WithRepositoryId() const string expectedUri = "repositories/1/zipball/"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -804,7 +806,7 @@ public async Task RequestsCorrectUrl3() const string expectedUri = "repos/org/repo/zipball/ref"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -818,7 +820,7 @@ public async Task RequestsCorrectUrl3WithRepositoryId() const string expectedUri = "repositories/1/zipball/ref"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -832,7 +834,7 @@ public async Task RequestsCorrectUrl4() const string expectedUri = "repos/org/repo/zipball/ref"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -846,7 +848,7 @@ public async Task RequestsCorrectUrl4WithRepositoryId() const string expectedUri = "repositories/1/zipball/ref"; var expectedTimeSpan = TimeSpan.FromMinutes(60); - connection.Connection.Received().Get(Arg.Is(uri => uri.ToString() == expectedUri), Arg.Is(span => span == expectedTimeSpan)); + connection.Connection.Received().GetRaw(Arg.Is(uri => uri.ToString() == expectedUri), null, Arg.Is(span => span == expectedTimeSpan)); } [Fact] @@ -882,6 +884,27 @@ public async Task EnsuresNonNullArguments() await Assert.ThrowsAsync(() => client.GetArchive(1, ArchiveFormat.Tarball, "ref", TimeSpan.Zero)); } + + [Fact] + public async Task ReturnsExpectedContent() + { + var headers = new Dictionary(); + var response = TestSetup.CreateResponse(HttpStatusCode.OK, new byte[] { 1, 2, 3, 4 }, headers); + var responseTask = Task.FromResult>(new ApiResponse(response)); + + var connection = Substitute.For(); + connection.GetRaw(Arg.Is(u => u.ToString() == "repos/org/repo/tarball/"), null, TimeSpan.FromMinutes(60)) + .Returns(responseTask); + + var apiConnection = Substitute.For(); + apiConnection.Connection.Returns(connection); + + var client = new RepositoryContentsClient(apiConnection); + + var actual = await client.GetArchive("org", "repo"); + + Assert.Equal(new byte[] { 1, 2, 3, 4 }, actual); + } } } } \ No newline at end of file diff --git a/Octokit/Clients/RepositoryContentsClient.cs b/Octokit/Clients/RepositoryContentsClient.cs index 200cc473a0..409acd9559 100644 --- a/Octokit/Clients/RepositoryContentsClient.cs +++ b/Octokit/Clients/RepositoryContentsClient.cs @@ -395,7 +395,7 @@ public async Task GetArchive(string owner, string name, ArchiveFormat ar var endpoint = ApiUrls.RepositoryArchiveLink(owner, name, archiveFormat, reference); - var response = await Connection.Get(endpoint, timeout).ConfigureAwait(false); + var response = await Connection.GetRaw(endpoint, null, timeout).ConfigureAwait(false); return response.Body; } @@ -416,7 +416,7 @@ public async Task GetArchive(long repositoryId, ArchiveFormat archiveFor var endpoint = ApiUrls.RepositoryArchiveLink(repositoryId, archiveFormat, reference); - var response = await Connection.Get(endpoint, timeout).ConfigureAwait(false); + var response = await Connection.GetRaw(endpoint, null, timeout).ConfigureAwait(false); return response.Body; } diff --git a/Octokit/Http/Connection.cs b/Octokit/Http/Connection.cs index c0fc81f4ee..3225bf88db 100644 --- a/Octokit/Http/Connection.cs +++ b/Octokit/Http/Connection.cs @@ -242,7 +242,21 @@ public Task> GetRaw(Uri uri, IDictionary pa Endpoint = uri.ApplyParameters(parameters) }); } - + + /// + public Task> GetRaw(Uri uri, IDictionary parameters, TimeSpan timeout) + { + Ensure.ArgumentNotNull(uri, nameof(uri)); + + return GetRaw(new Request + { + Method = HttpMethod.Get, + BaseAddress = BaseAddress, + Endpoint = uri.ApplyParameters(parameters), + Timeout = timeout + }); + } + /// public Task> GetRawStream(Uri uri, IDictionary parameters) { diff --git a/Octokit/Http/IConnection.cs b/Octokit/Http/IConnection.cs index 8301b64c0d..c9f8daa848 100644 --- a/Octokit/Http/IConnection.cs +++ b/Octokit/Http/IConnection.cs @@ -30,7 +30,17 @@ public interface IConnection : IApiInfoProvider /// representing the received HTTP response /// The property will be null if the points to a directory instead of a file Task> GetRaw(Uri uri, IDictionary parameters); - + + /// + /// Performs an asynchronous HTTP GET request that expects a containing raw data. + /// + /// URI endpoint to send request to + /// Querystring parameters for the request + /// The Timeout value + /// representing the received HTTP response + /// The property will be null if the points to a directory instead of a file + Task> GetRaw(Uri uri, IDictionary parameters, TimeSpan timeout); + /// /// Performs an asynchronous HTTP GET request that expects a containing raw data. ///