I'm trying to test the health check endpoint.
I've implemented an integration test with .net core and WebApplicationFactory and TestServer, in order to test my API endpoints.
I wrote test cases(xUnit) to test my API endpoints, the simple API controllers work just fine, but it's not found the Health check endpoint - it is not a controller, it's a configurable endpoint
Here is the service configuration
services.AddHealthChecks().AddCheck<PingHealthCheck>("ping_health_check");
Here is the configuration
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
// Prevent response caching
AllowCachingResponses = false,
ResponseWriter = (context, report) => context.Response
//Return an object instead of a plain text
.WriteAsync(JsonConvert.SerializeObject(new {status= report.Status.ToString() })),
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable
}
})
And here is the test case
[Fact]
public async Task Call_Health_Return_Healthy()
{
//Arrange
//Act
var response = await _factory.TestHttpClient
.GetAsync("/health");
//Assert
response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.OK);
}