Skip to content

Commit 479d5ed

Browse files
author
N. Taylor Mullen
committed
Addressed code review comments.
1 parent 9a6881b commit 479d5ed

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/Http/Routing/src/EndpointRoutingMiddleware.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Task Invoke(HttpContext httpContext)
5252
{
5353
Log.MatchSkipped(_logger, endpoint);
5454

55-
// Someone else set the endpoint, we'll let them handle the unsetting.
55+
// Someone else set the endpoint, we'll let them handle the clearing of the endpoint.
5656
return _next(httpContext);
5757
}
5858

@@ -88,7 +88,6 @@ static async Task AwaitMatch(EndpointRoutingMiddleware middleware, HttpContext h
8888

8989
}
9090

91-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9291
private async Task SetRoutingAndContinue(HttpContext httpContext)
9392
{
9493
// If there was no mutation of the endpoint then log failure
@@ -115,8 +114,7 @@ private async Task SetRoutingAndContinue(HttpContext httpContext)
115114
}
116115
finally
117116
{
118-
// We unset the endpoint after calling through to the next middleware. This enables any future calls into
119-
// endpoint routing don't no-op from there already being an endpoint set.
117+
// This allows a second call in a single request (such as from the ErrorHandlerMiddleware) to perform routing again.
120118
httpContext.SetEndpoint(endpoint: null);
121119
}
122120
}

src/Http/Routing/test/UnitTests/EndpointRoutingMiddlewareTest.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,48 @@ namespace Microsoft.AspNetCore.Routing
2020
{
2121
public class EndpointRoutingMiddlewareTest
2222
{
23+
[Fact]
24+
public async Task Invoke_ChangedPath_ResultsInDifferentResult()
25+
{
26+
// Arrange
27+
var httpContext = CreateHttpContext();
28+
var matcher = new Mock<Matcher>();
29+
var pathToEndpoints = new Dictionary<string, Endpoint>()
30+
{
31+
["/initial"] = new Endpoint(c => Task.CompletedTask, new EndpointMetadataCollection(), "initialEndpoint"),
32+
["/changed"] = new Endpoint(c => Task.CompletedTask, new EndpointMetadataCollection(), "changedEndpoint")
33+
};
34+
matcher.Setup(m => m.MatchAsync(httpContext))
35+
.Callback<HttpContext>(context =>
36+
{
37+
var endpointToSet = pathToEndpoints[context.Request.Path];
38+
context.SetEndpoint(endpointToSet);
39+
})
40+
.Returns(Task.CompletedTask)
41+
.Verifiable();
42+
var matcherFactory = Mock.Of<MatcherFactory>(factory => factory.CreateMatcher(It.IsAny<EndpointDataSource>()) == matcher.Object);
43+
var middleware = CreateMiddleware(
44+
matcherFactory: matcherFactory,
45+
next: context =>
46+
{
47+
Assert.True(pathToEndpoints.TryGetValue(context.Request.Path, out var expectedEndpoint));
48+
49+
var currentEndpoint = context.GetEndpoint();
50+
Assert.Equal(expectedEndpoint, currentEndpoint);
51+
52+
return Task.CompletedTask;
53+
});
54+
55+
// Act
56+
httpContext.Request.Path = "/initial";
57+
await middleware.Invoke(httpContext);
58+
httpContext.Request.Path = "/changed";
59+
await middleware.Invoke(httpContext);
60+
61+
// Assert
62+
matcher.Verify();
63+
}
64+
2365
[Fact]
2466
public async Task Invoke_OnException_ResetsEndpoint()
2567
{

0 commit comments

Comments
 (0)