Skip to content
This repository has been archived by the owner on Dec 8, 2018. It is now read-only.

Commit

Permalink
Don't use Map
Browse files Browse the repository at this point in the history
Fixes #511 and #514

It's really confusing to people that we use Map. Users expect that the
URL they provide for the health check middleware will only process
exact matches. The way it behaves when using map is not optimal for some
of the common patterns.
  • Loading branch information
rynowak committed Oct 28, 2018
1 parent b3db95e commit c8c99a8
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public static class HealthCheckApplicationBuilderExtensions
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapExtensions.Map(IApplicationBuilder, PathString, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests. If <paramref name="path"/> is set to a non-empty
/// value, the health check middleware will process requests with a URL that matches the provided value
/// of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/') character.
/// </para>
/// <para>
/// The health check middleware will use default settings from <see cref="IOptions{HealthCheckOptions}"/>.
Expand All @@ -48,8 +50,10 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapExtensions.Map(IApplicationBuilder, PathString, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests. If <paramref name="path"/> is set to a non-empty
/// value, the health check middleware will process requests with a URL that matches the provided value
/// of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/') character.
/// </para>
/// </remarks>
public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app, PathString path, HealthCheckOptions options)
Expand Down Expand Up @@ -77,8 +81,11 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapWhenExtensions.MapWhen(IApplicationBuilder, Func{HttpContext, bool}, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path and port.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests on the specified port. If <paramref name="path"/> is
/// set to a non-empty value, the health check middleware will process requests with a URL that matches the
/// provided value of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/')
/// character.
/// </para>
/// <para>
/// The health check middleware will use default settings from <see cref="IOptions{HealthCheckOptions}"/>.
Expand All @@ -104,8 +111,11 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapWhenExtensions.MapWhen(IApplicationBuilder, Func{HttpContext, bool}, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path and port.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests on the specified port. If <paramref name="path"/> is
/// set to a non-empty value, the health check middleware will process requests with a URL that matches the
/// provided value of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/')
/// character.
/// </para>
/// <para>
/// The health check middleware will use default settings from <see cref="IOptions{HealthCheckOptions}"/>.
Expand Down Expand Up @@ -142,8 +152,11 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapExtensions.Map(IApplicationBuilder, PathString, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests on the specified port. If <paramref name="path"/> is
/// set to a non-empty value, the health check middleware will process requests with a URL that matches the
/// provided value of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/')
/// character.
/// </para>
/// </remarks>
public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app, PathString path, int port, HealthCheckOptions options)
Expand Down Expand Up @@ -172,8 +185,11 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,
/// <returns>A reference to the <paramref name="app"/> after the operation has completed.</returns>
/// <remarks>
/// <para>
/// This method will use <see cref="MapExtensions.Map(IApplicationBuilder, PathString, Action{IApplicationBuilder})"/> to
/// listen to health checks requests on the specified URL path.
/// If <paramref name="path"/> is set to <c>null</c> or the empty string then the health check middleware
/// will ignore the URL path and process all requests on the specified port. If <paramref name="path"/> is
/// set to a non-empty value, the health check middleware will process requests with a URL that matches the
/// provided value of <paramref name="path"/> case-insensitively, allowing for an extra trailing slash ('/')
/// character.
/// </para>
/// </remarks>
public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app, PathString path, string port, HealthCheckOptions options)
Expand Down Expand Up @@ -204,16 +220,35 @@ public static IApplicationBuilder UseHealthChecks(this IApplicationBuilder app,

private static void UseHealthChecksCore(IApplicationBuilder app, PathString path, int? port, object[] args)
{
if (port == null)
{
app.Map(path, b => b.UseMiddleware<HealthCheckMiddleware>(args));
}
else
// NOTE: we explicitly don't use Map here because it's really common for multiple health
// check middleware to overlap in paths. Ex: `/health`, `/health/detailed` - this is order
// sensititive with Map, and it's really surprising to people.
//
// See:
// https://github.com/aspnet/Diagnostics/issues/511
// https://github.com/aspnet/Diagnostics/issues/512
// https://github.com/aspnet/Diagnostics/issues/514

Func<HttpContext, bool> predicate = c =>
{
app.MapWhen(
c => c.Connection.LocalPort == port,
b0 => b0.Map(path, b1 => b1.UseMiddleware<HealthCheckMiddleware>(args)));
}
return

// Process the port if we have one
(port == null || c.Connection.LocalPort == port) &&

// We allow you to listen on all URLs by providing the empty PathString.
(!path.HasValue ||

// If you do provide a PathString, want to handle all of the special cases that
// StartsWithSegments handles, but we also want it to have exact match semantics.
//
// Ex: /Foo/ == /Foo (true)
// Ex: /Foo/Bar == /Foo (false)
(c.Request.Path.StartsWithSegments(path, out var remaining) &&
string.IsNullOrEmpty(remaining)));
};

app.MapWhen(predicate, b => b.UseMiddleware<HealthCheckMiddleware>(args));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,131 @@ public async Task CanListenWithoutPath_AcceptsRequest()
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task CanListenWithPath_AcceptsRequestWithExtraSlash()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseHealthChecks("/health");
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health/");

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task CanListenWithPath_AcceptsRequestWithCaseInsensitiveMatch()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseHealthChecks("/health");
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/HEALTH");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task CanListenWithPath_RejectsRequestWithExtraSegments()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseHealthChecks("/health");
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health/detailed");

Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

// See: https://github.com/aspnet/Diagnostics/issues/511
[Fact]
public async Task CanListenWithPath_MultipleMiddleware_LeastSpecificFirst()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
// Throws if used
app.UseHealthChecks("/health", new HealthCheckOptions()
{
ResponseWriter = (c, r) => throw null,
});

app.UseHealthChecks("/health/detailed");
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health/detailed");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

// See: https://github.com/aspnet/Diagnostics/issues/511
[Fact]
public async Task CanListenWithPath_MultipleMiddleware_MostSpecificFirst()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseHealthChecks("/health/detailed");

// Throws if used
app.UseHealthChecks("/health", new HealthCheckOptions()
{
ResponseWriter = (c, r) => throw null,
});
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health/detailed");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task CanListenOnPort_AcceptsRequest_OnSpecifiedPort()
{
Expand Down Expand Up @@ -486,6 +611,78 @@ public async Task CanListenOnPort_RejectsRequest_OnOtherPort()
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}

[Fact]
public async Task CanListenOnPort_MultipleMiddleware()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.Use(next => async (context) =>
{
// Need to fake setting the connection info. TestServer doesn't
// do that, because it doesn't have a connection.
context.Connection.LocalPort = context.Request.Host.Port.Value;
await next(context);
});

// Throws if used
app.UseHealthChecks("/health", port: 5001, new HealthCheckOptions()
{
ResponseWriter = (c, r) => throw null,
});

app.UseHealthChecks("/health/detailed", port: 5001);
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health/detailed");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}

[Fact]
public async Task CanListenOnPort_MultipleMiddleware_DifferentPorts()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.Use(next => async (context) =>
{
// Need to fake setting the connection info. TestServer doesn't
// do that, because it doesn't have a connection.
context.Connection.LocalPort = context.Request.Host.Port.Value;
await next(context);
});

// Throws if used
app.UseHealthChecks("/health", port: 5002, new HealthCheckOptions()
{
ResponseWriter = (c, r) => throw null,
});

app.UseHealthChecks("/health", port: 5001);
})
.ConfigureServices(services =>
{
services.AddHealthChecks();
});

var server = new TestServer(builder);
var client = server.CreateClient();

var response = await client.GetAsync("http://localhost:5001/health");

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("text/plain", response.Content.Headers.ContentType.ToString());
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
}
}
}

0 comments on commit c8c99a8

Please sign in to comment.