Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Shtock authored and Simon Shtock committed Jul 28, 2024
1 parent e4a2b70 commit e5863c8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/HealthChecks.UI/Extensions/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HealthChecks.UI.Configuration;
using HealthChecks.UI.Core;
using HealthChecks.UI.Middleware;
using Microsoft.AspNetCore.Routing;

namespace Microsoft.AspNetCore.Builder;

Expand All @@ -22,7 +23,11 @@ public static IApplicationBuilder UseHealthChecksUI(this IApplicationBuilder app

private static IApplicationBuilder ConfigurePipeline(IApplicationBuilder app, Options options)
{
EnsureValidApiOptions(options);
EnsureValidApiOptions(options,
(app.ApplicationServices.GetService(typeof(IEnumerable<EndpointDataSource>)) as IEnumerable<EndpointDataSource>)
?.SelectMany(dataSource => dataSource.Endpoints)
?.OfType<RouteEndpoint>());


var embeddedResourcesAssembly = typeof(UIResource).Assembly;

Expand All @@ -44,7 +49,7 @@ private static IApplicationBuilder ConfigurePipeline(IApplicationBuilder app, Op
return app;
}

private static void EnsureValidApiOptions(Options options)
private static void EnsureValidApiOptions(Options options, IEnumerable<RouteEndpoint>? routeEndpoints)
{
Action<string, string> ensureValidPath = (string path, string argument) =>
{
Expand All @@ -57,5 +62,15 @@ private static void EnsureValidApiOptions(Options options)
ensureValidPath(options.ApiPath, nameof(Options.ApiPath));
ensureValidPath(options.UIPath, nameof(Options.UIPath));
ensureValidPath(options.WebhookPath, nameof(Options.WebhookPath));

Func<string, string> normalizeUriPath = (string path) =>
path.TrimEnd('/').ToLower();

if (routeEndpoints
?.Select(endPoint => normalizeUriPath(endPoint.RoutePattern.RawText ?? string.Empty))
?.Count(path => path == normalizeUriPath(options.ApiPath)) > 0)
{
throw new ArgumentException("ApiPath should not match any route registered via MapHealthChecks!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,32 @@ public async Task support_configuring_page_title()

html.ShouldContain($"<title>{pageTitle}</title>");
}

[Theory]
[InlineData("/healthz")]
[InlineData("/healthz/")]
[InlineData("/Healthz")]
public void throws_when_api_path_mounted_on_existing_health_check(string endPoint)
{
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services
.AddRouting()
.AddHealthChecksUI()
.AddInMemoryStorage()
.Services
.AddHealthChecks();
})
.Configure(app =>
{
app
.UseRouting()
.UseEndpoints(setup => setup.MapHealthChecks(endPoint, new HealthCheckOptions()))
.UseHealthChecksUI(setup => setup.ApiPath = "/healthz");
});

var exception = Assert.Throws<ArgumentException>(() => new TestServer(builder));
Assert.Equal("ApiPath should not match any route registered via MapHealthChecks!", exception.Message);
}
}

0 comments on commit e5863c8

Please sign in to comment.