From ce9ea55b90f0d2c3306e5f06b51bd75802a5cd4c Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Tue, 7 Feb 2023 18:50:05 +0800 Subject: [PATCH] Simulated health check (#116) Co-authored-by: Richard Pringle --- .../IntegrationTests/BasicTests.cs | 17 +++++++++++++++++ .../Controllers/HealthController.cs | 14 ++++++++++++++ .../Middleware/EventGridMiddleware.cs | 17 +++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/AzureEventGridSimulator/Controllers/HealthController.cs diff --git a/src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs b/src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs index 4e99f12..5354875 100644 --- a/src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs +++ b/src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs @@ -51,4 +51,21 @@ public async Task GivenAValidEvent_WhenPublished_ThenItShouldBeAccepted() response.EnsureSuccessStatusCode(); response.StatusCode.ShouldBe(HttpStatusCode.OK); } + + [Fact] + public async Task GivenAHealthRequest_ThenItShouldRespondWithOk() + { + // Arrange + var client = _factory.CreateClient(new WebApplicationFactoryClientOptions + { + BaseAddress = new Uri("https://localhost:60101") + }); + + // Act + var response = await client.GetAsync("/api/health"); + + // Assert + response.StatusCode.ShouldBe(HttpStatusCode.OK); + (await response.Content.ReadAsStringAsync()).ShouldBe("OK"); + } } diff --git a/src/AzureEventGridSimulator/Controllers/HealthController.cs b/src/AzureEventGridSimulator/Controllers/HealthController.cs new file mode 100644 index 0000000..8e9909f --- /dev/null +++ b/src/AzureEventGridSimulator/Controllers/HealthController.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Mvc; + +namespace AzureEventGridSimulator.Controllers; + +[Route("/api/health")] +[ApiController] +public class HealthController : ControllerBase +{ + [HttpGet] + public IActionResult Get() + { + return Ok("OK"); + } +} diff --git a/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs b/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs index 839d7a0..d1efea6 100644 --- a/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs +++ b/src/AzureEventGridSimulator/Infrastructure/Middleware/EventGridMiddleware.cs @@ -38,6 +38,12 @@ public async Task InvokeAsync(HttpContext context, return; } + if (IsHealthRequest(context)) + { + await ValidateHealthRequest(context); + return; + } + // This is the end of the line. await context.WriteErrorResponse(HttpStatusCode.BadRequest, "Request not supported.", null); } @@ -129,6 +135,11 @@ private async Task ValidateNotificationRequest(HttpContext context, await _next(context); } + private async Task ValidateHealthRequest(HttpContext context) + { + await _next(context); + } + private static bool IsNotificationRequest(HttpContext context) { return context.Request.Headers.Keys.Any(k => string.Equals(k, "Content-Type", StringComparison.OrdinalIgnoreCase)) && @@ -144,4 +155,10 @@ private static bool IsValidationRequest(HttpContext context) context.Request.Query.Keys.Any(k => string.Equals(k, "id", StringComparison.OrdinalIgnoreCase)) && Guid.TryParse(context.Request.Query["id"], out _); } + + private static bool IsHealthRequest(HttpContext context) + { + return context.Request.Method == HttpMethods.Get && + string.Equals(context.Request.Path, "/api/health", StringComparison.OrdinalIgnoreCase); + } }