示例#1
0
        public void GenericControllerTypeProvisionValidatedCorrectly()
        {
            foreach (var @case in new[] {
                new { callback = _provideGenericControllerType, shouldFail = false },
                new { callback = _provideGenericControllerType2, shouldFail = false },
                new { callback = _provideGenericControllerTypeConstructed, shouldFail = true },
                new { callback = _provideGenericControllerTypeExtended, shouldFail = false },
                new { callback = _provideGenericControllerTypeExtendedConstructed, shouldFail = true },
                new { callback = _provideNonGenericControllerType, shouldFail = true },
                new { callback = _provideGenericControllerInterface, shouldFail = true }
            })
            {
                var services          = GetServiceCollection();
                var featureProvider   = new MediatrMvcFeatureProvider(services, @case.callback);
                var controllerFeature = new ControllerFeature();

                if (@case.shouldFail)
                {
                    Assert.ThrowsException <InvalidTypeException>(() => featureProvider.PopulateFeature(null, controllerFeature));
                }
                else
                {
                    featureProvider.PopulateFeature(null, controllerFeature);
                    Assert.AreEqual(5, controllerFeature.Controllers.Count);
                }
            }
        }
示例#2
0
        public void GenericControllersConstructionFailsGracefully()
        {
            foreach (var @case in new[] {
                new { callback = _provideGenericControllerTypeSingleParam, expectedType = typeof(MediatrMvcGenericController <,>) },
                new { callback = _provideGenericControllerTypeBad, expectedType = typeof(RevertedParamsMediatrMvcGenericController <,>) }
            })
            {
                var services          = GetServiceCollection();
                var featureProvider   = new MediatrMvcFeatureProvider(services, @case.callback);
                var controllerFeature = new ControllerFeature();

                Assert.ThrowsException <InvalidTypeException>(() => featureProvider.PopulateFeature(null, controllerFeature));
            }
        }
示例#3
0
        public void GenericControllersConstructedCorrectlyForIRequest()
        {
            var services = new ServiceCollection();

            services.AddTransient <IRequestHandler <GetTestDataRequest5, Unit>, GetTestDataRequest5Handler>();

            var featureProvider   = new MediatrMvcFeatureProvider(services, _provideGenericControllerTypeSingleParam);
            var controllerFeature = new ControllerFeature();

            featureProvider.PopulateFeature(null, controllerFeature);
            Assert.AreEqual(1, controllerFeature.Controllers.Count);

            Assert.IsTrue(controllerFeature.Controllers.All(c =>
                                                            c.IsConstructedGenericType && c.GetGenericTypeDefinition() == typeof(SingleParamMediatrMvcGenericController <>)));
        }
示例#4
0
        public void HandledRequestDiscoveryDisabledWhenConfigured()
        {
            foreach (var @case in new[]
            {
                new { controllerType = typeof(ControllerWithHandledRequest), requestToBeSkipped = typeof(GetTestDataRequest3) },
                new { controllerType = typeof(ControllerWithHandledRequestAttr), requestToBeSkipped = typeof(GetTestDataRequest2) }
            })
            {
                var services          = GetServiceCollection();
                var featureProvider   = new MediatrMvcFeatureProvider(services, applySettings: settings => settings.DisableHandledRequestDiscovery());
                var controllerFeature = new ControllerFeature();

                controllerFeature.Controllers.Add(@case.controllerType.GetTypeInfo());

                featureProvider.PopulateFeature(null, controllerFeature);
                Assert.AreEqual(6, controllerFeature.Controllers.Count);
            }
        }
示例#5
0
        public void HandledRequestDiscoveryEnabledByDefault()
        {
            foreach (var @case in new[] {
                new { controllerType = typeof(ControllerWithHandledRequest), requestToBeSkipped = typeof(GetTestDataRequest3) },
                new { controllerType = typeof(ControllerWithHandledRequestAttr), requestToBeSkipped = typeof(GetTestDataRequest2) }
            })
            {
                var services          = GetServiceCollection();
                var featureProvider   = new MediatrMvcFeatureProvider(services);
                var controllerFeature = new ControllerFeature();

                controllerFeature.Controllers.Add(@case.controllerType.GetTypeInfo());

                featureProvider.PopulateFeature(null, controllerFeature);

                Assert.AreEqual(5, controllerFeature.Controllers.Count);
                Assert.IsTrue(controllerFeature.Controllers.All(c => !c.IsGenericType || c.GenericTypeArguments[0] != @case.requestToBeSkipped));
            }
        }
示例#6
0
        public void GenericControllersConstructedCorrectly()
        {
            foreach (var @case in new[] {
                new { callback = _provideGenericControllerType, expectedType = typeof(MediatrMvcGenericController <,>) },
                new { callback = _provideGenericControllerType2, expectedType = typeof(MediatrMvcGenericController <,>) },
                new { callback = _provideGenericControllerTypeExtended, expectedType = typeof(ExtendedMediatrMvcGenericController <,>) },
                //new { callback = _provideGenericControllerTypeReverted, expectedType = typeof(RevertedParamsMediatrMvcGenericController<,>) }
            })
            {
                var services          = GetServiceCollection();
                var featureProvider   = new MediatrMvcFeatureProvider(services, @case.callback);
                var controllerFeature = new ControllerFeature();

                featureProvider.PopulateFeature(null, controllerFeature);
                Assert.AreEqual(5, controllerFeature.Controllers.Count);

                Assert.IsTrue(controllerFeature.Controllers.All(c =>
                                                                c.IsConstructedGenericType && c.GetGenericTypeDefinition() == @case.expectedType));

                Assert.IsTrue(controllerFeature.Controllers.Any(c =>
                                                                c.GenericTypeArguments[0] == typeof(GetTestDataRequest) &&
                                                                c.GenericTypeArguments[1] == typeof(string)));

                Assert.IsTrue(controllerFeature.Controllers.Any(c =>
                                                                c.GenericTypeArguments[0] == typeof(GetTestDataRequest2) &&
                                                                c.GenericTypeArguments[1] == typeof(int)));

                Assert.IsTrue(controllerFeature.Controllers.Any(c =>
                                                                c.GenericTypeArguments[0] == typeof(GetTestDataRequest3) &&
                                                                c.GenericTypeArguments[1] == typeof(object)));

                Assert.IsTrue(controllerFeature.Controllers.Any(c =>
                                                                c.GenericTypeArguments[0] == typeof(GetTestDataRequest4) &&
                                                                c.GenericTypeArguments[1] == typeof(Unit)));

                Assert.IsTrue(controllerFeature.Controllers.Any(c =>
                                                                c.GenericTypeArguments[0] == typeof(GetTestDataRequest5) &&
                                                                c.GenericTypeArguments[1] == typeof(Unit)));
            }
        }