Files
Nemera/tests/UnitTests/Lumina.Presentation.Web.UnitTests/Common/Api/ApiHttpClientTests.cs
T
BinaryAlley 0a86f1a70b Replaces the custom EmptyRequest with explicit API client operations
Removes the Web-specific EmptyRequest record struct and keeps FastEndpoints.EmptyRequest only as an endpoint request DTO.

Adds bodyless and response-ignoring client overloads, centralizes request construction and response disposal, and separates stale-session failures from generic 404 responses.

Updates the API client stub and tests, including regression coverage for non-empty response bodies, overload selection, and crafted theme asset paths.
2026-11-20 08:52:59 +02:00

278 lines
12 KiB
C#

#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;
/// <summary>
/// Contains unit tests for the <see cref="ApiHttpClient"/> class.
/// </summary>
[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<Web.Common.DTO.Libraries.LibraryDto[]>("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<ApiException>(() => sut.GetAsync<object>("libraries", CancellationToken.None));
// Assert
Assert.Equal(HttpStatusCode.UnprocessableEntity, exception.HttpStatusCode);
Assert.NotNull(exception.ProblemDetails);
Assert.Equal("General.Validation", exception.ProblemDetails!.Title);
Assert.Equal(StatusCodes.Status422UnprocessableEntity, exception.ProblemDetails.Status);
Assert.Equal("OneOrMoreValidationErrorsOccurred", exception.ProblemDetails.Detail);
}
[Fact]
public async Task GetAsync_WhenApiReturnsEmptyContent_ShouldReturnDefault()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(string.Empty) });
ApiHttpClient sut = CreateSut(messageHandler);
// Act
object? result = await sut.GetAsync<object>("libraries", CancellationToken.None);
// Assert
Assert.Null(result);
}
[Fact]
public async Task GetAsync_WhenUserHasTokenClaim_ShouldSendAuthorizationHeader()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, "{}"));
ApiHttpClient sut = CreateSut(messageHandler, token: "test-jwt-token");
// Act
await sut.GetAsync<object>("libraries", CancellationToken.None);
// Assert
Assert.Equal("Bearer", messageHandler.Requests[0].Headers.Authorization!.Scheme);
Assert.Equal("test-jwt-token", messageHandler.Requests[0].Headers.Authorization!.Parameter);
}
[Fact]
public async Task PostAsync_WhenCalled_ShouldSerializePayloadAndDeserializeResponse()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, """{"success":true}"""));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
SuccessResponse result = await sut.PostAsync<SuccessResponse, object>("auth/login", new { username = "testuser" }, CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
Assert.Equal(HttpMethod.Post, messageHandler.Requests[0].Method);
Assert.Equal("http://localhost:5214/api/v1/auth/login", messageHandler.Requests[0].RequestUri!.ToString());
Assert.Contains("testuser", messageHandler.RequestBodies[0]);
}
[Fact]
public async Task PostAsync_WhenCalledWithoutPayload_ShouldDeserializeResponse()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, """{"success":true}"""));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
SuccessResponse result = await sut.PostAsync<SuccessResponse>("libraries", CancellationToken.None);
// Assert
Assert.True(result.IsSuccess);
Assert.Equal(HttpMethod.Post, messageHandler.Requests[0].Method);
Assert.Null(messageHandler.Requests[0].Content);
}
[Fact]
public async Task PostIgnoringResponseAsync_WhenCalledWithoutPayload_ShouldSendBodylessRequestAndIgnoreNonEmptyResponse()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, "{}"));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
await sut.PostIgnoringResponseAsync("libraries/1", CancellationToken.None);
// Assert
Assert.Equal(HttpMethod.Post, messageHandler.Requests[0].Method);
Assert.Null(messageHandler.Requests[0].Content);
}
[Fact]
public async Task PostIgnoringResponseAsync_WhenUserHasTokenClaim_ShouldSendAuthorizationHeader()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, "{}"));
ApiHttpClient sut = CreateSut(messageHandler, token: "test-jwt-token");
// Act
await sut.PostIgnoringResponseAsync("libraries/1", CancellationToken.None);
// Assert
Assert.Equal("Bearer", messageHandler.Requests[0].Headers.Authorization!.Scheme);
Assert.Equal("test-jwt-token", messageHandler.Requests[0].Headers.Authorization!.Parameter);
}
[Fact]
public async Task PutIgnoringResponseAsync_WhenCalledWithPayload_ShouldSerializePayloadAndIgnoreNonEmptyResponse()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.OK, """{"updated":true}"""));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
await sut.PutIgnoringResponseAsync("libraries/1", new { title = "Library One" }, CancellationToken.None);
// Assert
Assert.Contains("Library One", messageHandler.RequestBodies[0]);
}
[Fact]
public async Task PostIgnoringResponseAsync_WhenApiReturnsError_ShouldThrowApiExceptionWithProblemDetails()
{
// Arrange
string problemDetailsJson = """
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
"title": "General.Failure",
"status": 500,
"detail": "UnexpectedFailure"
}
""";
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.InternalServerError, problemDetailsJson));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
ApiException exception = await Assert.ThrowsAsync<ApiException>(() => sut.PostIgnoringResponseAsync("libraries/1", CancellationToken.None));
// Assert
Assert.Equal(HttpStatusCode.InternalServerError, exception.HttpStatusCode);
Assert.NotNull(exception.ProblemDetails);
Assert.Equal("General.Failure", exception.ProblemDetails!.Title);
Assert.Equal("UnexpectedFailure", exception.ProblemDetails.Detail);
}
[Fact]
public async Task DeleteAsync_WhenApiReturnsError_ShouldThrowApiException()
{
// Arrange
TestApiHttpMessageHandler messageHandler = new(_ => CreateJsonResponse(HttpStatusCode.InternalServerError, """{"title":"General.Failure","status":500}"""));
ApiHttpClient sut = CreateSut(messageHandler);
// Act
ApiException exception = await Assert.ThrowsAsync<ApiException>(() => sut.DeleteAsync("libraries/1", CancellationToken.None));
// Assert
Assert.Equal(HttpStatusCode.InternalServerError, exception.HttpStatusCode);
Assert.Equal("/api/v1/libraries/1", exception.RequestPath);
}
[Fact]
public async Task GetBlobAsync_WhenApiReturnsSuccess_ShouldReturnBlobData()
{
// Arrange
byte[] imageBytes = Encoding.UTF8.GetBytes("fake-image-bytes");
TestApiHttpMessageHandler messageHandler = new(_ =>
{
HttpResponseMessage response = new(HttpStatusCode.OK) { Content = new ByteArrayContent(imageBytes) };
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
return response;
});
ApiHttpClient sut = CreateSut(messageHandler);
// Act
BlobDataDto blob = await sut.GetBlobAsync("libraries/1/cover", CancellationToken.None);
// Assert
Assert.Equal(imageBytes, blob.Data);
Assert.Equal("image/png", blob.ContentType);
}
/// <summary>
/// Creates the system under test configured with the provided message handler.
/// </summary>
/// <param name="messageHandler">The message handler backing the inner <see cref="HttpClient"/>.</param>
/// <param name="token">Optional token claim to place on the current HTTP context user.</param>
/// <returns>The created <see cref="ApiHttpClient"/>.</returns>
private ApiHttpClient CreateSut(TestApiHttpMessageHandler messageHandler, string? token = null)
{
HttpClient httpClient = new(messageHandler);
IHttpContextAccessor httpContextAccessor = Substitute.For<IHttpContextAccessor>();
if (token is not null)
{
DefaultHttpContext httpContext = new()
{
User = new ClaimsPrincipal(new ClaimsIdentity([new Claim("Token", token)], "TestAuthentication"))
};
httpContextAccessor.HttpContext.Returns(httpContext);
}
IOptionsSnapshot<ServerConfigurationDto> serverConfigurationOptions = Substitute.For<IOptionsSnapshot<ServerConfigurationDto>>();
serverConfigurationOptions.Value.Returns(_serverConfigurationDtoFixture.Create(apiVersion: '1', baseAddress: "http://localhost", port: 5214));
return new ApiHttpClient(httpClient, httpContextAccessor, serverConfigurationOptions);
}
/// <summary>
/// Creates an <see cref="HttpResponseMessage"/> with the given status code and JSON body.
/// </summary>
/// <param name="statusCode">The HTTP status code of the response.</param>
/// <param name="json">The JSON payload of the response body.</param>
/// <returns>The created <see cref="HttpResponseMessage"/>.</returns>
private static HttpResponseMessage CreateJsonResponse(HttpStatusCode statusCode, string json)
{
return new HttpResponseMessage(statusCode)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
}
}