Add "/__admin/health" endpoint (#1112)

This commit is contained in:
Stef Heyenrath
2024-06-03 10:59:44 +02:00
committed by GitHub
parent f76ea1d8ec
commit 17f5ab5145
5 changed files with 50 additions and 6 deletions

View File

@@ -24,12 +24,24 @@ public interface IWireMockAdminApi
[Header("Authorization")]
AuthenticationHeaderValue Authorization { get; set; }
/// <summary>
/// Get health status.
/// </summary>
/// <param name="cancellationToken">The optional cancellationToken.</param>
/// <returns>
/// Returns HttpStatusCode <c>200</c> with a value <c>Healthy</c> to indicate that WireMock.Net is healthy.
/// Else it returns HttpStatusCode <c>404</c>.
/// </returns>
[Get("health")]
[AllowAnyStatusCode]
Task<string> GetHealthAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Get the settings.
/// </summary>
/// <returns>SettingsModel</returns>
[Get("settings")]
Task<SettingsModel> GetSettingsAsync();
Task<SettingsModel> GetSettingsAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Update the settings.

View File

@@ -31,6 +31,7 @@ public partial class WireMockServer
{
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
private const string AdminFiles = "/__admin/files";
private const string AdminHealth = "/__admin/health";
private const string AdminMappings = "/__admin/mappings";
private const string AdminMappingsCode = "/__admin/mappings/code";
private const string AdminMappingsWireMockOrg = "/__admin/mappings/wiremock.org";
@@ -54,6 +55,9 @@ public partial class WireMockServer
#region InitAdmin
private void InitAdmin()
{
// __admin/health
Given(Request.Create().WithPath(AdminHealth).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(HealthGet));
// __admin/settings
Given(Request.Create().WithPath(AdminSettings).UsingGet()).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(SettingsGet));
Given(Request.Create().WithPath(AdminSettings).UsingMethod("PUT", "POST").WithHeader(HttpKnownHeaderNames.ContentType, AdminRequestContentTypeJson)).AtPriority(WireMockConstants.AdminPriority).RespondWith(new DynamicResponseProvider(SettingsUpdate));
@@ -218,6 +222,22 @@ public partial class WireMockServer
}
#endregion
#region Health
private static IResponseMessage HealthGet(IRequestMessage requestMessage)
{
return new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = "Healthy"
},
StatusCode = (int)HttpStatusCode.OK,
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>(WireMockConstants.ContentTypeTextPlain) } }
};
}
#endregion
#region Settings
private IResponseMessage SettingsGet(IRequestMessage requestMessage)
{
@@ -830,4 +850,4 @@ public partial class WireMockServer
var singleResult = ((JObject)value).ToObject<T>();
return new[] { singleResult! };
}
}
}

View File

@@ -41,6 +41,18 @@ public partial class WireMockAdminApiTests
VerifyNewtonsoftJson.Enable(VerifySettings);
}
[Fact]
public async Task IWireMockAdminApi_GetHealthAsync()
{
// Arrange
var server = WireMockServer.StartWithAdminInterface();
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
// Act
var status = await api.GetHealthAsync().ConfigureAwait(false);
status.Should().Be("Healthy");
}
[Fact]
public async Task IWireMockAdminApi_GetSettingsAsync()
{

View File

@@ -117,7 +117,7 @@ public class WireMockServerProxyTests
}
// Assert
server.Mappings.Should().HaveCount(35);
server.Mappings.Should().HaveCount(36);
}
[Fact]

View File

@@ -81,7 +81,7 @@ public class WireMockServerSettingsTests
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(33);
server.Mappings.Should().HaveCount(34);
server.Mappings.All(m => m.Priority == WireMockConstants.AdminPriority).Should().BeTrue();
}
@@ -100,9 +100,9 @@ public class WireMockServerSettingsTests
// Assert
server.Mappings.Should().NotBeNull();
server.Mappings.Should().HaveCount(34);
server.Mappings.Should().HaveCount(35);
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(33);
server.Mappings.Count(m => m.Priority == WireMockConstants.AdminPriority).Should().Be(34);
server.Mappings.Count(m => m.Priority == WireMockConstants.ProxyPriority).Should().Be(1);
}