Skip to content

Commit

Permalink
Simulated health check (#116)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Pringle <[email protected]>
  • Loading branch information
EtherZa and Richard Pringle authored Feb 7, 2023
1 parent 12ae6af commit ce9ea55
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/AzureEventGridSimulator.Tests/IntegrationTests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
14 changes: 14 additions & 0 deletions src/AzureEventGridSimulator/Controllers/HealthController.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)) &&
Expand All @@ -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);
}
}

0 comments on commit ce9ea55

Please sign in to comment.