Exemple #1
0
        public void MapMvcAttributeRoutes_MapRouteAttributes()
        {
            // Arrange
            var controllerTypes = new[] { typeof(SimpleRoutingController) };
            var routes          = new RouteCollection();

            // Act
            routes.MapMvcAttributeRoutes(controllerTypes);

            // Assert
            var expectedResults = new List <Tuple <string, string, string[]> >
            {
                new Tuple <string, string, string[]>("getme", "GetMe", new[] { "GET" }),
                new Tuple <string, string, string[]>("postme", "PostMe", new[] { "POST" }),
                new Tuple <string, string, string[]>("getorpostme", "GetOrPostMe", new[] { "GET", "POST" }),
                new Tuple <string, string, string[]>("routeme", "RouteMe", null),
                new Tuple <string, string, string[]>("once", "FoolMe", new[] { "GET" }),
                new Tuple <string, string, string[]>("twice", "FoolMe", new[] { "GET" }),
                new Tuple <string, string, string[]>("twice", "FoolMe", new[] { "GET" }),
            };

            foreach (var expected in expectedResults)
            {
                var   url                 = expected.Item1;
                var   methodName          = expected.Item2;
                var   expectedHttpMethods = expected.Item3;
                Route route               = routes.Cast <Route>().Single(r => r.Url == url);
                Assert.Equal(methodName, route.GetTargetActionMethod().Name);
                var httpMethodConstraint = (HttpMethodConstraint)route.Constraints["httpMethod"];
                if (expectedHttpMethods == null)
                {
                    Assert.Null(httpMethodConstraint);
                }
                else
                {
                    Assert.NotNull(httpMethodConstraint);

                    var actualHttpMethods = httpMethodConstraint.AllowedMethods.ToArray();
                    Array.Sort(expectedHttpMethods);
                    Array.Sort(actualHttpMethods);
                    Assert.Equal(expectedHttpMethods, actualHttpMethods);
                }
            }
        }
Exemple #2
0
        public void MapMvcAttributeRoutes_WithInlineConstraints_ParseConstraintsDefaultsAndOptionals()
        {
            // Arrange
            var controllerTypes = new[] { typeof(SimpleRoutingController) };
            var routes          = new RouteCollection();

            // Act
            routes.MapMvcAttributeRoutes(controllerTypes);

            // Assert
            Route route = routes.Cast <Route>().Single(r => r.GetTargetActionMethod().Name == "Parameterized");

            Assert.NotNull(route);

            Assert.Equal("i/{have}/{id}/{defaultsto}/{name}", route.Url);
            Assert.IsAssignableFrom <IntRouteConstraint>(route.Constraints["id"]);
            Assert.Equal("VAL", route.Defaults["defaultsto"]);
            Assert.Equal("", route.Defaults["name"].ToString());
        }