test: add all 6 status code mapping tests to ApiClientBaseTests
- Add GetAsync_Returns404_MapsToNotFound test - Add GetAsync_Returns400_WithValidationErrors_MapsToValidationError test - Add GetAsync_Returns401_MapsToUnauthorized test - Add GetAsync_Returns403_MapsToForbidden test - Add GetAsync_Returns500_MapsToApiError test - Add FrameworkReference to Microsoft.AspNetCore.App for ValidationProblemDetails
This commit is contained in:
@@ -7,6 +7,10 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
<PackageReference Include="coverlet.collector" Version="6.0.4" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||||
|
|||||||
@@ -36,4 +36,85 @@ public class ApiClientBaseTests
|
|||||||
result.Value.Id.ShouldBe(42);
|
result.Value.Id.ShouldBe(42);
|
||||||
result.Value.Name.ShouldBe("Test");
|
result.Value.Name.ShouldBe("Test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_Returns404_MapsToNotFound()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockHttp.When("/api/test")
|
||||||
|
.Respond(HttpStatusCode.NotFound);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _client.GetAsync<TestDto>("/api/test");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.IsNotFound.ShouldBeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_Returns400_WithValidationErrors_MapsToValidationError()
|
||||||
|
{
|
||||||
|
// Arrange - use actual ValidationProblemDetails structure to match production
|
||||||
|
var validationProblem = new Microsoft.AspNetCore.Mvc.ValidationProblemDetails
|
||||||
|
{
|
||||||
|
Errors =
|
||||||
|
{
|
||||||
|
["Name"] = new[] { "Name is required" },
|
||||||
|
["Id"] = new[] { "Id must be positive" }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_mockHttp.When("/api/test")
|
||||||
|
.Respond(HttpStatusCode.BadRequest, "application/json", JsonSerializer.Serialize(validationProblem, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _client.GetAsync<TestDto>("/api/test");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.IsValidationError.ShouldBeTrue();
|
||||||
|
result.ValidationError.FieldErrors.ShouldContainKey("Name");
|
||||||
|
result.ValidationError.FieldErrors["Name"].ShouldContain("Name is required");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_Returns401_MapsToUnauthorized()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockHttp.When("/api/test")
|
||||||
|
.Respond(HttpStatusCode.Unauthorized);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _client.GetAsync<TestDto>("/api/test");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.IsUnauthorized.ShouldBeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_Returns403_MapsToForbidden()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockHttp.When("/api/test")
|
||||||
|
.Respond(HttpStatusCode.Forbidden);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _client.GetAsync<TestDto>("/api/test");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.IsForbidden.ShouldBeTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetAsync_Returns500_MapsToApiError()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_mockHttp.When("/api/test")
|
||||||
|
.Respond(HttpStatusCode.InternalServerError, "text/plain", "Internal Server Error");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await _client.GetAsync<TestDto>("/api/test");
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.IsError.ShouldBeTrue();
|
||||||
|
result.Error.StatusCode.ShouldBe(500);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user