Exemple #1
0
        public void GetService_GuardClauses()
        {
            // Arrange
            var config          = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            Assert.ThrowsArgumentNull(() => defaultServices.GetService(serviceType: null), "serviceType");
            Assert.ThrowsArgument(
                () => defaultServices.GetService(typeof(object)),
                "serviceType",
                "The service type Object is not supported.");
        }
Exemple #2
0
        public void GetService_CachesResultFromDependencyInjectionContainer()
        {
            // Arrange
            var config                 = new HttpConfiguration();
            var defaultServices        = new DefaultServices(config);
            var mockDependencyResolver = new Mock <IDependencyResolver>();

            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            defaultServices.GetService(typeof(IFilterProvider));
            defaultServices.GetService(typeof(IFilterProvider));

            // Assert
            mockDependencyResolver.Verify(dr => dr.GetService(typeof(IFilterProvider)), Times.Once());
        }
Exemple #3
0
        public void GetService_ReturnsFirstServiceInList()
        {
            // Arrange
            var config          = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);
            IEnumerable <object> servicesBefore = defaultServices.GetServices(typeof(IFilterProvider));

            // Act
            object service = defaultServices.GetService(typeof(IFilterProvider));

            // Assert
            Assert.Same(servicesBefore.First(), service);
        }
Exemple #4
0
        public void GetService_ReturnsNullWhenServiceListEmpty()
        {
            // Arrange
            var config          = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);

            defaultServices.Clear(typeof(IFilterProvider));

            // Act
            object service = defaultServices.GetService(typeof(IFilterProvider));

            // Assert
            Assert.Null(service);
        }
Exemple #5
0
        public void GetService_PrefersServiceInDependencyInjectionContainer()
        {
            // Arrange
            var config                 = new HttpConfiguration();
            var defaultServices        = new DefaultServices(config);
            var filterProvider         = new Mock <IFilterProvider>().Object;
            var mockDependencyResolver = new Mock <IDependencyResolver>();

            mockDependencyResolver.Setup(dr => dr.GetService(typeof(IFilterProvider))).Returns(filterProvider);
            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            object service = defaultServices.GetService(typeof(IFilterProvider));

            // Assert
            Assert.Same(filterProvider, service);
        }
