Beispiel #1
0
        public void ModelBinderCollection_AddingNewModelBinder_AddsSupportForNewType()
        {
            var mock = new Mock <IQueryModelBinder>();

            mock.SetupGet(x => x.SupportedTypes).Returns(new [] { typeof(ModelBinderCollectionTests) });

            var collection = new ModelBinderCollection(new JsonSerializer(), Fakes.FakeServiceProvider.GetServiceProvider(), new Fakes.FakeDefaultLiteApiOptionsRetriever());

            collection.AddAdditionalQueryModelBinder(mock.Object);
            var containsCustomType = collection.GetSupportedTypesFromUrl().Contains(typeof(ModelBinderCollectionTests));

            Assert.True(containsCustomType);
        }
Beispiel #2
0
        private void Initialize(IServiceProvider services)
        {
            _logger.LogInformation("LiteApi middleware initialization started");
            Options.InternalServiceResolver.RegisterInstance <IAuthorizationPolicyStore>(Options.AuthorizationPolicyStore);

            IControllerDiscoverer ctrlDiscoverer = Options.InternalServiceResolver.GetControllerDiscoverer();

            List <ControllerContext> ctrlContexts = new List <ControllerContext>();

            foreach (var assembly in Options.ControllerAssemblies)
            {
                ctrlContexts.AddRange(ctrlDiscoverer.GetControllers(assembly));
            }

            var actions = ctrlContexts.SelectMany(x => x.Actions).ToArray();

            IControllerBuilder    ctrlBuilder = Options.InternalServiceResolver.GetControllerBuilder();
            ModelBinderCollection modelBinder = new ModelBinderCollection(Options.InternalServiceResolver.GetJsonSerializer(), services);

            foreach (IQueryModelBinder qmb in Options.AdditionalQueryModelBinders)
            {
                modelBinder.AddAdditionalQueryModelBinder(qmb);
            }

            var authPolicyStore             = Options.AuthorizationPolicyStore;
            IControllersValidator validator = Options.InternalServiceResolver.GetControllerValidator();
            var errors = validator.GetValidationErrors(ctrlContexts.ToArray()).ToArray();

            if (errors.Any())
            {
                _logger.LogError("One or more errors occurred while initializing LiteApi middleware. Check next log entry/entries.");
                foreach (var error in errors)
                {
                    _logger.LogError(error);
                }
                string allErrors = "\n\n --------- \n\n" + string.Join("\n\n --------- \n\n", errors);
                throw new LiteApiRegistrationException($"Failed to initialize {nameof(LiteApiMiddleware)}, see property Errors, log if enabled, or check erros listed below." + allErrors, errors);
            }

            Func <Type, bool> isRegistered = (type) => Options.InternalServiceResolver.IsServiceRegistered(type);

            if (!isRegistered(typeof(IPathResolver)))
            {
                Options.InternalServiceResolver.RegisterInstance <IPathResolver>(new PathResolver(ctrlContexts.ToArray()));
            }

            if (!isRegistered(typeof(IModelBinder)))
            {
                Options.InternalServiceResolver.RegisterInstance <IModelBinder>(modelBinder);
            }
        }
Beispiel #3
0
        public void ModelBinderCollectionAddAdditionalQueryModelBinder_NullQueryModelBinder_ThrowsException()
        {
            bool error      = false;
            var  collection = new ModelBinderCollection(new JsonSerializer(), Fakes.FakeServiceProvider.GetServiceProvider(), new Fakes.FakeDefaultLiteApiOptionsRetriever());

            try
            {
                collection.AddAdditionalQueryModelBinder(null);
            }
            catch (ArgumentNullException)
            {
                error = true;
            }
            Assert.True(error);
        }
        public void ModelBinderCollectionAddAdditionalQueryModelBinder_NullQueryModelBinder_ThrowsException()
        {
            bool error      = false;
            var  collection = new ModelBinderCollection(new JsonSerializer(), new Moq.Mock <IServiceProvider>().Object);

            try
            {
                collection.AddAdditionalQueryModelBinder(null);
            }
            catch (ArgumentNullException)
            {
                error = true;
            }
            Assert.True(error);
        }