Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "e.map is not a function" JS exception in the UI #2269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading