Skip to content

Commit

Permalink
added more tests for CachingHttpClient::Send
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyfrankyren committed Jan 10, 2023
1 parent 63f236c commit 6c5d2d9
Showing 1 changed file with 65 additions and 12 deletions.
77 changes: 65 additions & 12 deletions Octokit.Tests/Caching/CachingHttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NSubstitute.ReturnsExtensions;
using Octokit.Caching;
using Octokit.Internal;
Expand Down Expand Up @@ -104,8 +105,8 @@ public async Task UsesCachedResponseIfEtagIsPresentAndGithubReturns304()
}

[Theory]
[MemberData(nameof(NonNotModifiedHttpStatusCodes))]
public async Task UsesGithubResponseIfEtagIsPresentAndGithubReturnsNon304(HttpStatusCode httpStatusCode)
[MemberData(nameof(NonNotModifiedHttpStatusCodesWithSetCacheFailure))]
public async Task UsesGithubResponseIfEtagIsPresentAndGithubReturnsNon304(HttpStatusCode httpStatusCode, bool setCacheThrows)
{
// arrange
var underlyingClient = Substitute.For<IHttpClient>();
Expand All @@ -126,6 +127,10 @@ public async Task UsesGithubResponseIfEtagIsPresentAndGithubReturnsNon304(HttpSt

underlyingClient.Send(Arg.Is<IRequest>(req => req == request && req.Headers["If-None-Matched"] == etag), cancellationToken).ReturnsForAnyArgs(githubResponse);
responseCache.GetAsync(request).Returns(cachedV1Response);
if (setCacheThrows)
{
responseCache.SetAsync(request, Arg.Any<CachedResponse.V1>()).ThrowsForAnyArgs<Exception>();
}

var cachingHttpClient = new CachingHttpClient(underlyingClient, responseCache);

Expand All @@ -141,19 +146,20 @@ public async Task UsesGithubResponseIfEtagIsPresentAndGithubReturnsNon304(HttpSt
responseCache.Received(1).SetAsync(request, Arg.Is<CachedResponse.V1>(v1 => new ResponseComparer().Equals(v1, CachedResponse.V1.Create(githubResponse))));
}

public static IEnumerable<object[]> NonNotModifiedHttpStatusCodes()
public static IEnumerable<object[]> NonNotModifiedHttpStatusCodesWithSetCacheFailure()
{
foreach (var statusCode in Enum.GetValues(typeof(HttpStatusCode)))
{
if (!statusCode.Equals(HttpStatusCode.NotModified))
{
yield return new[] { statusCode };
}
if (statusCode.Equals(HttpStatusCode.NotModified)) continue;
yield return new[] { statusCode, true };
yield return new[] { statusCode, false };
}
}

[Fact]
public async Task UsesGithubResponseIfCachedEntryIsNull()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task UsesGithubResponseIfCachedEntryIsNull(bool setCacheThrows)
{
// arrange
var underlyingClient = Substitute.For<IHttpClient>();
Expand All @@ -168,6 +174,10 @@ public async Task UsesGithubResponseIfCachedEntryIsNull()

underlyingClient.Send(Arg.Is<IRequest>(req => req == request && !req.Headers.Any()), cancellationToken).ReturnsForAnyArgs(githubResponse);
responseCache.GetAsync(request).ReturnsNull();
if (setCacheThrows)
{
responseCache.SetAsync(request, Arg.Any<CachedResponse.V1>()).ThrowsForAnyArgs<Exception>();
}

var cachingHttpClient = new CachingHttpClient(underlyingClient, responseCache);

Expand All @@ -184,9 +194,48 @@ public async Task UsesGithubResponseIfCachedEntryIsNull()
}

[Theory]
[InlineData(null)]
[InlineData("")]
public async Task UsesGithubResponseIfCachedEntryEtagIsNullOrEmpty(string etag)
[InlineData(true)]
[InlineData(false)]
public async Task UsesGithubResponseIfGetCachedEntryThrows(bool setCacheThrows)
{
// arrange
var underlyingClient = Substitute.For<IHttpClient>();
var responseCache = Substitute.For<IResponseCache>();
var request = Substitute.For<IRequest>();
request.Method.Returns(HttpMethod.Get);
request.Headers.Returns(new Dictionary<string, string>());

var cancellationToken = CancellationToken.None;

var githubResponse = Substitute.For<IResponse>();

underlyingClient.Send(Arg.Is<IRequest>(req => req == request && !req.Headers.Any()), cancellationToken).ReturnsForAnyArgs(githubResponse);
responseCache.GetAsync(Args.Request).ThrowsForAnyArgs<Exception>();
if (setCacheThrows)
{
responseCache.SetAsync(request, Arg.Any<CachedResponse.V1>()).ThrowsForAnyArgs<Exception>();
}

var cachingHttpClient = new CachingHttpClient(underlyingClient, responseCache);

// act
var response = await cachingHttpClient.Send(request, cancellationToken);

// assert
Assert.Equal(githubResponse, response, new ResponseComparer());
Assert.Single(underlyingClient.ReceivedCalls());
underlyingClient.Received(1).Send(request, cancellationToken);
Assert.Equal(2, responseCache.ReceivedCalls().Count());
responseCache.Received(1).GetAsync(request);
responseCache.Received(1).SetAsync(request, Arg.Is<CachedResponse.V1>(v1 => new ResponseComparer().Equals(v1, CachedResponse.V1.Create(githubResponse))));
}

[Theory]
[InlineData(null, true)]
[InlineData(null, false)]
[InlineData("", true)]
[InlineData("", false)]
public async Task UsesGithubResponseIfCachedEntryEtagIsNullOrEmpty(string etag, bool setCacheThrows)
{
// arrange
var underlyingClient = Substitute.For<IHttpClient>();
Expand All @@ -205,6 +254,10 @@ public async Task UsesGithubResponseIfCachedEntryEtagIsNullOrEmpty(string etag)

underlyingClient.Send(Arg.Is<IRequest>(req => req == request && !req.Headers.Any()), cancellationToken).ReturnsForAnyArgs(githubResponse);
responseCache.GetAsync(request).Returns(cachedV1Response);
if (setCacheThrows)
{
responseCache.SetAsync(request, Arg.Any<CachedResponse.V1>()).ThrowsForAnyArgs<Exception>();
}

var cachingHttpClient = new CachingHttpClient(underlyingClient, responseCache);

Expand Down

0 comments on commit 6c5d2d9

Please sign in to comment.