Beispiel #1
0
        public void add_api_versioning_should_configure_mvc_with_custom_options()
        {
            // arrange
            var services     = new ServiceCollection();
            var mvcOptions   = new MvcOptions();
            var routeOptions = new RouteOptions();

            services.AddMvc();
            services.AddApiVersioning(
                o =>
            {
                o.ReportApiVersions  = true;
                o.ApiVersionReader   = ApiVersionReader.Combine(new QueryStringApiVersionReader(), new HeaderApiVersionReader("api-version"));
                o.ApiVersionSelector = new ConstantApiVersionSelector(new ApiVersion(DateTime.Today));
            });

            var serviceProvider    = services.BuildServiceProvider();
            var mvcConfiguration   = serviceProvider.GetRequiredService <IConfigureOptions <MvcOptions> >();
            var routeConfiguration = serviceProvider.GetRequiredService <IConfigureOptions <RouteOptions> >();

            // act
            mvcConfiguration.Configure(mvcOptions);
            routeConfiguration.Configure(routeOptions);

            // assert
            services.Single(sd => sd.ServiceType == typeof(IApiVersionReader)).ImplementationInstance.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IApiVersionSelector)).ImplementationInstance.Should().BeOfType <ConstantApiVersionSelector>();
            services.Single(sd => sd.ServiceType == typeof(IActionSelector)).ImplementationType.Should().Be(typeof(ApiVersionActionSelector));
            mvcOptions.Filters.OfType <ServiceFilterAttribute>().Single().ServiceType.Should().Be(typeof(ReportApiVersionsAttribute));
            mvcOptions.Conventions.Single().Should().BeOfType <ApiVersionConvention>();
            routeOptions.ConstraintMap["apiVersion"].Should().Be(typeof(ApiVersionRouteConstraint));
        }
Beispiel #2
0
        public void add_api_versioning_should_configure_mvc_with_custom_options()
        {
            // arrange
            var services = new ServiceCollection();

            // act
            services.AddApiVersioning(
                options =>
            {
                options.ReportApiVersions  = true;
                options.ApiVersionReader   = ApiVersionReader.Combine(new QueryStringApiVersionReader(), new HeaderApiVersionReader("api-version"));
                options.ApiVersionSelector = new ConstantApiVersionSelector(new ApiVersion(DateTime.Today));
            });

            // assert
            services.Single(sd => sd.ServiceType == typeof(IApiVersionReader)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IApiVersionSelector)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IErrorResponseProvider)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IActionSelector)).ImplementationType.Should().Be(typeof(ApiVersionActionSelector));
            services.Single(sd => sd.ServiceType == typeof(IApiVersionRoutePolicy)).ImplementationType.Should().Be(typeof(DefaultApiVersionRoutePolicy));
            services.Single(sd => sd.ServiceType == typeof(IApiControllerFilter)).ImplementationType.Should().Be(typeof(DefaultApiControllerFilter));
            services.Single(sd => sd.ServiceType == typeof(ReportApiVersionsAttribute)).ImplementationType.Should().Be(typeof(ReportApiVersionsAttribute));
            services.Single(sd => sd.ServiceType == typeof(IReportApiVersions)).ImplementationFactory.Should().NotBeNull();
            services.Any(sd => sd.ServiceType == typeof(IPostConfigureOptions <MvcOptions>) && sd.ImplementationType == typeof(ApiVersioningMvcOptionsSetup)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IPostConfigureOptions <RouteOptions>) && sd.ImplementationType == typeof(ApiVersioningRouteOptionsSetup)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IApplicationModelProvider) && sd.ImplementationType == typeof(ApiVersioningApplicationModelProvider)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IActionDescriptorProvider) && sd.ImplementationType == typeof(ApiVersionCollator)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IApiControllerSpecification) && sd.ImplementationType == typeof(ApiBehaviorSpecification)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(MatcherPolicy) && sd.ImplementationType == typeof(ApiVersionMatcherPolicy)).Should().BeTrue();
        }
Beispiel #3
0
        public void add_api_versioning_should_configure_mvc_with_default_options()
        {
            // arrange
            var services     = new ServiceCollection();
            var mvcOptions   = new MvcOptions();
            var routeOptions = new RouteOptions();

            services.AddMvc();
            services.AddApiVersioning();

            var serviceProvider    = services.BuildServiceProvider();
            var mvcConfiguration   = serviceProvider.GetRequiredService <IConfigureOptions <MvcOptions> >();
            var routeConfiguration = serviceProvider.GetRequiredService <IConfigureOptions <RouteOptions> >();

            // act
            mvcConfiguration.Configure(mvcOptions);
            routeConfiguration.Configure(routeOptions);

            // assert
            services.Single(sd => sd.ServiceType == typeof(IApiVersionReader)).ImplementationInstance.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IApiVersionSelector)).ImplementationInstance.Should().BeOfType <DefaultApiVersionSelector>();
            services.Single(sd => sd.ServiceType == typeof(IActionSelector)).ImplementationType.Should().Be(typeof(ApiVersionActionSelector));
            mvcOptions.Conventions.Single().Should().BeOfType <ApiVersionConvention>();
            routeOptions.ConstraintMap["apiVersion"].Should().Be(typeof(ApiVersionRouteConstraint));
        }
Beispiel #4
0
        public void add_api_versioning_should_configure_mvc_with_default_options()
        {
            // arrange
            var services = new ServiceCollection();

            services.AddMvcCore();

            // act
            services.AddApiVersioning();

            // assert
            services.Single(sd => sd.ServiceType == typeof(IApiVersionReader)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IApiVersionSelector)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IErrorResponseProvider)).ImplementationFactory.Should().NotBeNull();
            services.Single(sd => sd.ServiceType == typeof(IActionSelector)).ImplementationType.Should().Be(typeof(ApiVersionActionSelector));
            services.Single(sd => sd.ServiceType == typeof(IApiVersionRoutePolicy)).ImplementationType.Should().Be(typeof(DefaultApiVersionRoutePolicy));
            services.Single(sd => sd.ServiceType == typeof(IApiControllerFilter)).ImplementationType.Should().Be(typeof(DefaultApiControllerFilter));
            services.Single(sd => sd.ServiceType == typeof(ReportApiVersionsAttribute)).ImplementationType.Should().Be(typeof(ReportApiVersionsAttribute));
            services.Single(sd => sd.ServiceType == typeof(IReportApiVersions)).ImplementationFactory.Should().NotBeNull();
            services.Any(sd => sd.ServiceType == typeof(IPostConfigureOptions <MvcOptions>) && sd.ImplementationType == typeof(ApiVersioningMvcOptionsSetup)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IPostConfigureOptions <RouteOptions>) && sd.ImplementationType == typeof(ApiVersioningRouteOptionsSetup)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IApplicationModelProvider) && sd.ImplementationType == typeof(ApiVersioningApplicationModelProvider)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IActionDescriptorProvider) && sd.ImplementationType == typeof(ApiVersionCollator)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(IApiControllerSpecification) && sd.ImplementationType == typeof(ApiBehaviorSpecification)).Should().BeTrue();
            services.Any(sd => sd.ServiceType == typeof(MatcherPolicy) && sd.ImplementationType == typeof(ApiVersionMatcherPolicy)).Should().BeTrue();
        }