From 0a1d68b8f375eb979c5722b8bedd57496749b1f8 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 7 May 2019 20:40:41 -0700 Subject: [PATCH] Remove EndpointSelectorContext --- .../Routing/EndpointHttpContextExtensions.cs | 2 +- .../EndpointHttpContextExtensionsTests.cs | 8 +- .../EndpointMetadataCollectionBenchmark.cs | 2 +- .../perf/Matching/MatcherAzureBenchmark.cs | 16 +- .../MatcherBuilderMultipleEntryBenchmark.cs | 4 +- .../perf/Matching/MatcherGithubBenchmark.cs | 17 +- .../Matching/MatcherSingleEntryBenchmark.cs | 21 +- .../Routing/perf/Matching/TrivialMatcher.cs | 7 +- ...rosoft.AspNetCore.Routing.netcoreapp3.0.cs | 16 +- .../Routing/src/EndpointRoutingMiddleware.cs | 41 ++-- .../Routing/src/EndpointSelectorContext.cs | 51 ----- .../Matching/DataSourceDependentMatcher.cs | 4 +- .../src/Matching/DefaultEndpointSelector.cs | 22 +-- src/Http/Routing/src/Matching/DfaMatcher.cs | 20 +- .../Routing/src/Matching/EndpointSelector.cs | 15 +- .../Routing/src/Matching/HostMatcherPolicy.cs | 2 +- .../src/Matching/HttpMethodMatcherPolicy.cs | 9 +- .../src/Matching/IEndpointSelectorPolicy.cs | 12 +- src/Http/Routing/src/Matching/Matcher.cs | 7 +- ...RoutingApplicationBuilderExtensionsTest.cs | 2 +- .../UnitTests/EndpointSelectorContextTest.cs | 53 ------ .../UnitTests/Matching/BarebonesMatcher.cs | 13 +- .../DataSourceDependentMatcherTest.cs | 2 +- .../Matching/DefaultEndpointSelectorTest.cs | 48 ++--- .../Matching/DfaMatcherConformanceTest.cs | 8 +- .../test/UnitTests/Matching/DfaMatcherTest.cs | 179 +++++++++--------- .../FullFeaturedMatcherConformanceTest.cs | 100 +++++----- .../HostMatcherPolicyIntegrationTestBase.cs | 100 +++++----- ...pMethodMatcherPolicyIntegrationTestBase.cs | 123 ++++++------ .../test/UnitTests/Matching/MatcherAssert.cs | 32 ++-- .../MatcherConformanceTest.SingleEndpoint.cs | 104 +++++----- .../Matching/MatcherConformanceTest.cs | 4 +- .../test/UnitTests/Matching/RouteMatcher.cs | 4 +- .../UnitTests/Matching/RouteMatcherBuilder.cs | 9 +- .../UnitTests/Matching/TreeRouterMatcher.cs | 4 +- .../Matching/TreeRouterMatcherBuilder.cs | 9 +- .../test/UnitTests/TestObjects/TestMatcher.cs | 9 +- .../Routing/ActionConstraintMatcherPolicy.cs | 4 +- .../src/Routing/ConsumesMatcherPolicy.cs | 9 +- .../DynamicControllerEndpointMatcherPolicy.cs | 6 +- .../test/Routing/ConsumesMatcherPolicyTest.cs | 40 ++-- .../DynamicPageEndpointMatcherPolicy.cs | 6 +- .../Infrastructure/PageLoaderMatcherPolicy.cs | 2 +- .../PageLoaderMatcherPolicyTest.cs | 4 +- .../ActionConstraintMatcherPolicyTest.cs | 18 +- 45 files changed, 514 insertions(+), 654 deletions(-) delete mode 100644 src/Http/Routing/src/EndpointSelectorContext.cs delete mode 100644 src/Http/Routing/test/UnitTests/EndpointSelectorContextTest.cs diff --git a/src/Http/Http.Abstractions/src/Routing/EndpointHttpContextExtensions.cs b/src/Http/Http.Abstractions/src/Routing/EndpointHttpContextExtensions.cs index 642af5f4d5..c7ec9618b3 100644 --- a/src/Http/Http.Abstractions/src/Routing/EndpointHttpContextExtensions.cs +++ b/src/Http/Http.Abstractions/src/Routing/EndpointHttpContextExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; diff --git a/src/Http/Http.Abstractions/test/EndpointHttpContextExtensionsTests.cs b/src/Http/Http.Abstractions/test/EndpointHttpContextExtensionsTests.cs index b65af91f4f..0e297c8041 100644 --- a/src/Http/Http.Abstractions/test/EndpointHttpContextExtensionsTests.cs +++ b/src/Http/Http.Abstractions/test/EndpointHttpContextExtensionsTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -87,7 +87,7 @@ namespace Microsoft.AspNetCore.Http.Abstractions.Tests // Assert var feature = context.Features.Get(); Assert.NotNull(feature); - Assert.Equal(endpoint, feature.Endpoint); + Assert.Equal(endpoint, context.GetEndpoint()); } [Fact] @@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Http.Abstractions.Tests // Assert var feature = context.Features.Get(); Assert.Equal(initialFeature, feature); - Assert.Equal(endpoint, feature.Endpoint); + Assert.Equal(endpoint, context.GetEndpoint()); } [Fact] @@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.Http.Abstractions.Tests // Assert var feature = context.Features.Get(); Assert.Equal(initialFeature, feature); - Assert.Null(feature.Endpoint); + Assert.Null(context.GetEndpoint()); } [Fact] diff --git a/src/Http/Routing/perf/EndpointMetadataCollectionBenchmark.cs b/src/Http/Routing/perf/EndpointMetadataCollectionBenchmark.cs index 3e5492b8a2..015fa5d0d6 100644 --- a/src/Http/Routing/perf/EndpointMetadataCollectionBenchmark.cs +++ b/src/Http/Routing/perf/EndpointMetadataCollectionBenchmark.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; diff --git a/src/Http/Routing/perf/Matching/MatcherAzureBenchmark.cs b/src/Http/Routing/perf/Matching/MatcherAzureBenchmark.cs index 5f437fad33..e3ee56d521 100644 --- a/src/Http/Routing/perf/Matching/MatcherAzureBenchmark.cs +++ b/src/Http/Routing/perf/Matching/MatcherAzureBenchmark.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using BenchmarkDotNet.Attributes; +using Microsoft.AspNetCore.Http.Endpoints; namespace Microsoft.AspNetCore.Routing.Matching { @@ -15,7 +16,6 @@ namespace Microsoft.AspNetCore.Routing.Matching private Matcher _dfa; private int[] _samples; - private EndpointSelectorContext _feature; [GlobalSetup] public void Setup() @@ -30,34 +30,30 @@ namespace Microsoft.AspNetCore.Routing.Matching _baseline = (BarebonesMatcher)SetupMatcher(new BarebonesMatcherBuilder()); _dfa = SetupMatcher(CreateDfaMatcherBuilder()); - - _feature = new EndpointSelectorContext(); } [Benchmark(Baseline = true, OperationsPerInvoke = SampleCount)] public async Task Baseline() { - var feature = _feature; for (var i = 0; i < SampleCount; i++) { var sample = _samples[i]; var httpContext = Requests[sample]; - await _baseline.Matchers[sample].MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[sample], feature.Endpoint); + await _baseline.Matchers[sample].MatchAsync(httpContext); + Validate(httpContext, Endpoints[sample], httpContext.GetEndpoint()); } } [Benchmark(OperationsPerInvoke = SampleCount)] public async Task Dfa() { - var feature = _feature; for (var i = 0; i < SampleCount; i++) { var sample = _samples[i]; var httpContext = Requests[sample]; - await _dfa.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[sample], feature.Endpoint); + await _dfa.MatchAsync(httpContext); + Validate(httpContext, Endpoints[sample], httpContext.GetEndpoint()); } } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/perf/Matching/MatcherBuilderMultipleEntryBenchmark.cs b/src/Http/Routing/perf/Matching/MatcherBuilderMultipleEntryBenchmark.cs index 23f1bf0729..7116b43fc1 100644 --- a/src/Http/Routing/perf/Matching/MatcherBuilderMultipleEntryBenchmark.cs +++ b/src/Http/Routing/perf/Matching/MatcherBuilderMultipleEntryBenchmark.cs @@ -162,7 +162,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return false; } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { throw new NotImplementedException(); } @@ -179,7 +179,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return false; } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { throw new NotImplementedException(); } diff --git a/src/Http/Routing/perf/Matching/MatcherGithubBenchmark.cs b/src/Http/Routing/perf/Matching/MatcherGithubBenchmark.cs index 059e97239c..35bf41521e 100644 --- a/src/Http/Routing/perf/Matching/MatcherGithubBenchmark.cs +++ b/src/Http/Routing/perf/Matching/MatcherGithubBenchmark.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using BenchmarkDotNet.Attributes; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Routing.Matching @@ -14,8 +15,6 @@ namespace Microsoft.AspNetCore.Routing.Matching private BarebonesMatcher _baseline; private Matcher _dfa; - private EndpointSelectorContext _feature; - [GlobalSetup] public void Setup() { @@ -25,32 +24,28 @@ namespace Microsoft.AspNetCore.Routing.Matching _baseline = (BarebonesMatcher)SetupMatcher(new BarebonesMatcherBuilder()); _dfa = SetupMatcher(CreateDfaMatcherBuilder()); - - _feature = new EndpointSelectorContext(); } [Benchmark(Baseline = true, OperationsPerInvoke = EndpointCount)] public async Task Baseline() { - var feature = _feature; for (var i = 0; i < EndpointCount; i++) { var httpContext = Requests[i]; - await _baseline.Matchers[i].MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[i], feature.Endpoint); + await _baseline.Matchers[i].MatchAsync(httpContext); + Validate(httpContext, Endpoints[i], httpContext.GetEndpoint()); } } [Benchmark( OperationsPerInvoke = EndpointCount)] public async Task Dfa() { - var feature = _feature; for (var i = 0; i < EndpointCount; i++) { var httpContext = Requests[i]; - await _dfa.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[i], feature.Endpoint); + await _dfa.MatchAsync(httpContext); + Validate(httpContext, Endpoints[i], httpContext.GetEndpoint()); } } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/perf/Matching/MatcherSingleEntryBenchmark.cs b/src/Http/Routing/perf/Matching/MatcherSingleEntryBenchmark.cs index 7e8285cade..0991ad48b0 100644 --- a/src/Http/Routing/perf/Matching/MatcherSingleEntryBenchmark.cs +++ b/src/Http/Routing/perf/Matching/MatcherSingleEntryBenchmark.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using BenchmarkDotNet.Attributes; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Routing.Matching @@ -43,40 +44,36 @@ namespace Microsoft.AspNetCore.Routing.Matching public async Task Baseline() { var httpContext = Requests[0]; - var feature = new EndpointSelectorContext(httpContext); - await _baseline.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[0], feature.Endpoint); + await _baseline.MatchAsync(httpContext); + Validate(httpContext, Endpoints[0], httpContext.GetEndpoint()); } [Benchmark] public async Task Dfa() { var httpContext = Requests[0]; - var feature = new EndpointSelectorContext(httpContext); - await _dfa.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[0], feature.Endpoint); + await _dfa.MatchAsync(httpContext); + Validate(httpContext, Endpoints[0], httpContext.GetEndpoint()); } [Benchmark] public async Task LegacyTreeRouter() { var httpContext = Requests[0]; - var feature = new EndpointSelectorContext(httpContext); - await _tree.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[0], feature.Endpoint); + await _tree.MatchAsync(httpContext); + Validate(httpContext, Endpoints[0], httpContext.GetEndpoint()); } [Benchmark] public async Task LegacyRouter() { var httpContext = Requests[0]; - var feature = new EndpointSelectorContext(httpContext); - await _route.MatchAsync(httpContext, feature); - Validate(httpContext, Endpoints[0], feature.Endpoint); + await _route.MatchAsync(httpContext); + Validate(httpContext, Endpoints[0], httpContext.GetEndpoint()); } } } diff --git a/src/Http/Routing/perf/Matching/TrivialMatcher.cs b/src/Http/Routing/perf/Matching/TrivialMatcher.cs index 2c4a5fd173..5d5714b52b 100644 --- a/src/Http/Routing/perf/Matching/TrivialMatcher.cs +++ b/src/Http/Routing/perf/Matching/TrivialMatcher.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Routing.Matching @@ -23,7 +24,7 @@ namespace Microsoft.AspNetCore.Routing.Matching _candidates = new Candidate[] { new Candidate(endpoint), }; } - public sealed override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public sealed override Task MatchAsync(HttpContext httpContext) { if (httpContext == null) { @@ -33,8 +34,8 @@ namespace Microsoft.AspNetCore.Routing.Matching var path = httpContext.Request.Path.Value; if (string.Equals(_endpoint.RoutePattern.RawText, path, StringComparison.OrdinalIgnoreCase)) { - context.Endpoint = _endpoint; - context.RouteValues = new RouteValueDictionary(); + httpContext.SetEndpoint(_endpoint); + httpContext.Request.RouteValues = new RouteValueDictionary(); } return Task.CompletedTask; diff --git a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs index fc0a07386f..9ca47cf13e 100644 --- a/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs +++ b/src/Http/Routing/ref/Microsoft.AspNetCore.Routing.netcoreapp3.0.cs @@ -89,14 +89,6 @@ namespace Microsoft.AspNetCore.Routing public EndpointNameMetadata(string endpointName) { } public string EndpointName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } } - [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] - public readonly partial struct EndpointSelectorContext - { - private readonly object _dummy; - public EndpointSelectorContext(Microsoft.AspNetCore.Http.HttpContext httpContext) { throw null; } - public Microsoft.AspNetCore.Http.Endpoint Endpoint { get { throw null; } set { } } - public Microsoft.AspNetCore.Routing.RouteValueDictionary RouteValues { get { throw null; } set { } } - } [System.AttributeUsageAttribute(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=false, Inherited=false)] [System.Diagnostics.DebuggerDisplayAttribute("{DebuggerToString(),nq}")] public sealed partial class HostAttribute : System.Attribute, Microsoft.AspNetCore.Routing.IHostMetadata @@ -572,14 +564,14 @@ namespace Microsoft.AspNetCore.Routing.Matching public abstract partial class EndpointSelector { protected EndpointSelector() { } - public abstract System.Threading.Tasks.Task SelectAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.EndpointSelectorContext context, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates); + public abstract System.Threading.Tasks.Task SelectAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates); } public sealed partial class HostMatcherPolicy : Microsoft.AspNetCore.Routing.MatcherPolicy, Microsoft.AspNetCore.Routing.Matching.IEndpointComparerPolicy, Microsoft.AspNetCore.Routing.Matching.IEndpointSelectorPolicy, Microsoft.AspNetCore.Routing.Matching.INodeBuilderPolicy { public HostMatcherPolicy() { } public System.Collections.Generic.IComparer Comparer { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } public override int Order { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } } - public System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.EndpointSelectorContext context, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates) { throw null; } + public System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates) { throw null; } public Microsoft.AspNetCore.Routing.Matching.PolicyJumpTable BuildJumpTable(int exitDestination, System.Collections.Generic.IReadOnlyList edges) { throw null; } public System.Collections.Generic.IReadOnlyList GetEdges(System.Collections.Generic.IReadOnlyList endpoints) { throw null; } bool Microsoft.AspNetCore.Routing.Matching.IEndpointSelectorPolicy.AppliesToEndpoints(System.Collections.Generic.IReadOnlyList endpoints) { throw null; } @@ -590,7 +582,7 @@ namespace Microsoft.AspNetCore.Routing.Matching public HttpMethodMatcherPolicy() { } public System.Collections.Generic.IComparer Comparer { get { throw null; } } public override int Order { get { throw null; } } - public System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.EndpointSelectorContext context, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates) { throw null; } + public System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates) { throw null; } public Microsoft.AspNetCore.Routing.Matching.PolicyJumpTable BuildJumpTable(int exitDestination, System.Collections.Generic.IReadOnlyList edges) { throw null; } public System.Collections.Generic.IReadOnlyList GetEdges(System.Collections.Generic.IReadOnlyList endpoints) { throw null; } bool Microsoft.AspNetCore.Routing.Matching.IEndpointSelectorPolicy.AppliesToEndpoints(System.Collections.Generic.IReadOnlyList endpoints) { throw null; } @@ -603,7 +595,7 @@ namespace Microsoft.AspNetCore.Routing.Matching public partial interface IEndpointSelectorPolicy { bool AppliesToEndpoints(System.Collections.Generic.IReadOnlyList endpoints); - System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.EndpointSelectorContext context, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates); + System.Threading.Tasks.Task ApplyAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Routing.Matching.CandidateSet candidates); } public partial interface INodeBuilderPolicy { diff --git a/src/Http/Routing/src/EndpointRoutingMiddleware.cs b/src/Http/Routing/src/EndpointRoutingMiddleware.cs index 93b0f94d08..eb42e484c1 100644 --- a/src/Http/Routing/src/EndpointRoutingMiddleware.cs +++ b/src/Http/Routing/src/EndpointRoutingMiddleware.cs @@ -6,6 +6,7 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Routing.Matching; using Microsoft.Extensions.Logging; @@ -55,12 +56,11 @@ namespace Microsoft.AspNetCore.Routing public Task Invoke(HttpContext httpContext) { - var feature = new EndpointSelectorContext(httpContext); - // There's already an endpoint, skip maching completely - if (feature.Endpoint != null) + var endpoint = httpContext.GetEndpoint(); + if (endpoint != null) { - Log.MatchSkipped(_logger, feature.Endpoint); + Log.MatchSkipped(_logger, endpoint); return _next(httpContext); } @@ -69,44 +69,45 @@ namespace Microsoft.AspNetCore.Routing var matcherTask = InitializeAsync(); if (!matcherTask.IsCompletedSuccessfully) { - return AwaitMatcher(this, httpContext, feature, matcherTask); + return AwaitMatcher(this, httpContext, matcherTask); } - var matchTask = matcherTask.Result.MatchAsync(httpContext, feature); + var matchTask = matcherTask.Result.MatchAsync(httpContext); if (!matchTask.IsCompletedSuccessfully) { - return AwaitMatch(this, httpContext, feature, matchTask); + return AwaitMatch(this, httpContext, matchTask); } - return SetRoutingAndContinue(httpContext, feature); + return SetRoutingAndContinue(httpContext); // Awaited fallbacks for when the Tasks do not synchronously complete - static async Task AwaitMatcher(EndpointRoutingMiddleware middleware, HttpContext httpContext, EndpointSelectorContext feature, Task matcherTask) + static async Task AwaitMatcher(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matcherTask) { var matcher = await matcherTask; - await matcher.MatchAsync(httpContext, feature); - await middleware.SetRoutingAndContinue(httpContext, feature); + await matcher.MatchAsync(httpContext); + await middleware.SetRoutingAndContinue(httpContext); } - static async Task AwaitMatch(EndpointRoutingMiddleware middleware, HttpContext httpContext, EndpointSelectorContext feature, Task matchTask) + static async Task AwaitMatch(EndpointRoutingMiddleware middleware, HttpContext httpContext, Task matchTask) { await matchTask; - await middleware.SetRoutingAndContinue(httpContext, feature); + await middleware.SetRoutingAndContinue(httpContext); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private Task SetRoutingAndContinue(HttpContext httpContext, EndpointSelectorContext feature) + private Task SetRoutingAndContinue(HttpContext httpContext) { // If there was no mutation of the endpoint then log failure - if (feature.Endpoint is null) + var endpoint = httpContext.GetEndpoint(); + if (endpoint == null) { Log.MatchFailure(_logger); } else { - Log.MatchSuccess(_logger, feature); + Log.MatchSuccess(_logger, endpoint); } return _next(httpContext); @@ -115,8 +116,8 @@ namespace Microsoft.AspNetCore.Routing // Initialization is async to avoid blocking threads while reflection and things // of that nature take place. // - // We've seen cases where startup is very slow if we allow multiple threads to race - // while initializing the set of endpoints/routes. Doing CPU intensive work is a + // We've seen cases where startup is very slow if we allow multiple threads to race + // while initializing the set of endpoints/routes. Doing CPU intensive work is a // blocking operation if you have a low core count and enough work to do. private Task InitializeAsync() { @@ -184,9 +185,9 @@ namespace Microsoft.AspNetCore.Routing new EventId(3, "MatchingSkipped"), "Endpoint '{EndpointName}' already set, skipping route matching."); - public static void MatchSuccess(ILogger logger, EndpointSelectorContext context) + public static void MatchSuccess(ILogger logger, Endpoint endpoint) { - _matchSuccess(logger, context.Endpoint.DisplayName, null); + _matchSuccess(logger, endpoint.DisplayName, null); } public static void MatchFailure(ILogger logger) diff --git a/src/Http/Routing/src/EndpointSelectorContext.cs b/src/Http/Routing/src/EndpointSelectorContext.cs deleted file mode 100644 index 2e30ef54f9..0000000000 --- a/src/Http/Routing/src/EndpointSelectorContext.cs +++ /dev/null @@ -1,51 +0,0 @@ -// 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; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Endpoints; - -namespace Microsoft.AspNetCore.Routing -{ - public readonly struct EndpointSelectorContext - { - private readonly HttpContext _httpContext; - - public EndpointSelectorContext(HttpContext httpContext) - { - _httpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext)); - } - - /// - /// Gets or sets the selected for the current - /// request. - /// - public Endpoint Endpoint - { - get - { - return _httpContext.GetEndpoint(); - } - set - { - _httpContext.SetEndpoint(value); - } - } - - /// - /// Gets or sets the associated with the currrent - /// request. - /// - public RouteValueDictionary RouteValues - { - get - { - return _httpContext.Request.RouteValues; - } - set - { - _httpContext.Request.RouteValues = value; - } - } - } -} diff --git a/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs b/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs index 4a796b954c..c757b1646e 100644 --- a/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs +++ b/src/Http/Routing/src/Matching/DataSourceDependentMatcher.cs @@ -31,9 +31,9 @@ namespace Microsoft.AspNetCore.Routing.Matching // Used in tests internal Matcher CurrentMatcher => _cache.Value; - public override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public override Task MatchAsync(HttpContext httpContext) { - return CurrentMatcher.MatchAsync(httpContext, context); + return CurrentMatcher.MatchAsync(httpContext); } private Matcher CreateMatcher(IReadOnlyList endpoints) diff --git a/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs b/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs index 2ef8015753..98415acaf4 100644 --- a/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs +++ b/src/Http/Routing/src/Matching/DefaultEndpointSelector.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; namespace Microsoft.AspNetCore.Routing.Matching { @@ -13,7 +14,6 @@ namespace Microsoft.AspNetCore.Routing.Matching { public override Task SelectAsync( HttpContext httpContext, - EndpointSelectorContext context, CandidateSet candidateSet) { if (httpContext == null) @@ -26,14 +26,11 @@ namespace Microsoft.AspNetCore.Routing.Matching throw new ArgumentNullException(nameof(candidateSet)); } - Select(httpContext, context, candidateSet.Candidates); + Select(httpContext, candidateSet.Candidates); return Task.CompletedTask; } - internal static void Select( - HttpContext httpContext, - EndpointSelectorContext context, - CandidateState[] candidateState) + internal static void Select(HttpContext httpContext, CandidateState[] candidateState) { // Fast path: We can specialize for trivial numbers of candidates since there can // be no ambiguities @@ -50,8 +47,8 @@ namespace Microsoft.AspNetCore.Routing.Matching ref var state = ref candidateState[0]; if (CandidateSet.IsValidCandidate(ref state)) { - context.Endpoint = state.Endpoint; - context.RouteValues = state.Values; + httpContext.SetEndpoint(state.Endpoint); + httpContext.Request.RouteValues = state.Values; } break; @@ -61,7 +58,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Slow path: There's more than one candidate (to say nothing of validity) so we // have to process for ambiguities. - ProcessFinalCandidates(httpContext, context, candidateState); + ProcessFinalCandidates(httpContext, candidateState); break; } } @@ -69,7 +66,6 @@ namespace Microsoft.AspNetCore.Routing.Matching private static void ProcessFinalCandidates( HttpContext httpContext, - EndpointSelectorContext context, CandidateState[] candidateState) { Endpoint endpoint = null; @@ -100,7 +96,7 @@ namespace Microsoft.AspNetCore.Routing.Matching } else if (foundScore == state.Score) { - // This is the second match we've found of the same score, so there + // This is the second match we've found of the same score, so there // must be an ambiguity. // // Don't worry about the 'null == state.Score' case, it returns false. @@ -114,8 +110,8 @@ namespace Microsoft.AspNetCore.Routing.Matching if (endpoint != null) { - context.Endpoint = endpoint; - context.RouteValues = values; + httpContext.SetEndpoint(endpoint); + httpContext.Request.RouteValues = values; } } diff --git a/src/Http/Routing/src/Matching/DfaMatcher.cs b/src/Http/Routing/src/Matching/DfaMatcher.cs index dc9a3a7b43..af4fd1af11 100644 --- a/src/Http/Routing/src/Matching/DfaMatcher.cs +++ b/src/Http/Routing/src/Matching/DfaMatcher.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.Extensions.Logging; @@ -27,7 +28,7 @@ namespace Microsoft.AspNetCore.Routing.Matching _isDefaultEndpointSelector = selector is DefaultEndpointSelector; } - public sealed override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public sealed override Task MatchAsync(HttpContext httpContext) { if (httpContext == null) { @@ -73,10 +74,10 @@ namespace Microsoft.AspNetCore.Routing.Matching { ref readonly var candidate = ref candidates[0]; - // Just strict path matching + // Just strict path matching (no route values) if (candidate.Flags == Candidate.CandidateFlags.None) { - context.Endpoint = candidate.Endpoint; + httpContext.SetEndpoint(candidate.Endpoint); // We're done return Task.CompletedTask; @@ -181,7 +182,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // Fast path that avoids allocating the candidate set. // // We can use this when there are no policies and we're using the default selector. - DefaultEndpointSelector.Select(httpContext, context, candidateState); + DefaultEndpointSelector.Select(httpContext, candidateState); return Task.CompletedTask; } else if (policyCount == 0) @@ -189,10 +190,10 @@ namespace Microsoft.AspNetCore.Routing.Matching // Fast path that avoids a state machine. // // We can use this when there are no policies and a non-default selector. - return _selector.SelectAsync(httpContext, context, new CandidateSet(candidateState)); + return _selector.SelectAsync(httpContext, new CandidateSet(candidateState)); } - return SelectEndpointWithPoliciesAsync(httpContext, context, policies, new CandidateSet(candidateState)); + return SelectEndpointWithPoliciesAsync(httpContext, policies, new CandidateSet(candidateState)); } internal (Candidate[] candidates, IEndpointSelectorPolicy[] policies) FindCandidateSet( @@ -304,22 +305,21 @@ namespace Microsoft.AspNetCore.Routing.Matching private async Task SelectEndpointWithPoliciesAsync( HttpContext httpContext, - EndpointSelectorContext context, IEndpointSelectorPolicy[] policies, CandidateSet candidateSet) { for (var i = 0; i < policies.Length; i++) { var policy = policies[i]; - await policy.ApplyAsync(httpContext, context, candidateSet); - if (context.Endpoint != null) + await policy.ApplyAsync(httpContext, candidateSet); + if (httpContext.GetEndpoint() != null) { // This is a short circuit, the selector chose an endpoint. return; } } - await _selector.SelectAsync(httpContext, context, candidateSet); + await _selector.SelectAsync(httpContext, candidateSet); } internal static class EventIds diff --git a/src/Http/Routing/src/Matching/EndpointSelector.cs b/src/Http/Routing/src/Matching/EndpointSelector.cs index ba1345bb5a..1e868d76b2 100644 --- a/src/Http/Routing/src/Matching/EndpointSelector.cs +++ b/src/Http/Routing/src/Matching/EndpointSelector.cs @@ -1,9 +1,9 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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 Microsoft.AspNetCore.Http.Endpoints; namespace Microsoft.AspNetCore.Routing.Matching { @@ -18,16 +18,13 @@ namespace Microsoft.AspNetCore.Routing.Matching /// Asynchronously selects an from the . /// /// The associated with the current request. - /// The associated with the current request. /// The . /// A that completes asynchronously once endpoint selection is complete. /// - /// An should assign the - /// and properties once an endpoint is selected. + /// An should assign the endpoint by calling + /// + /// and setting once an endpoint is selected. /// - public abstract Task SelectAsync( - HttpContext httpContext, - EndpointSelectorContext context, - CandidateSet candidates); + public abstract Task SelectAsync(HttpContext httpContext, CandidateSet candidates); } } diff --git a/src/Http/Routing/src/Matching/HostMatcherPolicy.cs b/src/Http/Routing/src/Matching/HostMatcherPolicy.cs index 4f52eb3932..be6f5bbf84 100644 --- a/src/Http/Routing/src/Matching/HostMatcherPolicy.cs +++ b/src/Http/Routing/src/Matching/HostMatcherPolicy.cs @@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.Routing.Matching }); } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { diff --git a/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs b/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs index 4a6d17344f..58b9c5f4fd 100644 --- a/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs +++ b/src/Http/Routing/src/Matching/HttpMethodMatcherPolicy.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Primitives; using Microsoft.Net.Http.Headers; @@ -84,10 +85,9 @@ namespace Microsoft.AspNetCore.Routing.Matching /// For framework use only. /// /// - /// /// /// - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { @@ -168,7 +168,8 @@ namespace Microsoft.AspNetCore.Routing.Matching if (needs405Endpoint == true) { // We saw some endpoints coming in, and we eliminated them all. - context.Endpoint = CreateRejectionEndpoint(methods.OrderBy(m => m, StringComparer.OrdinalIgnoreCase)); + httpContext.SetEndpoint(CreateRejectionEndpoint(methods.OrderBy(m => m, StringComparer.OrdinalIgnoreCase))); + httpContext.Request.RouteValues = null; } return Task.CompletedTask; @@ -294,7 +295,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // unlikely in a traditional MVC application. That's good. // // We don't bother returning a 405 when the CORS preflight method doesn't exist. - // The developer calling the API will see it as a CORS error, which is fine because + // The developer calling the API will see it as a CORS error, which is fine because // there isn't an endpoint to check for a CORS policy. if (!edges.TryGetValue(new EdgeKey(AnyMethod, false), out var matches)) { diff --git a/src/Http/Routing/src/Matching/IEndpointSelectorPolicy.cs b/src/Http/Routing/src/Matching/IEndpointSelectorPolicy.cs index cae20e085b..221f8a9776 100644 --- a/src/Http/Routing/src/Matching/IEndpointSelectorPolicy.cs +++ b/src/Http/Routing/src/Matching/IEndpointSelectorPolicy.cs @@ -1,7 +1,8 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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 Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using System.Collections.Generic; using System.Threading.Tasks; @@ -31,9 +32,6 @@ namespace Microsoft.AspNetCore.Routing.Matching /// /// The associated with the current request. /// - /// - /// The associated with the current request. - /// /// The . /// /// @@ -42,10 +40,12 @@ namespace Microsoft.AspNetCore.Routing.Matching /// to false where desired. /// /// - /// To signal an error condition, set to an + /// To signal an error condition, the should assign the endpoint by + /// calling + /// and setting to an /// value that will produce the desired error when executed. /// /// - Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates); + Task ApplyAsync(HttpContext httpContext, CandidateSet candidates); } } diff --git a/src/Http/Routing/src/Matching/Matcher.cs b/src/Http/Routing/src/Matching/Matcher.cs index b713e4bdf3..3f3297a651 100644 --- a/src/Http/Routing/src/Matching/Matcher.cs +++ b/src/Http/Routing/src/Matching/Matcher.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -17,10 +17,7 @@ namespace Microsoft.AspNetCore.Routing.Matching /// Attempts to asynchronously select an for the current request. /// /// The associated with the current request. - /// - /// The associated with the current request. The - /// will be mutated to contain the result of the operation. /// A which represents the asynchronous completion of the operation. - public abstract Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context); + public abstract Task MatchAsync(HttpContext httpContext); } } diff --git a/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs b/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs index 1941d77b98..dda1e3f79e 100644 --- a/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs +++ b/src/Http/Routing/test/UnitTests/Builder/EndpointRoutingApplicationBuilderExtensionsTest.cs @@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Builder // Assert var feature = httpContext.Features.Get(); Assert.NotNull(feature); - Assert.Same(endpoint, feature.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); } [Fact] diff --git a/src/Http/Routing/test/UnitTests/EndpointSelectorContextTest.cs b/src/Http/Routing/test/UnitTests/EndpointSelectorContextTest.cs deleted file mode 100644 index 833d2697d0..0000000000 --- a/src/Http/Routing/test/UnitTests/EndpointSelectorContextTest.cs +++ /dev/null @@ -1,53 +0,0 @@ -// 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 Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Endpoints; -using Microsoft.AspNetCore.Routing.Patterns; -using Xunit; - -namespace Microsoft.AspNetCore.Routing -{ - public class EndpointSelectorContextTest - { - [Fact] - public void SettingEndpointSetsEndpointOnHttpContext() - { - var httpContext = new DefaultHttpContext(); - var ep = new RouteEndpoint( - TestConstants.EmptyRequestDelegate, - RoutePatternFactory.Parse("/"), - 0, - new EndpointMetadataCollection(), - "test"); - - new EndpointSelectorContext(httpContext) - { - Endpoint = ep, - }; - - // Assert - var endpoint = httpContext.GetEndpoint(); - Assert.NotNull(endpoint); - Assert.Same(ep, endpoint); - } - - [Fact] - public void SettingRouteValuesSetRouteValuesHttpContext() - { - var httpContext = new DefaultHttpContext(); - var routeValues = new RouteValueDictionary(new { A = "1" }); - - new EndpointSelectorContext(httpContext) - { - RouteValues = routeValues - }; - - // Assert - Assert.NotNull(httpContext.Request.RouteValues); - Assert.Same(routeValues, httpContext.Request.RouteValues); - Assert.Single(httpContext.Request.RouteValues); - Assert.Equal("1", httpContext.Request.RouteValues["A"]); - } - } -} diff --git a/src/Http/Routing/test/UnitTests/Matching/BarebonesMatcher.cs b/src/Http/Routing/test/UnitTests/Matching/BarebonesMatcher.cs index 87eecf6b0f..160d11d014 100644 --- a/src/Http/Routing/test/UnitTests/Matching/BarebonesMatcher.cs +++ b/src/Http/Routing/test/UnitTests/Matching/BarebonesMatcher.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Routing.Matching @@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Matchers = matchers; } - public override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public override Task MatchAsync(HttpContext httpContext) { if (httpContext == null) { @@ -32,8 +33,8 @@ namespace Microsoft.AspNetCore.Routing.Matching { if (Matchers[i].TryMatch(path)) { - context.Endpoint = Matchers[i].Endpoint; - context.RouteValues = new RouteValueDictionary(); + httpContext.SetEndpoint(Matchers[i].Endpoint); + httpContext.Request.RouteValues = new RouteValueDictionary(); } } @@ -116,12 +117,12 @@ namespace Microsoft.AspNetCore.Routing.Matching return Array.Empty(); } - public override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public override Task MatchAsync(HttpContext httpContext) { if (TryMatch(httpContext.Request.Path.Value)) { - context.Endpoint = Endpoint; - context.RouteValues = new RouteValueDictionary(); + httpContext.SetEndpoint(Endpoint); + httpContext.Request.RouteValues = new RouteValueDictionary(); } return Task.CompletedTask; diff --git a/src/Http/Routing/test/UnitTests/Matching/DataSourceDependentMatcherTest.cs b/src/Http/Routing/test/UnitTests/Matching/DataSourceDependentMatcherTest.cs index da46b300ef..0e13e724a5 100644 --- a/src/Http/Routing/test/UnitTests/Matching/DataSourceDependentMatcherTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/DataSourceDependentMatcherTest.cs @@ -162,7 +162,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { public IReadOnlyList Endpoints { get; set; } - public override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public override Task MatchAsync(HttpContext httpContext) { throw new NotImplementedException(); } diff --git a/src/Http/Routing/test/UnitTests/Matching/DefaultEndpointSelectorTest.cs b/src/Http/Routing/test/UnitTests/Matching/DefaultEndpointSelectorTest.cs index 65b5660930..a5d234b97d 100644 --- a/src/Http/Routing/test/UnitTests/Matching/DefaultEndpointSelectorTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/DefaultEndpointSelectorTest.cs @@ -4,6 +4,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Patterns; using Moq; @@ -21,14 +22,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var scores = new int[] { }; var candidateSet = CreateCandidateSet(endpoints, scores); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } [Fact] @@ -42,14 +43,14 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet[0].Values = new RouteValueDictionary(); candidateSet.SetValidity(0, false); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } [Fact] @@ -63,14 +64,14 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet[0].Values = new RouteValueDictionary(); candidateSet.SetValidity(0, true); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Same(endpoints[0], context.Endpoint); + Assert.Same(endpoints[0], httpContext.GetEndpoint()); } [Fact] @@ -84,14 +85,14 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet.SetValidity(0, false); candidateSet.SetValidity(1, true); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Same(endpoints[1], context.Endpoint); + Assert.Same(endpoints[1], httpContext.GetEndpoint()); } [Fact] @@ -106,14 +107,14 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet.SetValidity(1, true); candidateSet.SetValidity(2, true); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Same(endpoints[1], context.Endpoint); + Assert.Same(endpoints[1], httpContext.GetEndpoint()); } [Fact] @@ -137,14 +138,14 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet.SetValidity(3, false); candidateSet.SetValidity(4, true); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - await selector.SelectAsync(httpContext, context, candidateSet); + await selector.SelectAsync(httpContext, candidateSet); // Assert - Assert.Same(endpoints[4], context.Endpoint); + Assert.Same(endpoints[4], httpContext.GetEndpoint()); } [Fact] @@ -159,23 +160,22 @@ namespace Microsoft.AspNetCore.Routing.Matching candidateSet.SetValidity(1, true); candidateSet.SetValidity(2, true); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); var selector = CreateSelector(); // Act - var ex = await Assert.ThrowsAsync(() => selector.SelectAsync(httpContext, context, candidateSet)); + var ex = await Assert.ThrowsAsync(() => selector.SelectAsync(httpContext, candidateSet)); // Assert Assert.Equal( @"The request matched multiple endpoints. Matches: " + Environment.NewLine + Environment.NewLine + "test: /test2" + Environment.NewLine + "test: /test3", ex.Message); - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } - private static (HttpContext httpContext, EndpointSelectorContext context) CreateContext() + private static HttpContext CreateContext() { - var httpContext = new DefaultHttpContext(); - return (httpContext, new EndpointSelectorContext(httpContext)); + return new DefaultHttpContext(); } private static RouteEndpoint CreateEndpoint(string template) diff --git a/src/Http/Routing/test/UnitTests/Matching/DfaMatcherConformanceTest.cs b/src/Http/Routing/test/UnitTests/Matching/DfaMatcherConformanceTest.cs index e34abbd5b3..46ca9fdb2c 100644 --- a/src/Http/Routing/test/UnitTests/Matching/DfaMatcherConformanceTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/DfaMatcherConformanceTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -14,13 +14,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } internal override Matcher CreateMatcher(params RouteEndpoint[] endpoints) diff --git a/src/Http/Routing/test/UnitTests/Matching/DfaMatcherTest.cs b/src/Http/Routing/test/UnitTests/Matching/DfaMatcherTest.cs index ff706040c9..1539abc9a9 100644 --- a/src/Http/Routing/test/UnitTests/Matching/DfaMatcherTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/DfaMatcherTest.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Constraints; using Microsoft.AspNetCore.Routing.Patterns; @@ -76,14 +77,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/1"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.NotNull(context.Endpoint); + Assert.NotNull(httpContext.GetEndpoint()); } [Fact] @@ -97,14 +98,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/One"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } [Fact] @@ -123,17 +124,17 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpoint, context.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); Assert.Collection( - context.RouteValues.OrderBy(kvp => kvp.Key), + httpContext.Request.RouteValues.OrderBy(kvp => kvp.Key), (kvp) => { Assert.Equal("action", kvp.Key); @@ -162,14 +163,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Login/Index"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } [Fact] @@ -188,17 +189,17 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Home/Index/123"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpoint, context.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); Assert.Collection( - context.RouteValues.OrderBy(kvp => kvp.Key), + httpContext.Request.RouteValues.OrderBy(kvp => kvp.Key), (kvp) => { Assert.Equal("action", kvp.Key); @@ -237,18 +238,18 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = path; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpoint, context.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); - Assert.Equal("TestAction", context.RouteValues["action"]); - Assert.Equal("TestController", context.RouteValues["controller"]); - Assert.Equal("17", context.RouteValues["id"]); + Assert.Equal("TestAction", httpContext.Request.RouteValues["action"]); + Assert.Equal("TestController", httpContext.Request.RouteValues["controller"]); + Assert.Equal("17", httpContext.Request.RouteValues["id"]); } [Fact] @@ -272,22 +273,22 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Home/Index/123"; // Act 1 - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert 1 - Assert.Same(endpoint1, context.Endpoint); + Assert.Same(endpoint1, httpContext.GetEndpoint()); httpContext.Request.Path = "/Login/Index/123"; // Act 2 - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert 2 - Assert.Same(endpoint2, context.Endpoint); + Assert.Same(endpoint2, httpContext.GetEndpoint()); } [Fact] @@ -306,17 +307,17 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/ConventionalTransformerRoute/conventional-transformer/Index"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpoint, context.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); Assert.Collection( - context.RouteValues.OrderBy(kvp => kvp.Key), + httpContext.Request.RouteValues.OrderBy(kvp => kvp.Key), (kvp) => { Assert.Equal("action", kvp.Key); @@ -345,17 +346,17 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/TestController"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpoint, context.Endpoint); + Assert.Same(endpoint, httpContext.GetEndpoint()); Assert.Collection( - context.RouteValues.OrderBy(kvp => kvp.Key), + httpContext.Request.RouteValues.OrderBy(kvp => kvp.Key), (kvp) => { Assert.Equal("action", kvp.Key); @@ -383,14 +384,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Teams"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Equal(lowerOrderEndpoint, context.Endpoint); + Assert.Equal(lowerOrderEndpoint, httpContext.GetEndpoint()); } [Fact] @@ -402,8 +403,8 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpointSelector = new Mock(); endpointSelector - .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((c, f, cs) => + .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny())) + .Callback((c, cs) => { Assert.Equal(2, cs.Count); @@ -417,7 +418,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Assert.Equal(1, cs[1].Score); Assert.Null(cs[1].Values); - f.Endpoint = endpoint2; + c.SetEndpoint(endpoint2); }) .Returns(Task.CompletedTask); @@ -429,14 +430,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource, endpointSelector: endpointSelector.Object); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Teams"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Equal(endpoint2, context.Endpoint); + Assert.Equal(endpoint2, httpContext.GetEndpoint()); } [Fact] @@ -448,8 +449,8 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpointSelector = new Mock(); endpointSelector - .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((c, f, cs) => + .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny())) + .Callback((c, cs) => { Assert.Equal(2, cs.Count); @@ -463,7 +464,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Assert.Equal(1, cs[1].Score); Assert.Empty(cs[1].Values); - f.Endpoint = endpoint2; + c.SetEndpoint(endpoint2); }) .Returns(Task.CompletedTask); @@ -475,14 +476,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource, endpointSelector: endpointSelector.Object); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Teams"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Equal(endpoint2, context.Endpoint); + Assert.Equal(endpoint2, httpContext.GetEndpoint()); } [Fact] @@ -495,8 +496,8 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpointSelector = new Mock(); endpointSelector - .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((c, f, cs) => + .Setup(s => s.SelectAsync(It.IsAny(), It.IsAny())) + .Callback((c, cs) => { Assert.Equal(2, cs.Count); @@ -510,7 +511,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Assert.Equal(1, cs[1].Score); Assert.Empty(cs[1].Values); - f.Endpoint = endpoint2; + c.SetEndpoint(endpoint2); }) .Returns(Task.CompletedTask); @@ -522,14 +523,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(endpointDataSource, endpointSelector: endpointSelector.Object); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/Teams"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Equal(endpoint2, context.Endpoint); + Assert.Equal(endpoint2, httpContext.GetEndpoint()); } [Fact] @@ -544,14 +545,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var sink = new TestSink(); var matcher = CreateDfaMatcher(endpointDataSource, loggerFactory: new TestLoggerFactory(sink, enabled: true)); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); Assert.Collection( sink.Writes, @@ -574,14 +575,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var sink = new TestSink(); var matcher = CreateDfaMatcher(endpointDataSource, loggerFactory: new TestLoggerFactory(sink, enabled: true)); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/One"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); Assert.Collection( sink.Writes, @@ -614,14 +615,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var sink = new TestSink(); var matcher = CreateDfaMatcher(endpointDataSource, loggerFactory: new TestLoggerFactory(sink, enabled: true)); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/One"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); Assert.Collection( sink.Writes, @@ -656,14 +657,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var sink = new TestSink(); var matcher = CreateDfaMatcher(endpointDataSource, loggerFactory: new TestLoggerFactory(sink, enabled: true)); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/One"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(endpointDataSource.Endpoints[0], context.Endpoint); + Assert.Same(endpointDataSource.Endpoints[0], httpContext.GetEndpoint()); Assert.Collection( sink.Writes, @@ -716,8 +717,8 @@ namespace Microsoft.AspNetCore.Routing.Matching .Setup(p => p.AppliesToEndpoints(It.IsAny>())).Returns(true); policy .As() - .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns((c, f, cs) => + .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny())) + .Returns((c, cs) => { cs.SetValidity(1, false); return Task.CompletedTask; @@ -725,14 +726,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var matcher = CreateDfaMatcher(dataSource, policies: new[] { policy.Object, }); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/test/17"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(dataSource.Endpoints[2], context.Endpoint); + Assert.Same(dataSource.Endpoints[2], httpContext.GetEndpoint()); } [Fact] @@ -752,22 +753,22 @@ namespace Microsoft.AspNetCore.Routing.Matching .Setup(p => p.AppliesToEndpoints(It.IsAny>())).Returns(false); policy .As() - .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns((c, f, cs) => + .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny())) + .Returns((c, cs) => { throw null; // Won't be called. }); var matcher = CreateDfaMatcher(dataSource, policies: new[] { policy.Object, }); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/test/17"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(dataSource.Endpoints[1], context.Endpoint); + Assert.Same(dataSource.Endpoints[1], httpContext.GetEndpoint()); } [Fact] @@ -787,10 +788,10 @@ namespace Microsoft.AspNetCore.Routing.Matching .Setup(p => p.AppliesToEndpoints(It.IsAny>())).Returns(true); policy1 .As() - .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .Returns((c, f, cs) => + .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny())) + .Returns((c, cs) => { - f.Endpoint = cs[0].Endpoint; + c.SetEndpoint(cs[0].Endpoint); return Task.CompletedTask; }); @@ -804,26 +805,24 @@ namespace Microsoft.AspNetCore.Routing.Matching .Setup(p => p.AppliesToEndpoints(It.IsAny>())).Returns(true); policy2 .As() - .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Setup(p => p.ApplyAsync(It.IsAny(), It.IsAny())) .Throws(new InvalidOperationException()); var matcher = CreateDfaMatcher(dataSource, policies: new[] { policy1.Object, policy2.Object, }); - var (httpContext, context) = CreateContext(); + var httpContext = CreateContext(); httpContext.Request.Path = "/test/17"; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.Same(dataSource.Endpoints[0], context.Endpoint); + Assert.Same(dataSource.Endpoints[0], httpContext.GetEndpoint()); } - private (HttpContext httpContext, EndpointSelectorContext context) CreateContext() + private HttpContext CreateContext() { - var httpContext = new DefaultHttpContext(); - - return (httpContext, new EndpointSelectorContext(httpContext)); + return new DefaultHttpContext(); } } } diff --git a/src/Http/Routing/test/UnitTests/Matching/FullFeaturedMatcherConformanceTest.cs b/src/Http/Routing/test/UnitTests/Matching/FullFeaturedMatcherConformanceTest.cs index b470d9369f..ca86fe3e1d 100644 --- a/src/Http/Routing/test/UnitTests/Matching/FullFeaturedMatcherConformanceTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/FullFeaturedMatcherConformanceTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -28,13 +28,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Fact] @@ -43,13 +43,13 @@ namespace Microsoft.AspNetCore.Routing.Matching // Arrange var endpoint = CreateEndpoint("/a/{b}/{c}", new { b = "17", c = "18", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/a"); + var httpContext = CreateContext("/a"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, new { b = "17", c = "18", }); + MatcherAssert.AssertMatch(httpContext, endpoint, new { b = "17", c = "18", }); } [Fact] @@ -58,13 +58,13 @@ namespace Microsoft.AspNetCore.Routing.Matching // Arrange var endpoint = CreateEndpoint("/a/{b}/{c}", new { b = "17", c = "18", d = "19" }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/a"); + var httpContext = CreateContext("/a"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, new { b = "17", c = "18", d = "19" }); + MatcherAssert.AssertMatch(httpContext, endpoint, new { b = "17", c = "18", d = "19" }); } [Theory] @@ -83,13 +83,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Theory] @@ -115,13 +115,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Theory] @@ -138,13 +138,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Theory] @@ -162,13 +162,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } // Historically catchall segments don't match an empty segment, but only if it's @@ -182,13 +182,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); // Need to access these to prevent a warning from the xUnit analyzer. // Some of these tests will match (and process the values) and some will not. @@ -217,13 +217,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Theory] @@ -237,13 +237,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Theory] @@ -266,13 +266,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Theory] @@ -292,13 +292,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } // Most of are copied from old routing tests that date back to the VS 2010 era. Enjoy! @@ -317,13 +317,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Theory] @@ -347,13 +347,13 @@ namespace Microsoft.AspNetCore.Routing.Matching // Arrange var matcher = CreateMatcher(other, expected); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, expected, ignoreValues: true); + MatcherAssert.AssertMatch(httpContext, expected, ignoreValues: true); } [Theory] @@ -381,13 +381,13 @@ namespace Microsoft.AspNetCore.Routing.Matching // Arrange var matcher = CreateMatcher(other, expected); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, expected, ignoreValues: true); + MatcherAssert.AssertMatch(httpContext, expected, ignoreValues: true); } [Theory] @@ -434,13 +434,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var expected = endpoints[Array.IndexOf(templates, expectedTemplate)]; var matcher = CreateMatcher(endpoints); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, expected, ignoreValues: true); + MatcherAssert.AssertMatch(httpContext, expected, ignoreValues: true); } } -} \ No newline at end of file +} diff --git a/src/Http/Routing/test/UnitTests/Matching/HostMatcherPolicyIntegrationTestBase.cs b/src/Http/Routing/test/UnitTests/Matching/HostMatcherPolicyIntegrationTestBase.cs index 8fdef05eef..e3cb4732b6 100644 --- a/src/Http/Routing/test/UnitTests/Matching/HostMatcherPolicyIntegrationTestBase.cs +++ b/src/Http/Routing/test/UnitTests/Matching/HostMatcherPolicyIntegrationTestBase.cs @@ -24,13 +24,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -40,13 +40,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:8080", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com:8080"); + var httpContext = CreateContext("/hello", "contoso.com:8080"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -56,13 +56,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "æon.contoso.com", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "æon.contoso.com"); + var httpContext = CreateContext("/hello", "æon.contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -72,13 +72,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:8080", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com:1111"); + var httpContext = CreateContext("/hello", "contoso.com:1111"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] @@ -88,13 +88,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:8080", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "www.contoso.com:8080"); + var httpContext = CreateContext("/hello", "www.contoso.com:8080"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] @@ -104,13 +104,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*.contoso.com:8080", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "æon.contoso.com:8080"); + var httpContext = CreateContext("/hello", "æon.contoso.com:8080"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -120,13 +120,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*.contoso.com:8080", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "www.contoso.com:8080"); + var httpContext = CreateContext("/hello", "www.contoso.com:8080"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -136,13 +136,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "Contoso.COM", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -152,13 +152,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:80", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com", "http"); + var httpContext = CreateContext("/hello", "contoso.com", "http"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -168,13 +168,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:443", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com", "https"); + var httpContext = CreateContext("/hello", "contoso.com", "https"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -184,13 +184,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "contoso.com:443", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", null, "https"); + var httpContext = CreateContext("/hello", null, "https"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] @@ -200,13 +200,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*:443", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", null, "https"); + var httpContext = CreateContext("/hello", null, "https"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -216,13 +216,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello"); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -232,13 +232,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -248,13 +248,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -264,13 +264,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", hosts: new string[] { "*:*", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "contoso.com"); + var httpContext = CreateContext("/hello", "contoso.com"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } private static Matcher CreateMatcher(params RouteEndpoint[] endpoints) @@ -290,7 +290,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return builder.Build(); } - internal static (HttpContext httpContext, EndpointSelectorContext context) CreateContext( + internal static HttpContext CreateContext( string path, string host, string scheme = null) @@ -303,7 +303,7 @@ namespace Microsoft.AspNetCore.Routing.Matching httpContext.Request.Path = path; httpContext.Request.Scheme = scheme; - return (httpContext, new EndpointSelectorContext(httpContext)); + return httpContext; } internal RouteEndpoint CreateEndpoint( diff --git a/src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyIntegrationTestBase.cs b/src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyIntegrationTestBase.cs index 6009c07d54..c665958a61 100644 --- a/src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyIntegrationTestBase.cs +++ b/src/Http/Routing/test/UnitTests/Matching/HttpMethodMatcherPolicyIntegrationTestBase.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.Extensions.DependencyInjection; @@ -25,13 +26,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GET", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -41,13 +42,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GET", }, acceptCorsPreflight: true); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -57,13 +58,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GET", }, acceptCorsPreflight: true); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true); + var httpContext = CreateContext("/hello", "GET", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } @@ -74,14 +75,14 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GET", }, acceptCorsPreflight: false); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true); + var httpContext = CreateContext("/hello", "GET", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.NotSame(endpoint, context.Endpoint); - Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, context.Endpoint.DisplayName); + Assert.NotSame(endpoint, httpContext.GetEndpoint()); + Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, httpContext.GetEndpoint().DisplayName); } [Fact] @@ -91,13 +92,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GeT", }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -107,13 +108,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { "GeT", }, acceptCorsPreflight: true); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true); + var httpContext = CreateContext("/hello", "GET", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -123,13 +124,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello"); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -139,13 +140,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", acceptCorsPreflight: true); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true); + var httpContext = CreateContext("/hello", "GET", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] // This matches because the endpoint accepts OPTIONS @@ -155,13 +156,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", acceptCorsPreflight: false); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET", corsPreflight: true); + var httpContext = CreateContext("/hello", "GET", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -171,13 +172,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint = CreateEndpoint("/hello", httpMethods: new string[] { }); var matcher = CreateMatcher(endpoint); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] // When all of the candidates handles specific verbs, use a 405 endpoint @@ -188,19 +189,19 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "POST"); + var httpContext = CreateContext("/hello", "POST"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.NotSame(endpoint1, context.Endpoint); - Assert.NotSame(endpoint2, context.Endpoint); + Assert.NotSame(endpoint1, httpContext.GetEndpoint()); + Assert.NotSame(endpoint2, httpContext.GetEndpoint()); - Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, context.Endpoint.DisplayName); + Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, httpContext.GetEndpoint().DisplayName); // Invoke the endpoint - await context.Endpoint.RequestDelegate(httpContext); + await httpContext.GetEndpoint().RequestDelegate(httpContext); Assert.Equal(405, httpContext.Response.StatusCode); Assert.Equal("DELETE, GET, PUT", httpContext.Response.Headers["Allow"]); } @@ -213,13 +214,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "POST", corsPreflight: true); + var httpContext = CreateContext("/hello", "POST", corsPreflight: true); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] // When one of the candidates handles all verbs, dont use a 405 endpoint @@ -230,13 +231,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "POST"); + var httpContext = CreateContext("/hello", "POST"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] @@ -247,13 +248,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/bar"); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint1); + MatcherAssert.AssertMatch(httpContext, endpoint1); } [Fact] @@ -264,13 +265,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/bar", httpMethods: new string[] { }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "GET"); + var httpContext = CreateContext("/hello", "GET"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint1); + MatcherAssert.AssertMatch(httpContext, endpoint1); } [Fact] // The non-http-method-specific endpoint is part of the same candidate set @@ -281,13 +282,13 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/{x}", httpMethods: new string[] { }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "POST"); + var httpContext = CreateContext("/hello", "POST"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint2, ignoreValues: true); + MatcherAssert.AssertMatch(httpContext, endpoint2, ignoreValues: true); } [Fact] // See https://github.com/aspnet/AspNetCore/issues/6415 @@ -298,24 +299,24 @@ namespace Microsoft.AspNetCore.Routing.Matching var endpoint2 = CreateEndpoint("/hello", httpMethods: new string[] { "DELETE" }); var matcher = CreateMatcher(endpoint1, endpoint2); - var (httpContext, context) = CreateContext("/hello", "POST"); + var httpContext = CreateContext("/hello", "POST"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - Assert.NotSame(endpoint1, context.Endpoint); - Assert.NotSame(endpoint2, context.Endpoint); + Assert.NotSame(endpoint1, httpContext.GetEndpoint()); + Assert.NotSame(endpoint2, httpContext.GetEndpoint()); - Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, context.Endpoint.DisplayName); + Assert.Same(HttpMethodMatcherPolicy.Http405EndpointDisplayName, httpContext.GetEndpoint().DisplayName); // Invoke the endpoint - await context.Endpoint.RequestDelegate(httpContext); + await httpContext.GetEndpoint().RequestDelegate(httpContext); Assert.Equal(405, httpContext.Response.StatusCode); Assert.Equal("DELETE, GET, PUT", httpContext.Response.Headers["Allow"]); // Invoke the endpoint again to verify headers not duplicated - await context.Endpoint.RequestDelegate(httpContext); + await httpContext.GetEndpoint().RequestDelegate(httpContext); Assert.Equal(405, httpContext.Response.StatusCode); Assert.Equal("DELETE, GET, PUT", httpContext.Response.Headers["Allow"]); } @@ -337,7 +338,7 @@ namespace Microsoft.AspNetCore.Routing.Matching return builder.Build(); } - internal static (HttpContext httpContext, EndpointSelectorContext context) CreateContext( + internal static HttpContext CreateContext( string path, string httpMethod, bool corsPreflight = false) @@ -352,7 +353,7 @@ namespace Microsoft.AspNetCore.Routing.Matching httpContext.Request.Headers[AccessControlRequestMethod] = httpMethod; } - return (httpContext, new EndpointSelectorContext(httpContext)); + return httpContext; } internal RouteEndpoint CreateEndpoint( diff --git a/src/Http/Routing/test/UnitTests/Matching/MatcherAssert.cs b/src/Http/Routing/test/UnitTests/Matching/MatcherAssert.cs index 1cf3b31350..16156551fd 100644 --- a/src/Http/Routing/test/UnitTests/Matching/MatcherAssert.cs +++ b/src/Http/Routing/test/UnitTests/Matching/MatcherAssert.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Xunit.Sdk; @@ -27,22 +28,22 @@ namespace Microsoft.AspNetCore.Routing.Matching } } - public static void AssertMatch(EndpointSelectorContext context, HttpContext httpContext, Endpoint expected) + public static void AssertMatch(HttpContext httpContext, Endpoint expected) { - AssertMatch(context, httpContext, expected, new RouteValueDictionary()); + AssertMatch(httpContext, expected, new RouteValueDictionary()); } - public static void AssertMatch(EndpointSelectorContext context, HttpContext httpContext, Endpoint expected, bool ignoreValues) + public static void AssertMatch(HttpContext httpContext, Endpoint expected, bool ignoreValues) { - AssertMatch(context, httpContext, expected, new RouteValueDictionary(), ignoreValues); + AssertMatch(httpContext, expected, new RouteValueDictionary(), ignoreValues); } - public static void AssertMatch(EndpointSelectorContext context, HttpContext httpContext, Endpoint expected, object values) + public static void AssertMatch(HttpContext httpContext, Endpoint expected, object values) { - AssertMatch(context, httpContext, expected, new RouteValueDictionary(values)); + AssertMatch(httpContext, expected, new RouteValueDictionary(values)); } - public static void AssertMatch(EndpointSelectorContext context, HttpContext httpContext, Endpoint expected, string[] keys, string[] values) + public static void AssertMatch(HttpContext httpContext, Endpoint expected, string[] keys, string[] values) { keys = keys ?? Array.Empty(); values = values ?? Array.Empty(); @@ -53,17 +54,16 @@ namespace Microsoft.AspNetCore.Routing.Matching } var zipped = keys.Zip(values, (k, v) => new KeyValuePair(k, v)); - AssertMatch(context, httpContext, expected, new RouteValueDictionary(zipped)); + AssertMatch(httpContext, expected, new RouteValueDictionary(zipped)); } public static void AssertMatch( - EndpointSelectorContext context, HttpContext httpContext, Endpoint expected, RouteValueDictionary values, bool ignoreValues = false) { - if (context.Endpoint == null) + if (httpContext.GetEndpoint() == null) { throw new XunitException($"Was expected to match '{expected.DisplayName}' but did not match."); } @@ -75,11 +75,11 @@ namespace Microsoft.AspNetCore.Routing.Matching throw new XunitException("RouteValues is null."); } - if (!object.ReferenceEquals(expected, context.Endpoint)) + if (!object.ReferenceEquals(expected, httpContext.GetEndpoint())) { throw new XunitException( $"Was expected to match '{expected.DisplayName}' but matched " + - $"'{context.Endpoint.DisplayName}' with values: {FormatRouteValues(actualValues)}."); + $"'{httpContext.GetEndpoint().DisplayName}' with values: {FormatRouteValues(actualValues)}."); } if (!ignoreValues) @@ -96,13 +96,13 @@ namespace Microsoft.AspNetCore.Routing.Matching } } - public static void AssertNotMatch(EndpointSelectorContext context, HttpContext httpContext) + public static void AssertNotMatch(HttpContext httpContext) { - if (context.Endpoint != null) + if (httpContext.GetEndpoint() != null) { throw new XunitException( - $"Was expected not to match '{context.Endpoint.DisplayName}' " + - $"but matched with values: {FormatRouteValues(httpContext.Features.Get().RouteValues)}."); + $"Was expected not to match '{httpContext.GetEndpoint().DisplayName}' " + + $"but matched with values: {FormatRouteValues(httpContext.Request.RouteValues)}."); } } diff --git a/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.SingleEndpoint.cs b/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.SingleEndpoint.cs index 667b5f3f1e..80ad342e3b 100644 --- a/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.SingleEndpoint.cs +++ b/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.SingleEndpoint.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -13,13 +13,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/"); - var (httpContext, context) = CreateContext("/"); + var httpContext = CreateContext("/"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -27,13 +27,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/simple"); - var (httpContext, context) = CreateContext("/simple"); + var httpContext = CreateContext("/simple"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Fact] @@ -41,13 +41,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/simple"); - var (httpContext, context) = CreateContext("/simple/"); + var httpContext = CreateContext("/simple/"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Theory] @@ -58,13 +58,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/Simple"); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } // Some matchers will optimize for the ASCII case @@ -75,13 +75,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } // Matchers should operate on the decoded representation - a matcher that calls @@ -93,13 +93,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } [Theory] @@ -113,13 +113,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/simple"); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Theory] @@ -130,13 +130,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext("/simple"); + var httpContext = CreateContext("/simple"); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } // Matchers do their own 'splitting' of the path into segments, so including @@ -153,13 +153,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint); + MatcherAssert.AssertMatch(httpContext, endpoint); } // Matchers do their own 'splitting' of the path into segments, so including @@ -184,13 +184,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Fact] @@ -198,14 +198,14 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/{p}"); - var (httpContext, context) = CreateContext("/14"); + var httpContext = CreateContext("/14"); var values = new RouteValueDictionary(new { p = "14", }); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, values); + MatcherAssert.AssertMatch(httpContext, endpoint, values); } [Fact] @@ -213,14 +213,14 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/{p:int}"); - var (httpContext, context) = CreateContext("/14"); + var httpContext = CreateContext("/14"); var values = new RouteValueDictionary(new { p = "14", }); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, values); + MatcherAssert.AssertMatch(httpContext, endpoint, values); } [Fact] @@ -228,14 +228,14 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/{p}"); - var (httpContext, context) = CreateContext("/14/"); + var httpContext = CreateContext("/14/"); var values = new RouteValueDictionary(new { p = "14", }); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, values); + MatcherAssert.AssertMatch(httpContext, endpoint, values); } [Fact] @@ -243,7 +243,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/foo/{ }/{.!$%}/{dynamic.data}"); - var (httpContext, context) = CreateContext("/foo/space/weirdmatch/matcherid"); + var httpContext = CreateContext("/foo/space/weirdmatch/matcherid"); var values = new RouteValueDictionary() { { " ", "space" }, @@ -252,10 +252,10 @@ namespace Microsoft.AspNetCore.Routing.Matching }; // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, values); + MatcherAssert.AssertMatch(httpContext, endpoint, values); } [Theory] @@ -267,13 +267,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher("/{p}"); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } [Theory] @@ -288,13 +288,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertMatch(context, httpContext, endpoint, keys, values); + MatcherAssert.AssertMatch(httpContext, endpoint, keys, values); } [Theory] @@ -317,13 +317,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Arrange var (matcher, endpoint) = CreateMatcher(template); - var (httpContext, context) = CreateContext(path); + var httpContext = CreateContext(path); // Act - await matcher.MatchAsync(httpContext, context); + await matcher.MatchAsync(httpContext); // Assert - MatcherAssert.AssertNotMatch(context, httpContext); + MatcherAssert.AssertNotMatch(httpContext); } } } diff --git a/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.cs b/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.cs index df0cdaf910..29b6fc9310 100644 --- a/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.cs +++ b/src/Http/Routing/test/UnitTests/Matching/MatcherConformanceTest.cs @@ -14,13 +14,13 @@ namespace Microsoft.AspNetCore.Routing.Matching { internal abstract Matcher CreateMatcher(params RouteEndpoint[] endpoints); - internal static (HttpContext httpContext, EndpointSelectorContext context) CreateContext(string path) + internal static HttpContext CreateContext(string path) { var httpContext = new DefaultHttpContext(); httpContext.Request.Method = "TEST"; httpContext.Request.Path = path; httpContext.RequestServices = CreateServices(); - return (httpContext, new EndpointSelectorContext(httpContext)); + return httpContext; } // The older routing implementations retrieve services when they first execute. diff --git a/src/Http/Routing/test/UnitTests/Matching/RouteMatcher.cs b/src/Http/Routing/test/UnitTests/Matching/RouteMatcher.cs index 24b4a006cb..540fc2e07b 100644 --- a/src/Http/Routing/test/UnitTests/Matching/RouteMatcher.cs +++ b/src/Http/Routing/test/UnitTests/Matching/RouteMatcher.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Routing.Matching _inner = inner; } - public async override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public async override Task MatchAsync(HttpContext httpContext) { if (httpContext == null) { @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Routing.Matching if (routeContext.Handler != null) { - context.RouteValues = routeContext.RouteData.Values; + httpContext.Request.RouteValues = routeContext.RouteData.Values; await routeContext.Handler(httpContext); } } diff --git a/src/Http/Routing/test/UnitTests/Matching/RouteMatcherBuilder.cs b/src/Http/Routing/test/UnitTests/Matching/RouteMatcherBuilder.cs index 058a7f40d9..ff4511ae85 100644 --- a/src/Http/Routing/test/UnitTests/Matching/RouteMatcherBuilder.cs +++ b/src/Http/Routing/test/UnitTests/Matching/RouteMatcherBuilder.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.AspNetCore.Routing.TestObjects; @@ -96,13 +97,11 @@ namespace Microsoft.AspNetCore.Routing.Matching public async Task RouteAsync(RouteContext routeContext) { - var context = new EndpointSelectorContext(routeContext.HttpContext); - // This is needed due to a quirk of our tests - they reuse the endpoint feature. - context.Endpoint = null; + routeContext.HttpContext.SetEndpoint(null); - await _selector.SelectAsync(routeContext.HttpContext, context, new CandidateSet(_candidates, _values, _scores)); - if (context.Endpoint != null) + await _selector.SelectAsync(routeContext.HttpContext, new CandidateSet(_candidates, _values, _scores)); + if (routeContext.HttpContext.GetEndpoint() != null) { routeContext.Handler = (_) => Task.CompletedTask; } diff --git a/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcher.cs b/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcher.cs index ec1c60105a..6145013c80 100644 --- a/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcher.cs +++ b/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcher.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Routing.Matching _inner = inner; } - public async override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public async override Task MatchAsync(HttpContext httpContext) { if (httpContext == null) { @@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Routing.Matching if (routeContext.Handler != null) { - context.RouteValues = routeContext.RouteData.Values; + httpContext.Request.RouteValues = routeContext.RouteData.Values; await routeContext.Handler(httpContext); } } diff --git a/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcherBuilder.cs b/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcherBuilder.cs index 572308e9a4..66af506cc3 100644 --- a/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcherBuilder.cs +++ b/src/Http/Routing/test/UnitTests/Matching/TreeRouterMatcherBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Internal; using Microsoft.AspNetCore.Routing.Template; @@ -99,13 +100,11 @@ namespace Microsoft.AspNetCore.Routing.Matching public async Task RouteAsync(RouteContext routeContext) { - var context = new EndpointSelectorContext(routeContext.HttpContext); - // This is needed due to a quirk of our tests - they reuse the endpoint feature. - context.Endpoint = null; + routeContext.HttpContext.SetEndpoint(null); - await _selector.SelectAsync(routeContext.HttpContext, context, new CandidateSet(_candidates, _values, _scores)); - if (context.Endpoint != null) + await _selector.SelectAsync(routeContext.HttpContext, new CandidateSet(_candidates, _values, _scores)); + if (routeContext.HttpContext.GetEndpoint() != null) { routeContext.Handler = (_) => Task.CompletedTask; } diff --git a/src/Http/Routing/test/UnitTests/TestObjects/TestMatcher.cs b/src/Http/Routing/test/UnitTests/TestObjects/TestMatcher.cs index 151ca9c67a..1ae4fa149f 100644 --- a/src/Http/Routing/test/UnitTests/TestObjects/TestMatcher.cs +++ b/src/Http/Routing/test/UnitTests/TestObjects/TestMatcher.cs @@ -1,8 +1,9 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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.Endpoints; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.Matching; @@ -17,12 +18,12 @@ namespace Microsoft.AspNetCore.Routing.TestObjects _isHandled = isHandled; } - public override Task MatchAsync(HttpContext httpContext, EndpointSelectorContext context) + public override Task MatchAsync(HttpContext httpContext) { if (_isHandled) { - context.RouteValues = new RouteValueDictionary(new { controller = "Home", action = "Index" }); - context.Endpoint = new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "Test endpoint"); + httpContext.Request.RouteValues = new RouteValueDictionary(new { controller = "Home", action = "Index" }); + httpContext.SetEndpoint(new Endpoint(TestConstants.EmptyRequestDelegate, EndpointMetadataCollection.Empty, "Test endpoint")); } return Task.CompletedTask; diff --git a/src/Mvc/Mvc.Core/src/Routing/ActionConstraintMatcherPolicy.cs b/src/Mvc/Mvc.Core/src/Routing/ActionConstraintMatcherPolicy.cs index a653898749..ee2f620912 100644 --- a/src/Mvc/Mvc.Core/src/Routing/ActionConstraintMatcherPolicy.cs +++ b/src/Mvc/Mvc.Core/src/Routing/ActionConstraintMatcherPolicy.cs @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing if (action?.ActionConstraints?.Count > 0 && HasSignificantActionConstraint(action)) { // We need to check for some specific action constraint implementations. - // We've implemented consumes, and HTTP method support inside endpoint routing, so + // We've implemented consumes, and HTTP method support inside endpoint routing, so // we don't need to run an 'action constraint phase' if those are the only constraints. return true; } @@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing } } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidateSet) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidateSet) { var finalMatches = EvaluateActionConstraints(httpContext, candidateSet); diff --git a/src/Mvc/Mvc.Core/src/Routing/ConsumesMatcherPolicy.cs b/src/Mvc/Mvc.Core/src/Routing/ConsumesMatcherPolicy.cs index dbe36b5d6b..5314d8ad87 100644 --- a/src/Mvc/Mvc.Core/src/Routing/ConsumesMatcherPolicy.cs +++ b/src/Mvc/Mvc.Core/src/Routing/ConsumesMatcherPolicy.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Matching; @@ -53,7 +54,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing return endpoints.Any(e => e.Metadata.GetMetadata()?.ContentTypes.Count > 0); } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { @@ -106,7 +107,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing } var contentType = httpContext.Request.ContentType; - var mediaType = string.IsNullOrEmpty(contentType) ? (MediaType?)null : new MediaType(contentType); + var mediaType = string.IsNullOrEmpty(contentType) ? (MediaType?)null : new MediaType(contentType); var matched = false; for (var j = 0; j < metadata.ContentTypes.Count; j++) @@ -145,7 +146,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing if (needs415Endpoint == true) { // We saw some endpoints coming in, and we eliminated them all. - context.Endpoint = CreateRejectionEndpoint(); + httpContext.SetEndpoint(CreateRejectionEndpoint()); } return Task.CompletedTask; @@ -220,7 +221,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var mediaType = new MediaType(contentType); // Example: 'application/json' is subset of 'application/*' - // + // // This means that when the request has content-type 'application/json' an endpoint // what consumes 'application/*' should match. if (edgeKey.IsSubsetOf(mediaType)) diff --git a/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs b/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs index 765ffac3ca..7ac5115056 100644 --- a/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs +++ b/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs @@ -60,13 +60,13 @@ namespace Microsoft.AspNetCore.Mvc.Routing return false; } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } - + if (candidates == null) { throw new ArgumentNullException(nameof(candidates)); @@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing // If there's no match this is a configuration error. We can't really check // during startup that the action you configured exists. throw new InvalidOperationException( - "Cannot find the fallback endpoint specified by route values: " + + "Cannot find the fallback endpoint specified by route values: " + "{ " + string.Join(", ", metadata.Values.Select(kvp => $"{kvp.Key}: {kvp.Value}")) + " }."); } diff --git a/src/Mvc/Mvc.Core/test/Routing/ConsumesMatcherPolicyTest.cs b/src/Mvc/Mvc.Core/test/Routing/ConsumesMatcherPolicyTest.cs index 564fa00bcf..bbb77a86c1 100644 --- a/src/Mvc/Mvc.Core/test/Routing/ConsumesMatcherPolicyTest.cs +++ b/src/Mvc/Mvc.Core/test/Routing/ConsumesMatcherPolicyTest.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Endpoints; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Matching; using Microsoft.AspNetCore.Routing.Patterns; @@ -326,13 +327,12 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext(); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -348,13 +348,12 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext(); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -370,13 +369,12 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext(); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -392,7 +390,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext() { Request = @@ -404,7 +401,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -420,7 +417,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext() { Request = @@ -432,7 +428,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -448,7 +444,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext() { Request = @@ -460,7 +455,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -476,7 +471,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext() { Request = @@ -488,7 +482,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -504,7 +498,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing }; var candidates = CreateCandidateSet(endpoints); - var context = new EndpointSelectorContext(); var httpContext = new DefaultHttpContext() { Request = @@ -516,7 +509,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.True(candidates.IsValidCandidate(0)); @@ -539,16 +532,15 @@ namespace Microsoft.AspNetCore.Mvc.Routing ContentType = "application/json", }, }; - var context = new EndpointSelectorContext(httpContext); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.False(candidates.IsValidCandidate(0)); - Assert.NotNull(context.Endpoint); + Assert.NotNull(httpContext.GetEndpoint()); } [Fact] @@ -569,16 +561,15 @@ namespace Microsoft.AspNetCore.Mvc.Routing ContentType = "application/json", }, }; - var context = new EndpointSelectorContext(httpContext); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.False(candidates.IsValidCandidate(0)); - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } [Fact] @@ -599,17 +590,16 @@ namespace Microsoft.AspNetCore.Mvc.Routing ContentType = "application/json", }, }; - var context = new EndpointSelectorContext(httpContext); var policy = CreatePolicy(); // Act - await policy.ApplyAsync(httpContext, context, candidates); + await policy.ApplyAsync(httpContext, candidates); // Assert Assert.False(candidates.IsValidCandidate(0)); Assert.True(candidates.IsValidCandidate(1)); - Assert.Null(context.Endpoint); + Assert.Null(httpContext.GetEndpoint()); } private static RouteEndpoint CreateEndpoint(string template, ConsumesMetadata consumesMetadata, params object[] more) diff --git a/src/Mvc/Mvc.RazorPages/src/Infrastructure/DynamicPageEndpointMatcherPolicy.cs b/src/Mvc/Mvc.RazorPages/src/Infrastructure/DynamicPageEndpointMatcherPolicy.cs index 8f8a4eedae..79853782b3 100644 --- a/src/Mvc/Mvc.RazorPages/src/Infrastructure/DynamicPageEndpointMatcherPolicy.cs +++ b/src/Mvc/Mvc.RazorPages/src/Infrastructure/DynamicPageEndpointMatcherPolicy.cs @@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure return false; } - public async Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { @@ -98,7 +98,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure // If there's no match this is a configuration error. We can't really check // during startup that the action you configured exists. throw new InvalidOperationException( - "Cannot find the fallback endpoint specified by route values: " + + "Cannot find the fallback endpoint specified by route values: " + "{ " + string.Join(", ", metadata.Values.Select(kvp => $"{kvp.Key}: {kvp.Value}")) + " }."); } @@ -106,7 +106,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure var compiled = await _loader.LoadAsync(endpoints[0].Metadata.GetMetadata()); var replacement = compiled.Endpoint; - + // We need to provide the route values associated with this endpoint, so that features // like URL generation work. var values = new RouteValueDictionary(metadata.Values); diff --git a/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageLoaderMatcherPolicy.cs b/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageLoaderMatcherPolicy.cs index d3beefd714..97951df719 100644 --- a/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageLoaderMatcherPolicy.cs +++ b/src/Mvc/Mvc.RazorPages/src/Infrastructure/PageLoaderMatcherPolicy.cs @@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure return false; } - public Task ApplyAsync(HttpContext httpContext, EndpointSelectorContext context, CandidateSet candidates) + public Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) { if (httpContext == null) { diff --git a/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageLoaderMatcherPolicyTest.cs b/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageLoaderMatcherPolicyTest.cs index f678690597..c193595923 100644 --- a/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageLoaderMatcherPolicyTest.cs +++ b/src/Mvc/Mvc.RazorPages/test/Infrastructure/PageLoaderMatcherPolicyTest.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure var policy = new PageLoaderMatcherPolicy(loader); // Act - await policy.ApplyAsync(new DefaultHttpContext(), new EndpointSelectorContext(), candidateSet); + await policy.ApplyAsync(new DefaultHttpContext(), candidateSet); // Assert Assert.Same(compiled.Endpoint, candidateSet[0].Endpoint); @@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure var policy = new PageLoaderMatcherPolicy(loader); // Act - var applyTask = policy.ApplyAsync(new DefaultHttpContext(), new EndpointSelectorContext(), candidateSet); + var applyTask = policy.ApplyAsync(new DefaultHttpContext(), candidateSet); tcs.SetResult(0); await applyTask; diff --git a/src/Mvc/Mvc/test/Routing/ActionConstraintMatcherPolicyTest.cs b/src/Mvc/Mvc/test/Routing/ActionConstraintMatcherPolicyTest.cs index 1da67ef346..ec95b2a212 100644 --- a/src/Mvc/Mvc/test/Routing/ActionConstraintMatcherPolicyTest.cs +++ b/src/Mvc/Mvc/test/Routing/ActionConstraintMatcherPolicyTest.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var selector = CreateSelector(actions); // Act - await selector.ApplyAsync(new DefaultHttpContext(), new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(new DefaultHttpContext(), candidateSet); // Assert Assert.True(candidateSet.IsValidCandidate(0)); @@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.True(candidateSet.IsValidCandidate(0)); @@ -102,7 +102,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.False(candidateSet.IsValidCandidate(0)); @@ -138,7 +138,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.False(candidateSet.IsValidCandidate(0)); @@ -173,7 +173,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.True(candidateSet.IsValidCandidate(0)); @@ -211,7 +211,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.False(candidateSet.IsValidCandidate(0)); @@ -246,7 +246,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.False(candidateSet.IsValidCandidate(0)); @@ -287,7 +287,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.True(candidateSet.IsValidCandidate(0)); @@ -328,7 +328,7 @@ namespace Microsoft.AspNetCore.Mvc.Routing var httpContext = CreateHttpContext("POST"); // Act - await selector.ApplyAsync(httpContext, new EndpointSelectorContext(), candidateSet); + await selector.ApplyAsync(httpContext, candidateSet); // Assert Assert.True(candidateSet.IsValidCandidate(0));