Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) RepositoryContentsClient.GetArchive does not return the expected binary content #2803

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions Octokit.Tests/Clients/RepositoryContentsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using NSubstitute;
using Xunit;
using System.Collections.Generic;
using System.Net;
using Octokit.Internal;

namespace Octokit.Tests.Clients
{
Expand Down Expand Up @@ -748,7 +750,7 @@ public async Task RequestsCorrectUrl1()
const string expectedUri = "repos/org/repo/tarball/";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -762,7 +764,7 @@ public async Task RequestsCorrectUrl1WithRepositoryId()
const string expectedUri = "repositories/1/tarball/";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -776,7 +778,7 @@ public async Task RequestsCorrectUrl2()
const string expectedUri = "repos/org/repo/zipball/";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -790,7 +792,7 @@ public async Task RequestsCorrectUrl2WithRepositoryId()
const string expectedUri = "repositories/1/zipball/";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -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<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -818,7 +820,7 @@ public async Task RequestsCorrectUrl3WithRepositoryId()
const string expectedUri = "repositories/1/zipball/ref";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -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<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand All @@ -846,7 +848,7 @@ public async Task RequestsCorrectUrl4WithRepositoryId()
const string expectedUri = "repositories/1/zipball/ref";
var expectedTimeSpan = TimeSpan.FromMinutes(60);

connection.Connection.Received().Get<byte[]>(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
connection.Connection.Received().GetRaw(Arg.Is<Uri>(uri => uri.ToString() == expectedUri), null, Arg.Is<TimeSpan>(span => span == expectedTimeSpan));
}

[Fact]
Expand Down Expand Up @@ -882,6 +884,27 @@ public async Task EnsuresNonNullArguments()

await Assert.ThrowsAsync<ArgumentException>(() => client.GetArchive(1, ArchiveFormat.Tarball, "ref", TimeSpan.Zero));
}

[Fact]
public async Task ReturnsExpectedContent()
{
var headers = new Dictionary<string, string>();
var response = TestSetup.CreateResponse(HttpStatusCode.OK, new byte[] { 1, 2, 3, 4 }, headers);
var responseTask = Task.FromResult<IApiResponse<byte[]>>(new ApiResponse<byte[]>(response));

var connection = Substitute.For<IConnection>();
connection.GetRaw(Arg.Is<Uri>(u => u.ToString() == "repos/org/repo/tarball/"), null, TimeSpan.FromMinutes(60))
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
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);
}
}
}
}
4 changes: 2 additions & 2 deletions Octokit/Clients/RepositoryContentsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ public async Task<byte[]> GetArchive(string owner, string name, ArchiveFormat ar

var endpoint = ApiUrls.RepositoryArchiveLink(owner, name, archiveFormat, reference);

var response = await Connection.Get<byte[]>(endpoint, timeout).ConfigureAwait(false);
var response = await Connection.GetRaw(endpoint, null, timeout).ConfigureAwait(false);

return response.Body;
}
Expand All @@ -416,7 +416,7 @@ public async Task<byte[]> GetArchive(long repositoryId, ArchiveFormat archiveFor

var endpoint = ApiUrls.RepositoryArchiveLink(repositoryId, archiveFormat, reference);

var response = await Connection.Get<byte[]>(endpoint, timeout).ConfigureAwait(false);
var response = await Connection.GetRaw(endpoint, null, timeout).ConfigureAwait(false);

return response.Body;
}
Expand Down
16 changes: 15 additions & 1 deletion Octokit/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,21 @@ public Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> pa
Endpoint = uri.ApplyParameters(parameters)
});
}


/// <inheritdoc/>
public Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, TimeSpan timeout)
{
Ensure.ArgumentNotNull(uri, nameof(uri));

return GetRaw(new Request
{
Method = HttpMethod.Get,
BaseAddress = BaseAddress,
Endpoint = uri.ApplyParameters(parameters),
Timeout = timeout
});
}

/// <inheritdoc/>
public Task<IApiResponse<Stream>> GetRawStream(Uri uri, IDictionary<string, string> parameters)
{
Expand Down
12 changes: 11 additions & 1 deletion Octokit/Http/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ public interface IConnection : IApiInfoProvider
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters);


/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
/// <param name="uri">URI endpoint to send request to</param>
/// <param name="parameters">Querystring parameters for the request</param>
/// <param name="timeout">The Timeout value</param>
/// <returns><seealso cref="IResponse"/> representing the received HTTP response</returns>
/// <remarks>The <see cref="IResponse.Body"/> property will be <c>null</c> if the <paramref name="uri"/> points to a directory instead of a file</remarks>
Task<IApiResponse<byte[]>> GetRaw(Uri uri, IDictionary<string, string> parameters, TimeSpan timeout);

/// <summary>
/// Performs an asynchronous HTTP GET request that expects a <seealso cref="IResponse"/> containing raw data.
/// </summary>
Expand Down
Loading