Skip to content

Commit

Permalink
Added utils for server to match client and added server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pingu2k4 committed Aug 9, 2024
1 parent 1afad7b commit 5ec12d9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/PinguApps.Appwrite.Server/Utils/ResponseUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
using System;
using System.Text.Json;
using OneOf.Types;
using PinguApps.Appwrite.Shared;
using Refit;

namespace PinguApps.Appwrite.Server.Utils;
internal static class ResponseUtils
{
internal static AppwriteResult GetApiResponse(this IApiResponse result)
{
if (result.IsSuccessStatusCode)
{
return new AppwriteResult(new Success());
}

if (result.Error?.Content is null || string.IsNullOrEmpty(result.Error.Content))
{
throw new Exception("Unknown error encountered.");
}

var error = JsonSerializer.Deserialize<AppwriteError>(result.Error.Content);

return new AppwriteResult(error!);
}

internal static AppwriteResult<T> GetApiResponse<T>(this IApiResponse<T> result)
{
if (result.IsSuccessStatusCode)
Expand All @@ -28,6 +46,11 @@ internal static AppwriteResult<T> GetApiResponse<T>(this IApiResponse<T> result)
return new AppwriteResult<T>(error!);
}

internal static AppwriteResult GetExceptionResponse(this Exception e)
{
return new AppwriteResult(new InternalError(e.Message));
}

internal static AppwriteResult<T> GetExceptionResponse<T>(this Exception e)
{
return new AppwriteResult<T>(new InternalError(e.Message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace PinguApps.Appwrite.Client.Tests.Utils;
public class ResponseUtilsTests
{

[Fact]
public void GetApiResponse_Success_ReturnsContent()
{
Expand Down
84 changes: 81 additions & 3 deletions tests/PinguApps.Appwrite.Server.Tests/Utils/ResponseUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using Moq;
using OneOf.Types;
using PinguApps.Appwrite.Server.Utils;
using PinguApps.Appwrite.Shared.Tests;
using Refit;
Expand All @@ -9,6 +10,60 @@ public class ResponseUtilsTests
{
[Fact]
public void GetApiResponse_Success_ReturnsContent()
{
var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);

var result = mockApiResponse.Object.GetApiResponse();

Assert.True(result.Success);
Assert.IsType<Success>(result.Result.AsT0);
}

[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>();
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>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns(exception);

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

[Fact]
public void GetApiResponse_FailureButNullError_ThrowsException()
{
var mockApiResponse = new Mock<IApiResponse>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns((ApiException)null!);

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

[Fact]
public void GenericGetApiResponse_Success_ReturnsContent()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
Expand All @@ -21,7 +76,7 @@ public void GetApiResponse_Success_ReturnsContent()
}

[Fact]
public void GetApiResponse_SuccessButNullContent_ReturnsInternalError()
public void GenericGetApiResponse_SuccessButNullContent_ReturnsInternalError()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(true);
Expand All @@ -34,7 +89,7 @@ public void GetApiResponse_SuccessButNullContent_ReturnsInternalError()
}

[Fact]
public async Task GetApiResponse_Failure_ReturnsError()
public async Task GenericGetApiResponse_Failure_ReturnsError()
{
var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Expand All @@ -54,7 +109,7 @@ public async Task GetApiResponse_Failure_ReturnsError()
}

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

Expand All @@ -65,11 +120,34 @@ public async Task GetApiResponse_FailureButNullErrorContent_ThrowsException()
Assert.Throws<Exception>(() => mockApiResponse.Object.GetApiResponse());
}

[Fact]
public void GenericGetApiResponse_FailureButNullError_ThrowsException()
{
var mockApiResponse = new Mock<IApiResponse<string>>();
mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false);
mockApiResponse.SetupGet(x => x.Error).Returns((ApiException)null!);

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

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

var result = exception.GetExceptionResponse();

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

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

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

Assert.False(result.Success);
Expand Down

0 comments on commit 5ec12d9

Please sign in to comment.