Skip to content

Commit

Permalink
Added response utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Jul 7, 2024
1 parent ae40c19 commit 31c1eb5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static AppwriteResult<T> GetApiResponse<T>(this IApiResponse<T> result)
return new AppwriteResult<T>(result.Content);
}

if (result.Error?.Content is null)
if (result.Error?.Content is null || string.IsNullOrEmpty(result.Error.Content))
{
throw new Exception("Unknown error encountered.");
}
Expand Down
80 changes: 80 additions & 0 deletions tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Net;
using Moq;
using PinguApps.Appwrite.Client.Utils;
using PinguApps.Appwrite.Shared.Tests;
using Refit;

namespace PinguApps.Appwrite.Client.Tests.Utils;
public class ResponseUtilsTests
{
[Fact]
public void GetApiResponse_Success_ReturnsContent()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
mockApiResponse.SetupGet(r => r.Content).Returns("Success");

var result = mockApiResponse.Object.GetApiResponse();

Assert.True(result.Success);
Assert.Equal("Success", result.Result);
}

[Fact]
public void GetApiResponse_SuccessButNullContent_ReturnsInternalError()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
mockApiResponse.SetupGet(r => r.Content).Returns((string?)null);

var result = mockApiResponse.Object.GetApiResponse();

Assert.False(result.Success);
Assert.True(result.Result.IsT2);
}

[Fact]
public async Task GetApiResponse_Failure_ReturnsError()
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(Constants.AppwriteError)
};
var exception = await ApiException.Create(new HttpRequestMessage(), HttpMethod.Get, response, new RefitSettings());

var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns(exception);

var result = mockApiResponse.Object.GetApiResponse();

Assert.False(result.Success);
Assert.True(result.IsAppwriteError);
Assert.True(result.Result.IsT1);
}

[Fact]
public async Task GetApiResponse_FailureButNullErrorContent_ThrowsException()
{
var exception = await ApiException.Create(new HttpRequestMessage(), HttpMethod.Get, new HttpResponseMessage(HttpStatusCode.InternalServerError), new RefitSettings());

var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns(exception);

Assert.Throws<Exception>(() => mockApiResponse.Object.GetApiResponse());
}

[Fact]
public void GetExceptionResponse_ReturnsInternalError()
{
var exception = new Exception("Test exception");

var result = exception.GetExceptionResponse<string>();

Assert.False(result.Success);
Assert.True(result.IsInternalError);
Assert.True(result.Result.IsT2);
Assert.Equal("Test exception", result.Result.AsT2.Message);
}
}

0 comments on commit 31c1eb5

Please sign in to comment.