-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
tests/PinguApps.Appwrite.Client.Tests/Utils/ResponseUtilsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |