public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithHandler_Throws() { // Arrange string areaPrefix = null; string controllerPrefix = null; ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor("IgnoreController"); ActionDescriptor actionDescriptor = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction"); Route route = new Route(url: null, routeHandler: null); route.DataTokens = new RouteValueDictionary(); route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[] { actionDescriptor }); route.RouteHandler = new Mock <IRouteHandler>(MockBehavior.Strict).Object; ActionDescriptor[] originalActions = route.GetTargetActionDescriptors(); RouteEntry entry = new RouteEntry(name: null, route: route); IDirectRouteFactory factory = CreateStubRouteFactory(entry); IReadOnlyCollection <ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor }; IInlineConstraintResolver constraintResolver = new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object; // Act & Assert string expectedMessage = "Direct routing does not support per-route route handlers."; Assert.Throws <InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix, controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage); }
public void CreateRouteEntry_IfDirectRouteProviderReturnsRouteWithEmptyActionDescriptors_Throws() { // Arrange string areaPrefix = null; string controllerPrefix = null; Route route = new Route(url: null, routeHandler: null); route.DataTokens = new RouteValueDictionary(); route.DataTokens.Add(RouteDataTokenKeys.Actions, new ActionDescriptor[0]); ActionDescriptor[] originalActions = route.GetTargetActionDescriptors(); Assert.NotNull(originalActions); // Guard Assert.Empty(originalActions); // Guard RouteEntry entry = new RouteEntry(name: null, route: route); IDirectRouteFactory factory = CreateStubRouteFactory(entry); ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor("IgnoreController"); ActionDescriptor actionDescriptor = CreateStubActionDescriptor(controllerDescriptor, "IgnoreAction"); IReadOnlyCollection <ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor }; IInlineConstraintResolver constraintResolver = new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object; // Act & Assert string expectedMessage = "The route does not have any associated action descriptors. Routing requires " + "that each direct route map to a non-empty set of actions."; Assert.Throws <InvalidOperationException>(() => DefaultDirectRouteProvider.CreateRouteEntry(areaPrefix, controllerPrefix, factory, actions, constraintResolver, targetIsAction: false), expectedMessage); }
public void CreateRouteEntry_IfDirectRouteProviderReturnsNull_Throws() { // Arrange string areaPrefix = null; string controllerPrefix = null; IDirectRouteFactory factory = CreateStubRouteFactory(null); ControllerDescriptor controllerDescriptor = CreateStubControllerDescriptor( "IgnoreController" ); ActionDescriptor actionDescriptor = CreateStubActionDescriptor( controllerDescriptor, "IgnoreAction" ); IReadOnlyCollection <ActionDescriptor> actions = new ActionDescriptor[] { actionDescriptor }; IInlineConstraintResolver constraintResolver = new Mock <IInlineConstraintResolver>(MockBehavior.Strict).Object; // Act & Assert Assert.Throws <InvalidOperationException>( () => DefaultDirectRouteProvider.CreateRouteEntry( areaPrefix, controllerPrefix, factory, actions, constraintResolver, targetIsAction: false ), "IDirectRouteFactory.CreateRoute must not return null." ); }
public IReadOnlyList <RouteEntry> GetDirectRoutes(HttpControllerDescriptor controllerDescriptor, IReadOnlyList <HttpActionDescriptor> actionDescriptors, IInlineConstraintResolver constraintResolver) { var realDirectRouteProvider = new DefaultDirectRouteProvider(); var directRoutes = realDirectRouteProvider.GetDirectRoutes(controllerDescriptor, actionDescriptors, constraintResolver); // Store the routes in a property so that they can be retrieved later DirectRoutes = directRoutes; return(directRoutes); }
public IReadOnlyList <RouteEntry> GetDirectRoutes(ControllerDescriptor controllerDescriptor, IReadOnlyList <ActionDescriptor> actionDescriptors, IInlineConstraintResolver constraintResolver) { // Get routes from default provider var defaultProvider = new DefaultDirectRouteProvider(); var routes = defaultProvider.GetDirectRoutes(controllerDescriptor, actionDescriptors, constraintResolver); // Create new constraint to accept only supported url prefixes var lc = new LocaleConstraint(); // Add locale parameter and constraint to every route foreach (var item in routes) { item.Route.Url = "{locale}/" + item.Route.Url; item.Route.Constraints.Add("locale", lc); } return(routes); }