mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-02-23 18:54:51 +01:00
* Add WireMockHealthCheck For use with Aspire, to make WaitFor(wiremock) more useful. Calls /__admin/health and checks the result, as well as checks if mappings using AdminApiMappingBuilder has been submitted to the server. This created a catch-22 problem where the mappings were not submitted until the health check was healthy, but the health check was not healthy until the mappings were submitted. To avoid this, the WireMockServerLifecycleHook class has been slightly re-arranged, and is now using the AfterEndpointsAllocatedAsync callback rather than the AfterResourcesCreatedAsync callback. Within which a separate Task is created that waits until the server is ready and submits the mappings. * Move WireMockMappingState to its own file * Dispose the cancellation tokens in WireMockServerLifecycleHook
45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using Aspire.Hosting.ApplicationModel;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
using WireMock.Client;
|
|
|
|
namespace WireMock.Net.Aspire;
|
|
|
|
/// <summary>
|
|
/// WireMockHealthCheck
|
|
/// </summary>
|
|
public class WireMockHealthCheck(WireMockServerResource resource) : IHealthCheck
|
|
{
|
|
private const string HealthStatusHealthy = "Healthy";
|
|
|
|
/// <inheritdoc />
|
|
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
|
{
|
|
if (!await IsHealthyAsync(resource.AdminApi.Value, cancellationToken))
|
|
{
|
|
return HealthCheckResult.Unhealthy("WireMock.Net is not healthy");
|
|
}
|
|
|
|
if (resource.ApiMappingState == WireMockMappingState.NotSubmitted)
|
|
{
|
|
return HealthCheckResult.Unhealthy("WireMock.Net has not received mappings");
|
|
}
|
|
|
|
return HealthCheckResult.Healthy();
|
|
}
|
|
|
|
private static async Task<bool> IsHealthyAsync(IWireMockAdminApi adminApi, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var status = await adminApi.GetHealthAsync(cancellationToken);
|
|
return string.Equals(status, HealthStatusHealthy, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|