Implicitly execute matched endpoint at the end of middleware pipeline (#1059)

This commit is contained in:
James Newton-King 2018-11-07 12:57:31 +13:00 committed by GitHub
parent d77b370fb1
commit 066c5ce337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions

View File

@ -81,6 +81,13 @@ namespace Microsoft.AspNetCore.Builder.Internal
{
RequestDelegate app = context =>
{
// Implicitly execute matched endpoint at the end of the pipeline instead of returning 404
var endpointRequestDelegate = context.GetEndpoint()?.RequestDelegate;
if (endpointRequestDelegate != null)
{
return endpointRequestDelegate(context);
}
context.Response.StatusCode = 404;
return Task.CompletedTask;
};

View File

@ -1,7 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit;
namespace Microsoft.AspNetCore.Builder.Internal
@ -20,6 +22,59 @@ namespace Microsoft.AspNetCore.Builder.Internal
Assert.Equal(404, httpContext.Response.StatusCode);
}
[Fact]
public void BuildImplicitlyCallsMatchedEndpointAsLastStep()
{
var builder = new ApplicationBuilder(null);
var app = builder.Build();
var endpointCalled = false;
var endpoint = new Endpoint(
context =>
{
endpointCalled = true;
return Task.CompletedTask;
},
EndpointMetadataCollection.Empty,
"Test endpoint");
var httpContext = new DefaultHttpContext();
httpContext.SetEndpoint(endpoint);
app.Invoke(httpContext);
Assert.True(endpointCalled);
}
[Fact]
public void BuildDoesNotCallMatchedEndpointWhenTerminated()
{
var builder = new ApplicationBuilder(null);
builder.Use((context, next) =>
{
// Do not call next
return Task.CompletedTask;
});
var app = builder.Build();
var endpointCalled = false;
var endpoint = new Endpoint(
context =>
{
endpointCalled = true;
return Task.CompletedTask;
},
EndpointMetadataCollection.Empty,
"Test endpoint");
var httpContext = new DefaultHttpContext();
httpContext.SetEndpoint(endpoint);
app.Invoke(httpContext);
Assert.False(endpointCalled);
}
[Fact]
public void PropertiesDictionaryIsDistinctAfterNew()
{