public void CreateAmbiguousControllerException_RouteWithUrl() { // Arrange RouteBase route = new Route("{controller}/blah", new Mock <IRouteHandler>().Object); Type[] matchingTypes = new Type[] { typeof(object), typeof(string) }; // Act InvalidOperationException exception = DefaultControllerFactory.CreateAmbiguousControllerException( route, "Foo", matchingTypes ); // Assert Assert.Equal( "Multiple types were found that match the controller named 'Foo'. This can happen if the route that services this request ('{controller}/blah') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter." + Environment.NewLine + Environment.NewLine + "The request for 'Foo' has found the following matching controllers:" + Environment.NewLine + "System.Object" + Environment.NewLine + "System.String", exception.Message ); }
public IController CreateController(RequestContext requestContext, string controllerName) { if (controllerName.ToLower().StartsWith("devchrgtracking")) { // show dependecy chain if manually created //var repository = new DeviceChargeRepository(100); //var service = new DevChrgTrackingService(repository); //var controller = new DevChrgTrackingController(service); Resolver resolve = new Resolver(); resolve.RegisterComponents( new Component().For <DeviceChargeRepository>().WithInstanceMode(InstanceMode.Transient), new Component().For <DevChrgTrackingService>().WithInstanceMode(InstanceMode.Transient), new Component().For <DevChrgTrackingController>().WithInstanceMode(InstanceMode.Transient)); resolve.ResolveDependencies(); var controller = resolve.Resolve <DevChrgTrackingController>(); return(controller); } var defaultFactory = new DefaultControllerFactory(); return(defaultFactory.CreateController(requestContext, controllerName)); }
public void CreateController_UsesControllerActivatorToInstantiateController() { // Arrange var expected = new MyController(); var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(MyController).GetTypeInfo() }; var httpContext = new DefaultHttpContext(); httpContext.RequestServices = GetServices(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); var activator = new Mock<IControllerActivator>(); activator.Setup(a => a.Create(actionContext, typeof(MyController))) .Returns(expected) .Verifiable(); var controllerFactory = new DefaultControllerFactory(activator.Object); // Act var result = controllerFactory.CreateController(actionContext); // Assert var controller = Assert.IsType<MyController>(result); Assert.Same(expected, controller); activator.Verify(); }
public void GetControllerTypeDoesNotThrowIfSameControllerMatchedMultipleNamespaces() { // both namespaces "ns3a" and "ns3a.ns3b" will match a controller type, but it's actually // the same type. in this case, we shouldn't throw. // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a", "ns3a.ns3b"); requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false; DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); // Assert Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.Equal(verifiedC1, c1Type); }
public void GetControllerTypeForManyAssemblies() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); Type c3Type = factory.GetControllerType(requestContext, "c3"); // lower case Type c4Type = factory.GetControllerType(requestContext, "c4"); // lower case // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm2 = Assembly.Load("MvcAssembly2"); Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller"); Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.NotNull(verifiedC3); Assert.NotNull(verifiedC4); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(verifiedC3, c3Type); Assert.Equal(verifiedC4, c4Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeSearchesOnlyRouteDefinedNamespacesIfRequested() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); requestContext.RouteData.DataTokens["UseNamespaceFallback"] = false; DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.Equal(verifiedC1, c1Type); Assert.Null(c2Type); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType(requestContext, "C1"); Type c2Type = factory.GetControllerType(requestContext, "C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.NotNull(verifiedC1); Assert.NotNull(verifiedC2); Assert.Equal(verifiedC1, c1Type); Assert.Equal(verifiedC2, c2Type); Assert.Equal(4, controllerTypeCache.Count); }
public void GetControllerTypeSearchesRouteDefinedNamespacesBeforeApplicationDefinedNamespaces() { // Arrange RequestContext requestContext = GetRequestContextWithNamespaces("ns3a.ns3b"); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; factory.RequestContext = requestContext; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm3 = Assembly.Load("MvcAssembly3"); Type verifiedC1 = asm3.GetType("NS3a.NS3b.C1Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInDifferentNamespaces() { // Arrange DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly3") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public static void RenderView(string controllerName, string viewName, RouteValueDictionary parameters, object model, TextWriter writer) { // Get the HttpContext var httpContextBase = new HttpContextWrapper(HttpContext.Current); // Build the route data, pointing to the dummy controller var routeData = new RouteData(); routeData.Values.Add("controller", controllerName); foreach (var pair in parameters) { routeData.Values[pair.Key] = pair.Value; } // Create the controller context var requestContext = new RequestContext(httpContextBase, routeData); var controller = new DefaultControllerFactory().CreateController(requestContext, controllerName); var controllerContext = new ControllerContext(requestContext, (ControllerBase)controller); // Find the partial view var view = FindPartialView(controllerContext, viewName); // create the view context and pass in the model var viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary() { Model = model }, new TempDataDictionary(), writer); // finally, render the view view.Render(viewContext, writer); }
public void CreateControllerReleaser_ReturnsReleaser() { // Arrange var controller = new object(); var factory = new DefaultControllerFactory( Mock.Of <IControllerActivator>(), Enumerable.Empty <IControllerPropertyActivator>()); Action <ControllerContext, object> expected = (_, __) => { }; var activatorProvider = new Mock <IControllerActivatorProvider>(); activatorProvider.Setup(p => p.CreateReleaser(It.IsAny <ControllerActionDescriptor>())) .Returns(expected) .Verifiable(); var provider = new ControllerFactoryProvider( activatorProvider.Object, factory, Enumerable.Empty <IControllerPropertyActivator>()); var descriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(object).GetTypeInfo(), }; // Act var actual = provider.CreateControllerReleaser(descriptor); // Assert Assert.Same(expected, actual); activatorProvider.Verify(); }
public void GetControllerDelegatesToActivatorResolver() { //Arrange var context = new RequestContext(); var expectedController = new Goodcontroller(); var resolverActivator = new Mock <IControllerActivator>(); resolverActivator .Setup(a => a.Create(context, typeof(Goodcontroller))) .Returns(expectedController); var activatorResolver = new Resolver <IControllerActivator> { Current = resolverActivator.Object }; var factory = new DefaultControllerFactory(null, activatorResolver, null); //Act IController returnedController = factory.GetControllerInstance( context, typeof(Goodcontroller) ); //Assert Assert.Same(returnedController, expectedController); }
public void CreateController_UsesControllerActivatorToInstantiateController() { // Arrange var expected = new MyController(); var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(MyController).GetTypeInfo() }; var httpContext = new DefaultHttpContext(); httpContext.RequestServices = GetServices(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); var activator = new Mock <IControllerActivator>(); activator.Setup(a => a.Create(actionContext, typeof(MyController))) .Returns(expected) .Verifiable(); var controllerFactory = new DefaultControllerFactory(activator.Object); // Act var result = controllerFactory.CreateController(actionContext); // Assert var controller = Assert.IsType <MyController>(result); Assert.Same(expected, controller); activator.Verify(); }
public void CreateController_SetsBindingContext() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithAttributes).GetTypeInfo() }; var bindingContext = new ActionBindingContext(); var services = GetServices(); services.GetRequiredService <IScopedInstance <ActionBindingContext> >().Value = bindingContext; var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType <ControllerWithAttributes>(result); Assert.Same(bindingContext, controller.BindingContext); }
public void EvaluateAjaxRequest(AjaxRequestResult requestResult) { //The easy way requestResult.IsAjaxRequest = (_httpRequest[XRequestedWith] == XmlHttpRequest) || (_httpRequest.Headers[XRequestedWith] == XmlHttpRequest); //If we are not sure that we have an AJAX request or that we have to return JSON //we fall back to Reflection if (!requestResult.IsAjaxRequest) { try { //We create a controller instance var controllerFactory = new DefaultControllerFactory(); var controller = controllerFactory.CreateController(_httpRequest.RequestContext, requestResult.RequestController) as Controller; //We get the controller actions if (controller != null) { var controllerDescriptor = new ReflectedControllerDescriptor(controller.GetType()); var controllerActions = controllerDescriptor.GetCanonicalActions(); //We search for our action if (SeekControllerActionWithJson(requestResult, controllerActions)) { requestResult.IsAjaxRequest = true; } } } catch { } } }
/// <summary> /// Maps the attribute-defined routes for the application. /// </summary> /// <param name="routes">The route collection.</param> /// <param name="constraintResolver"> /// The <see cref="IInlineConstraintResolver"/> to use for resolving inline constraints in route templates. /// </param> /// <param name="directRouteProvider"> /// The <see cref="IDirectRouteProvider"/> to use for mapping routes from controller types. /// </param> public static void MapAttributeRoutes( RouteCollection routes, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider ) { if (routes == null) { throw new ArgumentNullException("routes"); } if (constraintResolver == null) { throw new ArgumentNullException("constraintResolver"); } if (directRouteProvider == null) { throw new ArgumentNullException("directRouteProvider"); } DefaultControllerFactory typesLocator = DependencyResolver.Current.GetService <IControllerFactory>() as DefaultControllerFactory ?? ControllerBuilder.Current.GetControllerFactory() as DefaultControllerFactory ?? new DefaultControllerFactory(); IReadOnlyList <Type> controllerTypes = typesLocator.GetControllerTypes(); MapAttributeRoutes(routes, controllerTypes, constraintResolver, directRouteProvider); }
public void GetControllerType_WithMatchedDirectRoute_UseControllerDescriptorType() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteFromController <AttributeRouteAtControllerLevelController>(); requestContext.RouteData.AddDirectRouteMatches(); var expectedControllerType = typeof(AttributeRouteAtControllerLevelController); var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver <IControllerActivator>(); var factory = new DefaultControllerFactory( controllerActivator, activatorResolver, null ); // Act // if it would not get the controller type from the DirectRoute, then it would not // be able to find any controller var type = factory.GetControllerType(requestContext, controllerName: null); // Assert Assert.Equal(expectedControllerType, type); }
public void GetControllerType_WithMultipleDirectRouteControllers_ThrowsInvalidOperationException() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); SubRouteCollection subRoutes = new SubRouteCollection(); DirectRouteTestHelpers.AddDirectRouteFromController <AttributeRouteAtControllerLevelController>( subRoutes ); DirectRouteTestHelpers.AddDirectRouteFromMethod <WithRoutingAttributeController>( subRoutes, c => c.Action() ); requestContext.RouteData.Route = new RouteCollectionRoute(subRoutes); requestContext.RouteData.AddDirectRouteMatches(); var controllerActivator = new Mock <IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver <IControllerActivator>(); var factory = new DefaultControllerFactory( controllerActivator, activatorResolver, null ); // Act & Assert Assert.Throws <InvalidOperationException>( () => factory.GetControllerType(requestContext, controllerName: null) ); }
public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenControllerActivatorIsPassedInConstructor() { // Arrange var context = new RequestContext(); var expectedController = new Goodcontroller(); Mock <IControllerActivator> activator = new Mock <IControllerActivator>(); activator .Setup(a => a.Create(context, typeof(Goodcontroller))) .Returns(expectedController); var resolverActivator = new Mock <IControllerActivator>(MockBehavior.Strict); var activatorResolver = new Resolver <IControllerActivator> { Current = resolverActivator.Object }; var dependencyResolver = new Mock <IDependencyResolver>(MockBehavior.Strict); //Act var factory = new DefaultControllerFactory( activator.Object, activatorResolver, dependencyResolver.Object ); IController returnedController = factory.GetControllerInstance( context, typeof(Goodcontroller) ); //Assert Assert.Same(returnedController, expectedController); }
public void GetControllerTypeForManyAssemblies() { // Arrange DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b", "ns2a.ns2b", "ns3a.ns3b", "ns4a.ns4b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly2") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type c1Type = factory.GetControllerType("C1"); Type c2Type = factory.GetControllerType("C2"); Type c3Type = factory.GetControllerType("c3"); // lower case Type c4Type = factory.GetControllerType("c4"); // lower case // Assert Assembly asm1 = Assembly.Load("MvcAssembly1"); Type verifiedC1 = asm1.GetType("NS1a.NS1b.C1Controller"); Type verifiedC2 = asm1.GetType("NS2a.NS2b.C2Controller"); Assembly asm2 = Assembly.Load("MvcAssembly2"); Type verifiedC3 = asm2.GetType("NS3a.NS3b.C3Controller"); Type verifiedC4 = asm2.GetType("NS4a.NS4b.C4Controller"); Assert.IsNotNull(verifiedC1, "Couldn't find real C1 type"); Assert.IsNotNull(verifiedC2, "Couldn't find real C2 type"); Assert.IsNotNull(verifiedC3, "Couldn't find real C3 type"); Assert.IsNotNull(verifiedC4, "Couldn't find real C4 type"); Assert.AreEqual <Type>(verifiedC1, c1Type, "Should have found C1Controller type."); Assert.AreEqual <Type>(verifiedC2, c2Type, "Should have found C2Controller type."); Assert.AreEqual <Type>(verifiedC3, c3Type, "Should have found C3Controller type."); Assert.AreEqual <Type>(verifiedC4, c4Type, "Should have found C4Controller type."); Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public void GetControllerTypeForAssembliesWithSameTypeNamesInSameNamespaceThrows() { // Arrange RequestContext requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = GetDefaultControllerFactory("ns1a.ns1b"); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { Assembly.Load("MvcAssembly1"), Assembly.Load("MvcAssembly4") }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act ExceptionHelper.ExpectException <InvalidOperationException>( delegate { factory.GetControllerType(requestContext, "C1"); }, @"Multiple types were found that match the controller named 'C1'. This can happen if the route that services this request does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. The request for 'C1' has found the following matching controllers: NS1a.NS1b.C1Controller NS1a.NS1b.C1Controller"); // Assert Assert.AreEqual <int>(4, controllerTypeCache.Count, "Cache should have 4 controller types."); }
public MefControllerFactory(string pluginPath) { this.pluginPath = pluginPath; this.catalog = new DirectoryCatalog(pluginPath); this.container = new CompositionContainer(catalog); this.defaultControllerFactory = new DefaultControllerFactory(); }
/// <summary> /// A static method that routes errors to a single centralized error-handler. /// Message is expected to be a human-readable string that can be displayed to the user. /// The error details can contains any additional details (e.g., for logging purposes). /// </summary> public static ActionResult ShowErrorMessage(string message, string errorDetails) { IController commonController = new DefaultControllerFactory().CreateController( System.Web.HttpContext.Current.Request.RequestContext, "Office365Common"); return(((Office365CommonController)commonController).ShowErrorMessageHelper(message, errorDetails)); }
public MefControllerFactory2(string pluginPath) { _pluginPath = pluginPath; _catalog = new DirectoryCatalog(pluginPath); _container = new CompositionContainer(_catalog); defaultControllerFactory = new DefaultControllerFactory(); }
public void DefaultControllerFactory_ReleasesNonIDisposableController() { // Arrange var factory = new DefaultControllerFactory(Mock.Of <IControllerActivator>()); var controller = new object(); // Act + Assert (does not throw) factory.ReleaseController(controller); }
/// <summary> /// Initializes the controller type cache of the default controller factory. /// </summary> private void InitializeControllerTypeCache() { DefaultControllerFactory defaultControllerFactory = System.Web.Mvc.DependencyResolver.Current.GetService <IControllerFactory>() as DefaultControllerFactory ?? (ControllerBuilder.Current.GetControllerFactory() as DefaultControllerFactory ?? new DefaultControllerFactory()); MethodInfo getControllerTypesMethod = defaultControllerFactory.GetType().GetMethod("GetControllerTypes", BindingFlags.Instance | BindingFlags.NonPublic); getControllerTypesMethod.Invoke(defaultControllerFactory, null); }
static Provider() { container = new WindsorContainer(); container.AddFacility <WcfFacility>(); controllerFactory = new WindsorControllerFactory(container.Kernel); container.Install(FromAssembly.Containing(typeof(FrameworkInstaller))); }
public void should_get_null_if_controller_name_partially_matched() { var controllerTypes = new[] { typeof(ControllerWithPublicAction) }; HttpController controller = new DefaultControllerFactory().CreateController( "WithPublicAction", controllerTypes, new DefaultDependencyResolver(controllerTypes)); Assert.Null(controller); }
public void should_get_null_if_no_controller_type_is_found() { var controllerTypes = new Type[] { }; HttpController controller = new DefaultControllerFactory().CreateController( "NotExistedController", controllerTypes, new DefaultDependencyResolver(controllerTypes)); Assert.Null(controller); }
public void should_create_controllers_by_its_name_case_insensitively() { var controllerTypes = new[] { typeof(ControllerWithPublicAction) }; HttpController controller = new DefaultControllerFactory().CreateController( "controllerwithpublicaction", controllerTypes, new DefaultDependencyResolver(controllerTypes)); Assert.Equal(typeof(ControllerWithPublicAction), controller.GetType()); }
public void DefaultControllerFactory_ReleasesNonIDisposableController() { // Arrange var factory = new DefaultControllerFactory( Mock.Of<IServiceProvider>(), Mock.Of<ITypeActivator>(), Mock.Of<IControllerActivator>()); var controller = new Object(); // Act + Assert Assert.DoesNotThrow(() => factory.ReleaseController(controller)); }
public void CreateControllerWithNullContextThrows() { // Arrange DefaultControllerFactory factory = new DefaultControllerFactory(); // Act Assert.ThrowsArgumentNull( delegate { ((IControllerFactory)factory).CreateController( null, "foo"); }, "requestContext"); }
public void CreateControllerWithEmptyControllerNameThrows() { // Arrange DefaultControllerFactory factory = new DefaultControllerFactory(); // Act Assert.Throws<ArgumentException>( delegate { ((IControllerFactory)factory).CreateController( new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()), String.Empty); }, "Value cannot be null or empty.\r\nParameter name: controllerName"); }
public void DefaultControllerFactory_DisposesIDisposableController() { // Arrange var factory = new DefaultControllerFactory( Mock.Of<IServiceProvider>(), Mock.Of<ITypeActivator>(), Mock.Of<IControllerActivator>()); var controller = new MyController(); // Act + Assert Assert.False(controller.Disposed); factory.ReleaseController(controller); Assert.True(controller.Disposed); }
public void CreateController_ThrowsIfActionDescriptorIsNotControllerActionDescriptor() { // Arrange var expected = "The action descriptor must be of type 'Microsoft.AspNet.Mvc.ControllerActionDescriptor'." + Environment.NewLine + "Parameter name: actionContext"; var actionDescriptor = new ActionDescriptor(); var controllerFactory = new DefaultControllerFactory(Mock.Of<IControllerActivator>()); var httpContext = new DefaultHttpContext(); var actionContext = new ActionContext(httpContext, new RouteData(), actionDescriptor); // Act and Assert var ex = Assert.Throws<ArgumentException>(() => controllerFactory.CreateController(actionContext)); Assert.Equal(expected, ex.Message); Assert.Equal("actionContext", ex.ParamName); }
public async void Start(Assembly assembly) { Host = Browser.Window.Location.Host; Port = Browser.Window.Location.Port; Scheme = Browser.Window.Location.Protocol; Browser.Window.AddEventListener("popstate", evt => OnPopState(evt.As<PopStateEvent>())); var path = Browser.Window.Location.PathName; Console.WriteLine(path); ControllerFactory = new DefaultControllerFactory(DependencyResolver); var routeGenerator = new RouteGenerator(); routeTree = routeGenerator.GenerateRoutes(assembly); await OnStarting(); await OnStarted(); }
public void CreateController_SetsPropertiesFromActionContextHierarchy() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Same(context, controller.ActionContext); Assert.Same(httpContext, controller.HttpContext); }
public void GetControllerSessionBehaviorReturnsAttributeValueFromController() { // Arrange var factory = new DefaultControllerFactory(); // Act SessionStateBehavior result = factory.GetControllerSessionBehavior(null, typeof(MyReadOnlyController)); // Assert Assert.Equal(SessionStateBehavior.ReadOnly, result); }
public void GetControllerSessionBehaviorReturnsDefaultForControllerWithoutAttribute() { // Arrange var factory = new DefaultControllerFactory(); // Act SessionStateBehavior result = factory.GetControllerSessionBehavior(null, typeof(object)); // Assert Assert.Equal(SessionStateBehavior.Default, result); }
public void GetControllerSessionBehaviorReturnsDefaultForNullControllerType() { // Arrange var factory = new DefaultControllerFactory(); // Act SessionStateBehavior result = factory.GetControllerSessionBehavior(null, null); // Assert Assert.Equal(SessionStateBehavior.Default, result); }
public void GetControllerSessionBehaviorGuardClauses() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); IControllerFactory factory = new DefaultControllerFactory(); // Act & Assert Assert.ThrowsArgumentNull( () => factory.GetControllerSessionBehavior(null, "controllerName"), "requestContext" ); Assert.ThrowsArgumentNullOrEmpty( () => factory.GetControllerSessionBehavior(requestContext, null), "controllerName" ); Assert.ThrowsArgumentNullOrEmpty( () => factory.GetControllerSessionBehavior(requestContext, ""), "controllerName" ); }
public void GetControllerInstanceThrowsIfControllerTypeIsNotControllerBase() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = new DefaultControllerFactory(); // Act Assert.Throws<ArgumentException>( delegate { factory.GetControllerInstance(requestContext, typeof(int)); }, "The controller type 'System.Int32' must implement IController.\r\nParameter name: controllerType"); }
public void GetControllerDelegatesToActivatorResolver() { //Arrange var context = new RequestContext(); var expectedController = new Goodcontroller(); var resolverActivator = new Mock<IControllerActivator>(); resolverActivator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController); var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object }; var factory = new DefaultControllerFactory(null, activatorResolver, null); //Act IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller)); //Assert Assert.Same(returnedController, expectedController); }
public void GetControllerDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull() { // Arrange var context = new RequestContext(); var dependencyResolver = new Mock<IDependencyResolver>(); var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object); // Act & Assert Assert.Throws<InvalidOperationException>( () => factory.GetControllerInstance(context, typeof(NoParameterlessCtor)), "An error occurred when trying to create a controller of type 'System.Web.Mvc.Test.DefaultControllerFactoryTest+NoParameterlessCtor'. Make sure that the controller has a parameterless public constructor." ); }
public void GetControllerTypeForNoAssemblies() { // Arrange RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()); DefaultControllerFactory factory = new DefaultControllerFactory(); MockBuildManager buildManagerMock = new MockBuildManager(new Assembly[] { }); ControllerTypeCache controllerTypeCache = new ControllerTypeCache(); factory.BuildManager = buildManagerMock; factory.ControllerTypeCache = controllerTypeCache; // Act Type controllerType = factory.GetControllerType(requestContext, "sometype"); // Assert Assert.Null(controllerType); Assert.Equal(0, controllerTypeCache.Count); }
public void DefaultControllerFactory_ReleasesNonIDisposableController() { // Arrange var factory = new DefaultControllerFactory(Mock.Of<IControllerActivator>()); var controller = new object(); // Act + Assert (does not throw) factory.ReleaseController(controller); }
public void GetControllerType_WithMultipleDirectRouteControllers_ThrowsInvalidOperationException() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); SubRouteCollection subRoutes = new SubRouteCollection(); DirectRouteTestHelpers.AddDirectRouteFromController<AttributeRouteAtControllerLevelController>(subRoutes); DirectRouteTestHelpers.AddDirectRouteFromMethod<WithRoutingAttributeController>(subRoutes, c => c.Action()); requestContext.RouteData.Route = new RouteCollectionRoute(subRoutes); requestContext.RouteData.AddDirectRouteMatches(); var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver<IControllerActivator>(); var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null); // Act & Assert Assert.Throws<InvalidOperationException>(() => factory.GetControllerType(requestContext, controllerName: null)); }
public void GetControllerType_WithMatchedDirectRoute_UseControllerDescriptorType() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteFromController<AttributeRouteAtControllerLevelController>(); requestContext.RouteData.AddDirectRouteMatches(); var expectedControllerType = typeof(AttributeRouteAtControllerLevelController); var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver<IControllerActivator>(); var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null); // Act // if it would not get the controller type from the DirectRoute, then it would not // be able to find any controller var type = factory.GetControllerType(requestContext, controllerName: null); // Assert Assert.Equal(expectedControllerType, type); }
public void CreateControllerWithFactoryInstanceReturnsInstance() { // Arrange ControllerBuilder cb = new ControllerBuilder(); DefaultControllerFactory factory = new DefaultControllerFactory(); cb.SetControllerFactory(factory); // Act IControllerFactory cf = cb.GetControllerFactory(); // Assert Assert.Same(factory, cf); }
public void GetControllerInstanceConsultsSetControllerActivator() { //Arrange Mock<IControllerActivator> activator = new Mock<IControllerActivator>(); DefaultControllerFactory factory = new DefaultControllerFactory(activator.Object); RequestContext context = new RequestContext(); //Act factory.GetControllerInstance(context, typeof(Goodcontroller)); //Assert activator.Verify(l => l.Create(context, typeof(Goodcontroller))); }
public void CreateController_ThrowsIfPropertyCannotBeActivated() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerThatCannotBeActivated).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act and Assert var exception = Assert.Throws<InvalidOperationException>(() => factory.CreateController(context)); Assert.Equal("The property 'Service' on controller '" + typeof(ControllerThatCannotBeActivated) + "' cannot be activated.", exception.Message); }
public void GetControllerDelegatesToDependencyResolveWhenActivatorResolverIsNull() { // Arrange var context = new RequestContext(); var expectedController = new Goodcontroller(); var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict); dependencyResolver.Setup(dr => dr.GetService(typeof(Goodcontroller))).Returns(expectedController); var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object); // Act IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller)); // Assert Assert.Same(returnedController, expectedController); }
public void CreateController_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var services = GetServices(); var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Null(controller.Response); }
public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenControllerActivatorIsPassedInConstructor() { // Arrange var context = new RequestContext(); var expectedController = new Goodcontroller(); Mock<IControllerActivator> activator = new Mock<IControllerActivator>(); activator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController); var resolverActivator = new Mock<IControllerActivator>(MockBehavior.Strict); var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object }; var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict); //Act var factory = new DefaultControllerFactory(activator.Object, activatorResolver, dependencyResolver.Object); IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller)); //Assert Assert.Same(returnedController, expectedController); }
public void CreateController_SetsBindingContext() { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = typeof(ControllerWithActivateAndFromServices).GetTypeInfo() }; var bindingContext = new ActionBindingContext(); var services = GetServices(); services.GetRequiredService<IScopedInstance<ActionBindingContext>>().Value = bindingContext; var httpContext = new DefaultHttpContext { RequestServices = services }; var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); // Act var result = factory.CreateController(context); // Assert var controller = Assert.IsType<ControllerWithActivateAndFromServices>(result); Assert.Same(bindingContext, controller.BindingContext); }
public void GetControllerType_WithMatchedDirectRoute_UseTargetMethodType() { // Arrange var requestContext = new RequestContext(); requestContext.RouteData = new RouteData(); requestContext.RouteData.Route = DirectRouteTestHelpers.BuildDirectRouteStubsFrom<WithRoutingAttributeController>(c => c.Action())[0]; var expectedControllerType = typeof(WithRoutingAttributeController); var controllerActivator = new Mock<IControllerActivator>(MockBehavior.Strict).Object; var activatorResolver = new Resolver<IControllerActivator>(); var factory = new DefaultControllerFactory(controllerActivator, activatorResolver, null); // Act // if it would not get the controller type from the DirectRoute, then it would not // be able to find any controller var type = factory.GetControllerType(requestContext, "no_such_controller"); // Assert Assert.Equal(expectedControllerType, type); }
public void DisposeControllerFactoryWithDisposableController() { // Arrange IControllerFactory factory = new DefaultControllerFactory(); Mock<ControllerBase> mockController = new Mock<ControllerBase>(); Mock<IDisposable> mockDisposable = mockController.As<IDisposable>(); mockDisposable.Setup(d => d.Dispose()).Verifiable(); // Act factory.ReleaseController(mockController.Object); // Assert mockDisposable.Verify(); }