diff --git a/src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs b/src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs index 01fbc0d1..ecfeacc4 100644 --- a/src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs +++ b/src/PinguApps.Appwrite.Client/Utils/ResponseUtils.cs @@ -18,7 +18,7 @@ internal static AppwriteResult GetApiResponse(this IApiResponse result) return new AppwriteResult(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."); } diff --git a/tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs b/tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs new file mode 100644 index 00000000..a7663bc4 --- /dev/null +++ b/tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs @@ -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>(); + 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>(); + 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>(); + 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>(); + mockApiResponse.SetupGet(r => r.IsSuccessStatusCode).Returns(false); + mockApiResponse.SetupGet(x => x.Error).Returns(exception); + + Assert.Throws(() => 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); + } +}