Fix #468 - all UseHealthChecks without a path

This commit is contained in:
Ryan Nowak 2018-08-30 15:29:54 -07:00
Родитель 3e4a3d0b90
Коммит 525fbf495b
2 изменённых файлов: 58 добавлений и 32 удалений

Просмотреть файл

@ -35,11 +35,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
UseHealthChecksCore(app, path, port: null, Array.Empty<object>()); UseHealthChecksCore(app, path, port: null, Array.Empty<object>());
return app; return app;
} }
@ -64,11 +59,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
if (options == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
@ -101,11 +91,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
UseHealthChecksCore(app, path, port, Array.Empty<object>()); UseHealthChecksCore(app, path, port, Array.Empty<object>());
return app; return app;
} }
@ -133,11 +118,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
if (port == null) if (port == null)
{ {
throw new ArgumentNullException(nameof(port)); throw new ArgumentNullException(nameof(port));
@ -173,11 +153,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
if (options == null) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
@ -208,11 +183,6 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(app)); throw new ArgumentNullException(nameof(app));
} }
if (!path.HasValue)
{
throw new ArgumentException("A URL path must be provided", nameof(path));
}
if (path == null) if (path == null)
{ {
throw new ArgumentNullException(nameof(port)); throw new ArgumentNullException(nameof(port));
@ -241,8 +211,8 @@ namespace Microsoft.AspNetCore.Builder
else else
{ {
app.MapWhen( app.MapWhen(
c => c.Connection.LocalPort == port && c.Request.Path.StartsWithSegments(path), c => c.Connection.LocalPort == port,
b => b.UseMiddleware<HealthCheckMiddleware>(args)); b0 => b0.Map(path, b1 => b1.UseMiddleware<HealthCheckMiddleware>(args)));
} }
} }
} }

Просмотреть файл

@ -323,6 +323,29 @@ namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync()); Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
} }
[Fact]
public async Task CanListenWithoutPath_AcceptsRequest()
{
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseHealthChecks(default);
})
.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] [Fact]
public async Task CanListenOnPort_AcceptsRequest_OnSpecifiedPort() public async Task CanListenOnPort_AcceptsRequest_OnSpecifiedPort()
{ {
@ -354,6 +377,37 @@ namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
Assert.Equal("Healthy", await response.Content.ReadAsStringAsync()); Assert.Equal("Healthy", await response.Content.ReadAsStringAsync());
} }
[Fact]
public async Task CanListenOnPortWithoutPath_AcceptsRequest_OnSpecifiedPort()
{
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);
});
app.UseHealthChecks(default, 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());
}
[Fact] [Fact]
public async Task CanListenOnPort_RejectsRequest_OnOtherPort() public async Task CanListenOnPort_RejectsRequest_OnOtherPort()
{ {
@ -382,5 +436,7 @@ namespace Microsoft.AspNetCore.Diagnostics.HealthChecks
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
} }
} }
} }