#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.DTO.Common;
using Lumina.Presentation.Web.Common.DTO.Configuration;
using Lumina.Presentation.Web.Common.DTO.FileSystemManagement;
using Lumina.Presentation.Web.Common.Exceptions;
using Lumina.Presentation.Web.Fixtures.Common.Api;
using Lumina.Presentation.Web.Fixtures.Common.DTO.Configuration;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.UnitTests.Common.Api;
///
/// Contains unit tests for the class.
///
[ExcludeFromCodeCoverage]
public class ApiHttpClientTests
{
private readonly ServerConfigurationDtoFixture _serverConfigurationDtoFixture = new();
[Fact]
public async Task GetAsync_WhenApiReturnsSuccess_ShouldDeserializeResponseContent()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, """[{"title":"Library One"}]"""));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
Web.Common.DTO.Libraries.LibraryDto[] result = await sut.GetAsync("libraries", CancellationToken.None);
// Assert
Assert.Single(result);
Assert.Equal("Library One", result[0].Title);
Assert.Equal(new Uri("http://localhost:5214/api/v1/libraries"), messageHandler.Requests[0].RequestUri);
}
[Fact]
public async Task GetAsync_WhenApiReturnsError_ShouldThrowApiExceptionWithProblemDetails()
{
// Arrange
string problemDetailsJson = """
{
"type": "https://tools.ietf.org/html/rfc4918#section-11.2",
"title": "General.Validation",
"status": 422,
"detail": "OneOrMoreValidationErrorsOccurred"
}
""";
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.UnprocessableEntity, problemDetailsJson));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
ApiException exception = await Assert.ThrowsAsync(() => sut.GetAsync