Exemple #6
0
        public void Constructor_DefaultServicesInContainer()
        {
            // Arrange
            var config = new HttpConfiguration();

            // Act
            var defaultServices = new DefaultServices(config);

            // Assert
            Assert.Null(defaultServices.GetService(typeof(IDocumentationProvider)));
            Assert.Null(defaultServices.GetService(typeof(ITraceWriter)));

            Assert.IsType <DefaultActionValueBinder>(defaultServices.GetService(typeof(IActionValueBinder)));
            Assert.IsType <ApiExplorer>(defaultServices.GetService(typeof(IApiExplorer)));
            Assert.IsType <DefaultAssembliesResolver>(defaultServices.GetService(typeof(IAssembliesResolver)));
            Assert.IsType <DefaultBodyModelValidator>(defaultServices.GetService(typeof(IBodyModelValidator)));
            Assert.IsType <DefaultContentNegotiator>(defaultServices.GetService(typeof(IContentNegotiator)));
            Assert.IsType <ApiControllerActionInvoker>(defaultServices.GetService(typeof(IHttpActionInvoker)));
            Assert.IsType <ApiControllerActionSelector>(defaultServices.GetService(typeof(IHttpActionSelector)));
            Assert.IsType <DefaultHttpControllerActivator>(defaultServices.GetService(typeof(IHttpControllerActivator)));
            Assert.IsType <DefaultHttpControllerSelector>(defaultServices.GetService(typeof(IHttpControllerSelector)));
            Assert.IsType <DefaultHttpControllerTypeResolver>(defaultServices.GetService(typeof(IHttpControllerTypeResolver)));
            Assert.IsType <TraceManager>(defaultServices.GetService(typeof(ITraceManager)));
            Assert.IsType <DataAnnotationsModelMetadataProvider>(defaultServices.GetService(typeof(ModelMetadataProvider)));

            object[] filterProviders = defaultServices.GetServices(typeof(IFilterProvider)).ToArray();
            Assert.Equal(2, filterProviders.Length);
            Assert.IsType <ConfigurationFilterProvider>(filterProviders[0]);
            Assert.IsType <ActionDescriptorFilterProvider>(filterProviders[1]);

            object[] modelBinderProviders = defaultServices.GetServices(typeof(ModelBinderProvider)).ToArray();
            Assert.Equal(9, modelBinderProviders.Length);
            Assert.IsType <TypeConverterModelBinderProvider>(modelBinderProviders[0]);
            Assert.IsType <TypeMatchModelBinderProvider>(modelBinderProviders[1]);
            Assert.IsType <BinaryDataModelBinderProvider>(modelBinderProviders[2]);
            Assert.IsType <KeyValuePairModelBinderProvider>(modelBinderProviders[3]);
            Assert.IsType <ComplexModelDtoModelBinderProvider>(modelBinderProviders[4]);
            Assert.IsType <ArrayModelBinderProvider>(modelBinderProviders[5]);
            Assert.IsType <DictionaryModelBinderProvider>(modelBinderProviders[6]);
            Assert.IsType <CollectionModelBinderProvider>(modelBinderProviders[7]);
            Assert.IsType <MutableObjectModelBinderProvider>(modelBinderProviders[8]);

            object[] validatorProviders = defaultServices.GetServices(typeof(ModelValidatorProvider)).ToArray();
            Assert.Equal(2, validatorProviders.Length);
            Assert.IsType <DataAnnotationsModelValidatorProvider>(validatorProviders[0]);
            Assert.IsType <DataMemberModelValidatorProvider>(validatorProviders[1]);

            object[] valueProviderFactories = defaultServices.GetServices(typeof(ValueProviderFactory)).ToArray();
            Assert.Equal(2, valueProviderFactories.Length);
            Assert.IsType <QueryStringValueProviderFactory>(valueProviderFactories[0]);
            Assert.IsType <RouteDataValueProviderFactory>(valueProviderFactories[1]);
        }
        public void GetService_CachesResultFromDependencyInjectionContainer()
        {
            // Arrange
            var config = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);
            var mockDependencyResolver = new Mock<IDependencyResolver>();
            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            defaultServices.GetService(typeof(IActionValueBinder));
            defaultServices.GetService(typeof(IActionValueBinder));

            // Assert
            mockDependencyResolver.Verify(dr => dr.GetService(typeof(IActionValueBinder)), Times.Once());
        }
        public void Constructor_DefaultServicesInContainer()
        {
            // Arrange
            var config = new HttpConfiguration();

            // Act
            var defaultServices = new DefaultServices(config);

            // Assert
            Assert.Null(defaultServices.GetService(typeof(IDocumentationProvider)));
            Assert.Null(defaultServices.GetService(typeof(ITraceWriter)));

            Assert.IsType<DefaultActionValueBinder>(defaultServices.GetService(typeof(IActionValueBinder)));
            Assert.IsType<ApiExplorer>(defaultServices.GetService(typeof(IApiExplorer)));
            Assert.IsType<DefaultAssembliesResolver>(defaultServices.GetService(typeof(IAssembliesResolver)));
            Assert.IsType<DefaultBodyModelValidator>(defaultServices.GetService(typeof(IBodyModelValidator)));
            Assert.IsType<DefaultContentNegotiator>(defaultServices.GetService(typeof(IContentNegotiator)));
            Assert.IsType<ApiControllerActionInvoker>(defaultServices.GetService(typeof(IHttpActionInvoker)));
            Assert.IsType<ApiControllerActionSelector>(defaultServices.GetService(typeof(IHttpActionSelector)));
            Assert.IsType<DefaultHttpControllerActivator>(defaultServices.GetService(typeof(IHttpControllerActivator)));
            Assert.IsType<DefaultHttpControllerSelector>(defaultServices.GetService(typeof(IHttpControllerSelector)));
            Assert.IsType<DefaultHttpControllerTypeResolver>(defaultServices.GetService(typeof(IHttpControllerTypeResolver)));
            Assert.IsType<TraceManager>(defaultServices.GetService(typeof(ITraceManager)));
            Assert.IsType<DataAnnotationsModelMetadataProvider>(defaultServices.GetService(typeof(ModelMetadataProvider)));
            Assert.IsType<ModelValidatorCache>(defaultServices.GetService(typeof(IModelValidatorCache)));

            object[] filterProviders = defaultServices.GetServices(typeof(IFilterProvider)).ToArray();
            Assert.Equal(2, filterProviders.Length);
            Assert.IsType<ConfigurationFilterProvider>(filterProviders[0]);
            Assert.IsType<ActionDescriptorFilterProvider>(filterProviders[1]);

            object[] modelBinderProviders = defaultServices.GetServices(typeof(ModelBinderProvider)).ToArray();
            Assert.Equal(8, modelBinderProviders.Length);
            Assert.IsType<TypeConverterModelBinderProvider>(modelBinderProviders[0]);
            Assert.IsType<TypeMatchModelBinderProvider>(modelBinderProviders[1]);
            Assert.IsType<KeyValuePairModelBinderProvider>(modelBinderProviders[2]);
            Assert.IsType<ComplexModelDtoModelBinderProvider>(modelBinderProviders[3]);
            Assert.IsType<ArrayModelBinderProvider>(modelBinderProviders[4]);
            Assert.IsType<DictionaryModelBinderProvider>(modelBinderProviders[5]);
            Assert.IsType<CollectionModelBinderProvider>(modelBinderProviders[6]);
            Assert.IsType<MutableObjectModelBinderProvider>(modelBinderProviders[7]);

            object[] validatorProviders = defaultServices.GetServices(typeof(ModelValidatorProvider)).ToArray();
            Assert.Equal(2, validatorProviders.Length);
            Assert.IsType<DataAnnotationsModelValidatorProvider>(validatorProviders[0]);
            Assert.IsType<DataMemberModelValidatorProvider>(validatorProviders[1]);

            object[] valueProviderFactories = defaultServices.GetServices(typeof(ValueProviderFactory)).ToArray();
            Assert.Equal(2, valueProviderFactories.Length);
            Assert.IsType<QueryStringValueProviderFactory>(valueProviderFactories[0]);
            Assert.IsType<RouteDataValueProviderFactory>(valueProviderFactories[1]);
        }
        public void GetService_PrefersServiceInDependencyInjectionContainer()
        {
            // Arrange
            var config = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);
            var filterProvider = new Mock<IActionValueBinder>().Object;
            var mockDependencyResolver = new Mock<IDependencyResolver>();
            mockDependencyResolver.Setup(dr => dr.GetService(typeof(IActionValueBinder))).Returns(filterProvider);
            config.DependencyResolver = mockDependencyResolver.Object;

            // Act
            object service = defaultServices.GetService(typeof(IActionValueBinder));

            // Assert
            Assert.Same(filterProvider, service);
        }
        public void GetService_ReturnsNullWhenServiceListEmpty()
        {
            // Arrange
            var config = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);
            defaultServices.Clear(typeof(IActionValueBinder));

            // Act
            object service = defaultServices.GetService(typeof(IActionValueBinder));

            // Assert
            Assert.Null(service);
        }
        public void GetService_GuardClauses()
        {
            // Arrange
            var config = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);

            // Act & assert
            Assert.ThrowsArgumentNull(() => defaultServices.GetService(serviceType: null), "serviceType");
            Assert.ThrowsArgument(
                () => defaultServices.GetService(typeof(object)),
                "serviceType",
                "The service type Object is not supported.");
        }
        public void Class_IsDefaultIAssembliesResolver()
        {
            var serviceResolver = new DefaultServices(new HttpConfiguration());

            Assert.IsType<DefaultAssembliesResolver>(serviceResolver.GetService(typeof(IAssembliesResolver)));
        }
        public void GetService_ReturnsFirstServiceInList()
        {
            // Arrange
            var config = new HttpConfiguration();
            var defaultServices = new DefaultServices(config);
            IEnumerable<object> servicesBefore = defaultServices.GetServices(typeof(IFilterProvider));

            // Act
            object service = defaultServices.GetService(typeof(IFilterProvider));

            // Assert
            Assert.Same(servicesBefore.First(), service);
        }