public DefaultParameterPolicyFactory( IOptions <RouteOptions> options, IServiceProvider serviceProvider) { _options = options.Value; _serviceProvider = serviceProvider; }
private static RouteContext CreateRouteContext( string requestPath, ILoggerFactory loggerFactory = null, RouteOptions options = null) { if (loggerFactory == null) { loggerFactory = NullLoggerFactory.Instance; } if (options == null) { options = new RouteOptions(); } var request = new Mock <HttpRequest>(MockBehavior.Strict); request.SetupGet(r => r.Path).Returns(requestPath); var optionsAccessor = new Mock <IOptions <RouteOptions> >(MockBehavior.Strict); optionsAccessor.SetupGet(o => o.Value).Returns(options); var context = new Mock <HttpContext>(MockBehavior.Strict); context.Setup(m => m.RequestServices.GetService(typeof(ILoggerFactory))) .Returns(loggerFactory); context.Setup(m => m.RequestServices.GetService(typeof(IOptions <RouteOptions>))) .Returns(optionsAccessor.Object); context.SetupGet(c => c.Request).Returns(request.Object); return(new RouteContext(context.Object)); }
private void EnsureOptions(HttpContext context) { if (_options == null) { _options = context.RequestServices.GetRequiredService <IOptions <RouteOptions> >().Value; } }
public EndpointMiddleware( ILogger <EndpointMiddleware> logger, RequestDelegate next, IOptions <RouteOptions> routeOptions) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _next = next ?? throw new ArgumentNullException(nameof(next)); _routeOptions = routeOptions?.Value ?? throw new ArgumentNullException(nameof(routeOptions)); }
public DefaultLinkGenerator( MatchProcessorFactory matchProcessorFactory, ObjectPool <UriBuildingContext> uriBuildingContextPool, IOptions <RouteOptions> routeOptions, ILogger <DefaultLinkGenerator> logger) { _matchProcessorFactory = matchProcessorFactory; _uriBuildingContextPool = uriBuildingContextPool; _options = routeOptions.Value; _logger = logger; }
public DefaultLinkGenerator( ParameterPolicyFactory parameterPolicyFactory, ObjectPool <UriBuildingContext> uriBuildingContextPool, IOptions <RouteOptions> routeOptions, ILogger <DefaultLinkGenerator> logger, IServiceProvider serviceProvider) { _parameterPolicyFactory = parameterPolicyFactory; _uriBuildingContextPool = uriBuildingContextPool; _options = routeOptions.Value; _logger = logger; _serviceProvider = serviceProvider; }
private DefaultParameterPolicyFactory GetParameterPolicyFactory( RouteOptions options = null, ServiceCollection services = null) { if (options == null) { options = new RouteOptions(); } if (services == null) { services = new ServiceCollection(); } return(new DefaultParameterPolicyFactory( Options.Create(options), services.BuildServiceProvider())); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicy_Optional() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("customParameterPolicy", typeof(CustomParameterPolicy)); var services = new ServiceCollection(); services.AddTransient <CustomParameterPolicy>(); var factory = GetParameterPolicyFactory(options, services); // Act var parameterPolicy = factory.Create(RoutePatternFactory.ParameterPart("id", @default: null, RoutePatternParameterKind.Optional), "customParameterPolicy"); // Assert Assert.IsType <CustomParameterPolicy>(parameterPolicy); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicyWithArgumentAndUnresolvedServices_Throw() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("customConstraintPolicy", typeof(CustomParameterPolicyWithArguments)); var services = new ServiceCollection(); var factory = GetParameterPolicyFactory(options, services); // Act var exception = Assert.Throws <RouteCreationException>( () => factory.Create(RoutePatternFactory.ParameterPart("id"), "customConstraintPolicy(20)")); // Assert var inner = Assert.IsType <InvalidOperationException>(exception.InnerException); Assert.Equal($"No service for type '{typeof(ITestService).FullName}' has been registered.", inner.Message); }
public void Create_ThrowsException_OnInvalidType() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("bad", typeof(string)); var services = new ServiceCollection(); var factory = GetParameterPolicyFactory(options, services); // Act var exception = Assert.Throws <RouteCreationException>( () => factory.Create(RoutePatternFactory.ParameterPart("id"), @"bad")); // Assert Assert.Equal( $"The constraint type '{typeof(string)}' which is mapped to constraint key 'bad' must implement the '{nameof(IParameterPolicy)}' interface.", exception.Message); }
public void GetPathByAddress_WithParameterTransformer() { // Arrange var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller:slugify}/{action}/{id}", metadata: new object[] { new IntMetadata(1), }); var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller:slugify}/{action}/{id?}", metadata: new object[] { new IntMetadata(1), }); var routeOptions = new RouteOptions(); routeOptions.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); var linkGenerator = CreateLinkGenerator(routeOptions: routeOptions, configureServices: null, endpoint1, endpoint2); // Act var path = linkGenerator.GetPathByAddress( 1, values: new RouteValueDictionary(new { controller = "TestController", action = "Index", })); // Assert Assert.Equal("/test-controller/Index", path); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicyWithAmbigiousMatchingCtors() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("customConstraintPolicy", typeof(CustomParameterPolicyWithAmbigiousMultpleCtors)); var services = new ServiceCollection(); services.AddTransient <ITestService, TestService>(); var factory = GetParameterPolicyFactory(options, services); // Act var exception = Assert.Throws <RouteCreationException>( () => factory.Create(RoutePatternFactory.ParameterPart("id"), "customConstraintPolicy(1)")); // Assert Assert.Equal($"The constructor to use for activating the constraint type '{nameof(CustomParameterPolicyWithAmbigiousMultpleCtors)}' is ambiguous. " + "Multiple constructors were found with the following number of parameters: 2.", exception.Message); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicyWithMultipleMatchingCtors() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("customConstraintPolicy", typeof(CustomParameterPolicyWithMultpleCtors)); var services = new ServiceCollection(); services.AddTransient <ITestService, TestService>(); var factory = GetParameterPolicyFactory(options, services); // Act var parameterPolicy = factory.Create(RoutePatternFactory.ParameterPart("id"), "customConstraintPolicy(1)"); // Assert var constraint = Assert.IsType <CustomParameterPolicyWithMultpleCtors>(parameterPolicy); Assert.NotNull(constraint.TestService); Assert.Equal(1, constraint.Count); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicyWithSingleArgumentAndServiceArgument() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("regex-service", typeof(RegexInlineRouteConstraintWithService)); var services = new ServiceCollection(); services.AddTransient <ITestService, TestService>(); var factory = GetParameterPolicyFactory(options, services); // Act var parameterPolicy = factory.Create(RoutePatternFactory.ParameterPart("id"), @"regex-service(\\d{1,2})"); // Assert var constraint = Assert.IsType <RegexInlineRouteConstraintWithService>(parameterPolicy); Assert.NotNull(constraint.TestService); Assert.Equal("\\\\d{1,2}", constraint.Constraint.ToString()); }
public void Create_CreatesParameterPolicy_FromConstraintText_AndParameterPolicyWithOnlyServiceArguments() { // Arrange var options = new RouteOptions(); options.ConstraintMap.Add("customConstraintPolicy", typeof(CustomParameterPolicyWithOnlyServiceArguments)); var services = new ServiceCollection(); services.AddTransient <ITestService, TestService>(); var factory = GetParameterPolicyFactory(options, services); // Act var parameterPolicy = factory.Create(RoutePatternFactory.ParameterPart("id"), "customConstraintPolicy"); // Assert var constraint = Assert.IsType <CustomParameterPolicyWithOnlyServiceArguments>(parameterPolicy); Assert.NotNull(constraint.TestService1); Assert.NotNull(constraint.TestService2); }
public void GetLink_ParameterTransformer_ForQueryString() { // Arrange var endpoint = EndpointFactory.CreateRouteEndpoint("{controller:upper-case}/{name}", policies: new { c = new UpperCaseParameterTransform(), }); var routeOptions = new RouteOptions(); routeOptions.ConstraintMap["upper-case"] = typeof(UpperCaseParameterTransform); Action <IServiceCollection> configure = (s) => { s.AddSingleton(typeof(UpperCaseParameterTransform), new UpperCaseParameterTransform()); }; var linkGenerator = CreateLinkGenerator(routeOptions, configure, endpoint); // Act var link = linkGenerator.GetPathByRouteValues(routeName: null, new { controller = "Home", name = "Test", c = "hithere", }); // Assert Assert.Equal("/HOME/Test?c=HITHERE", link); }
private protected DefaultLinkGenerator CreateLinkGenerator(RouteOptions routeOptions, params Endpoint[] endpoints) { return(CreateLinkGenerator(routeOptions, configureServices: null, endpoints)); }