Make RouteOptions.EndpointDataSources internal (#9708)

This commit is contained in:
James Newton-King 2019-04-26 07:54:27 +12:00 committed by GitHub
parent 1845706a71
commit 8ce7bd171d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 7 deletions

View File

@ -319,7 +319,6 @@ namespace Microsoft.AspNetCore.Routing
public RouteOptions() { }
public bool AppendTrailingSlash { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public System.Collections.Generic.IDictionary<string, System.Type> ConstraintMap { get { throw null; } set { } }
public System.Collections.Generic.ICollection<Microsoft.AspNetCore.Routing.EndpointDataSource> EndpointDataSources { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
public bool LowercaseQueryStrings { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public bool LowercaseUrls { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public bool SuppressCheckForUnhandledSecurityMetadata { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }

View File

@ -3,13 +3,28 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Routing.Constraints;
namespace Microsoft.AspNetCore.Routing
{
public class RouteOptions
{
public ICollection<EndpointDataSource> EndpointDataSources { get; internal set; }
private IDictionary<string, Type> _constraintTypeMap = GetDefaultConstraintMap();
private ICollection<EndpointDataSource> _endpointDataSources;
/// <summary>
/// Gets a collection of <see cref="EndpointDataSource"/> instances configured with routing.
/// </summary>
internal ICollection<EndpointDataSource> EndpointDataSources
{
get
{
Debug.Assert(_endpointDataSources != null, "Endpoint data sources should have been set in DI.");
return _endpointDataSources;
}
set => _endpointDataSources = value;
}
/// <summary>
/// Gets or sets a value indicating whether all generated paths URLs are lower-case.
@ -48,8 +63,6 @@ namespace Microsoft.AspNetCore.Routing
/// </remarks>
public bool SuppressCheckForUnhandledSecurityMetadata { get; set; }
private IDictionary<string, Type> _constraintTypeMap = GetDefaultConstraintMap();
public IDictionary<string, Type> ConstraintMap
{
get

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@ -27,6 +28,37 @@ namespace Microsoft.AspNetCore.Routing.Tests
Assert.Equal("TestRouteConstraint", accessor.Value.ConstraintMap["foo"].Name);
}
[Fact]
public void EndpointDataSources_WithDependencyInjection_AddedDataSourcesAddedToEndpointDataSource()
{
// Arrange
var services = new ServiceCollection();
services.AddOptions();
services.AddRouting();
var serviceProvider = services.BuildServiceProvider();
var endpoint1 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty);
var endpoint2 = new Endpoint((c) => Task.CompletedTask, EndpointMetadataCollection.Empty, string.Empty);
var options = serviceProvider.GetRequiredService<IOptions<RouteOptions>>().Value;
var endpointDataSource = serviceProvider.GetRequiredService<EndpointDataSource>();
// Act 1
options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint1));
// Assert 1
var result = Assert.Single(endpointDataSource.Endpoints);
Assert.Same(endpoint1, result);
// Act 2
options.EndpointDataSources.Add(new DefaultEndpointDataSource(endpoint2));
// Assert 2
Assert.Collection(endpointDataSource.Endpoints,
ep => Assert.Same(endpoint1, ep),
ep => Assert.Same(endpoint2, ep));
}
private class TestRouteConstraint : IRouteConstraint
{
public TestRouteConstraint(string pattern)

View File

@ -55,10 +55,10 @@ namespace Microsoft.AspNetCore.Mvc.Core.Builder
template: "{controller=Home}/{action=Index}/{id?}");
});
var routeOptions = appBuilder.ApplicationServices
.GetRequiredService<IOptions<RouteOptions>>();
var endpointDataSource = appBuilder.ApplicationServices
.GetRequiredService<EndpointDataSource>();
Assert.Empty(routeOptions.Value.EndpointDataSources);
Assert.Empty(endpointDataSource.Endpoints);
}
[Fact]