Exemplo n.º 1
0
 private void EnsureOptions(ProtoContext context)
 {
     if (_options == null)
     {
         _options = context.RequestServices.GetRequiredService <IOptions <RouteOptions> >().Value;
     }
 }
 public DefaultParameterPolicyFactory(
     IOptions <RouteOptions> options,
     IServiceProvider serviceProvider)
 {
     _options         = options.Value;
     _serviceProvider = serviceProvider;
 }
Exemplo n.º 3
0
 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));
 }
        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 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_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_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_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);
        }