Exemple #1
0
        public void SubstituteRequiredValues_CanRejectValueForParameter_WithNonMatchingConstraint()
        {
            // Arrange
            var template = "{controller}/{action}/{id?}";
            var defaults = new { };
            var policies = new { controller = "Home", action = new RegexRouteConstraint("Index"), };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { controller = "Blog", action = "Index", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Null(actual);
        }
Exemple #2
0
        public void SubstituteRequiredValues_RejectsNullForOutOfLineDefault()
        {
            // Arrange
            var template = "{controller=Home}/{action=Index}/{id?}";
            var defaults = new { area = "Admin" };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { area = string.Empty, };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Null(actual);
        }
Exemple #3
0
        public void SubstituteRequiredValues_NullRequiredValueParameter_Fail()
        {
            // Arrange
            var template = "PageRoute/Attribute/{page}";
            var defaults = new { area = (string)null, page = (string)null, controller = "Home", action = "Index", };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { area = (string)null, page = (string)null, controller = "Home", action = "Index", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Null(actual);
        }
Exemple #4
0
        public void SubstituteRequiredValues_CanRejectValueForDefault_WithDifferentValue()
        {
            // Arrange
            var template = "Home/Index/{id?}";
            var defaults = new { controller = "Home", action = "Index", };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { controller = "Blog", action = "Index", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Null(actual);
        }
Exemple #5
0
        public void Parse_WithRequiredValues_AllowsEmptyRequiredValue()
        {
            // Arrange
            var template       = "{controller=Home}/{action=Index}/{id?}";
            var defaults       = new { };
            var policies       = new { };
            var requiredValues = new { area = "", controller = "Store", action = "Index", };

            // Act
            var action = RoutePatternFactory.Parse(template, defaults, policies, requiredValues);

            // Assert
            Assert.Collection(
                action.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => { Assert.Equal("action", kvp.Key); Assert.Equal("Index", kvp.Value); },
                kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("", kvp.Value); },
                kvp => { Assert.Equal("controller", kvp.Key); Assert.Equal("Store", kvp.Value); });
        }
Exemple #6
0
        public void Pattern_ExtraConstraints_MergeMultipleConstraintsForKey()
        {
            // Arrange
            var template    = "{a:int}/{b}/{c:int}";
            var defaults    = new { };
            var constraints = new { b = "fizz", c = new object[] { new RegexRouteConstraint("foo"), new RegexRouteConstraint("bar"), "baz" } };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var actual = RoutePatternFactory.Pattern(
                original.RawText,
                defaults,
                constraints,
                original.PathSegments);

            // Assert
            Assert.Collection(
                actual.ParameterPolicies.OrderBy(kvp => kvp.Key),
                kvp =>
            {
                Assert.Equal("a", kvp.Key);
                Assert.Collection(
                    kvp.Value,
                    c => Assert.Equal("int", c.Content));
            },
                kvp =>
            {
                Assert.Equal("b", kvp.Key);
                Assert.Collection(
                    kvp.Value,
                    c => Assert.Equal("^(fizz)$", Assert.IsType <RegexRouteConstraint>(c.ParameterPolicy).Constraint.ToString()));
            },
                kvp =>
            {
                Assert.Equal("c", kvp.Key);
                Assert.Collection(
                    kvp.Value,
                    c => Assert.Equal("foo", Assert.IsType <RegexRouteConstraint>(c.ParameterPolicy).Constraint.ToString()),
                    c => Assert.Equal("bar", Assert.IsType <RegexRouteConstraint>(c.ParameterPolicy).Constraint.ToString()),
                    c => Assert.Equal("^(baz)$", Assert.IsType <RegexRouteConstraint>(c.ParameterPolicy).Constraint.ToString()),
                    c => Assert.Equal("int", c.Content));
            });
        }
        public void Pattern_MergesConstraints()
        {
            // Arrange
            var template    = "{a:int}/{b}/{c}";
            var defaults    = new { };
            var constraints = new { a = new RegexRouteConstraint("foo"), b = new RegexRouteConstraint("bar") };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var actual = RoutePatternFactory.Pattern(
                original.RawText,
                defaults,
                constraints,
                original.PathSegments);

            // Assert
            Assert.Collection(
                actual.GetParameter("a").Constraints,
                c => Assert.IsType <RegexRouteConstraint>(c.Constraint),
                c => Assert.Equal("int", c.Content));
            Assert.Collection(
                actual.GetParameter("b").Constraints,
                c => Assert.IsType <RegexRouteConstraint>(c.Constraint));

            Assert.Collection(
                actual.Constraints.OrderBy(kvp => kvp.Key),
                kvp =>
            {
                Assert.Equal("a", kvp.Key);
                Assert.Collection(
                    kvp.Value,
                    c => Assert.IsType <RegexRouteConstraint>(c.Constraint),
                    c => Assert.Equal("int", c.Content));
            },
                kvp =>
            {
                Assert.Equal("b", kvp.Key);
                Assert.Collection(
                    kvp.Value,
                    c => Assert.IsType <RegexRouteConstraint>(c.Constraint));
            });
        }
Exemple #8
0
        public void Pattern_ExtraConstraints_NestedArray_Throws()
        {
            // Arrange
            var template    = "{a}/{b}/{c:int}";
            var defaults    = new { };
            var constraints = new { c = new object[] { new object[0] } };

            var original = RoutePatternFactory.Parse(template);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(() =>
            {
                RoutePatternFactory.Pattern(
                    original.RawText,
                    defaults,
                    constraints,
                    original.PathSegments);
            });
        }
Exemple #9
0
        public void SubstituteRequiredValues_CanRejectValueForDefault_WithSameValue_WithNonMatchingConstraint()
        {
            // Arrange
            var template = "Home/Index/{id?}";
            var defaults = new { controller = "Home", action = "Index", };
            var policies = new { controller = "Home", };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { controller = "Home", action = "Index", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Collection(
                actual.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("action", "Index"), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("controller", "Home"), kvp));
        }
Exemple #10
0
        public void SubstituteRequiredValues_CanAcceptValueForDefault_WithSameValue_Null()
        {
            // Arrange
            var template = "Home/Index/{id?}";
            var defaults = new { controller = (string)null, action = "", };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { controller = string.Empty, action = (string)null, };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Collection(
                actual.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("action", null), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("controller", ""), kvp));
        }
Exemple #11
0
        public void SubstituteRequiredValues_CanAcceptNullForAnyKey()
        {
            // Arrange
            var template = "{controller=Home}/{action=Index}/{id?}";
            var defaults = new { };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { a = (string)null, b = "", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Collection(
                actual.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("a", null), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("b", string.Empty), kvp));
        }
Exemple #12
0
        public void Parse_WithRequiredValues_ThrowsForNonParameterNonDefault()
        {
            // Arrange
            var template       = "{controller=Home}/{action=Index}/{id?}";
            var defaults       = new { };
            var policies       = new { };
            var requiredValues = new { area = "Admin", controller = "Store", action = "Index", };

            // Act
            var exception = Assert.Throws <InvalidOperationException>(() =>
            {
                var action = RoutePatternFactory.Parse(template, defaults, policies, requiredValues);
            });

            // Assert
            Assert.Equal(
                "No corresponding parameter or default value could be found for the required value " +
                "'area=Admin'. A non-null required value must correspond to a route parameter or the " +
                "route pattern must have a matching default value.",
                exception.Message);
        }
        public void Pattern_SameDuplicateDefaultValue()
        {
            // Arrange
            var template    = "{a=13}/{b}/{c}";
            var defaults    = new { a = "13", };
            var constraints = new { };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var actual = RoutePatternFactory.Pattern(
                original.RawText,
                defaults,
                constraints,
                original.PathSegments);

            // Assert
            Assert.Collection(
                actual.Defaults,
                kvp => { Assert.Equal("a", kvp.Key); Assert.Equal("13", kvp.Value); });
        }
        public void Pattern_OptionalParameterDefaultValue_Throws()
        {
            // Arrange
            var template    = "{a}/{b}/{c?}";
            var defaults    = new { c = "15", };
            var constraints = new { };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => RoutePatternFactory.Pattern(
                                                                   original.RawText,
                                                                   defaults,
                                                                   constraints,
                                                                   original.PathSegments));

            // Assert
            Assert.Equal(
                "An optional parameter cannot have default value.",
                ex.Message);
        }
Exemple #15
0
        public void SubstituteRequiredValues_CanMergeExistingRequiredValues()
        {
            // Arrange
            var template = "Home/Index/{id?}";
            var defaults = new { area = "Admin", controller = "Home", action = "Index", };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies, new { area = "Admin", controller = "Home", });

            var requiredValues = new { controller = "Home", action = "Index", };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Collection(
                actual.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("action", "Index"), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("area", "Admin"), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("controller", "Home"), kvp));
        }
        public void Pattern_InvalidConstraintTypeThrows()
        {
            // Arrange
            var template    = "{a}/{b}/{c}";
            var defaults    = new { };
            var constraints = new { d = 17, };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => RoutePatternFactory.Pattern(
                                                                   original.RawText,
                                                                   defaults,
                                                                   constraints,
                                                                   original.PathSegments));

            // Assert
            Assert.Equal(
                $"Invalid constraint '17'. A constraint must be of type 'string', '{typeof(IRouteConstraint)}', or '{typeof(MatchProcessor)}'.",
                ex.Message);
        }
        public void Pattern_ExtraDefaultValues()
        {
            // Arrange
            var template    = "{a}/{b}/{c}";
            var defaults    = new { d = "15", e = 17 };
            var constraints = new { };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var actual = RoutePatternFactory.Pattern(
                original.RawText,
                defaults,
                constraints,
                original.PathSegments);

            // Assert
            Assert.Collection(
                actual.Defaults.OrderBy(kvp => kvp.Key),
                kvp => { Assert.Equal("d", kvp.Key); Assert.Equal("15", kvp.Value); },
                kvp => { Assert.Equal("e", kvp.Key); Assert.Equal(17, kvp.Value); });
        }
        public void Pattern_DuplicateDefaultValue_Throws()
        {
            // Arrange
            var template    = "{a=13}/{b}/{c}";
            var defaults    = new { a = "15", };
            var constraints = new { };

            var original = RoutePatternFactory.Parse(template);

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => RoutePatternFactory.Pattern(
                                                                   original.RawText,
                                                                   defaults,
                                                                   constraints,
                                                                   original.PathSegments));

            // Assert
            Assert.Equal(
                "The route parameter 'a' has both an inline default value and an explicit default " +
                "value specified. A route parameter cannot contain an inline default value when a " +
                "default value is specified explicitly. Consider removing one of them.",
                ex.Message);
        }
Exemple #19
0
        public void SubstituteRequiredValues_AllowRequiredValueAnyForParameter()
        {
            // Arrange
            var template = "{controller=Home}/{action=Index}/{id?}";
            var defaults = new { };
            var policies = new { };

            var original = RoutePatternFactory.Parse(template, defaults, policies);

            var requiredValues = new { controller = RoutePattern.RequiredValueAny, };

            // Act
            var actual = Transformer.SubstituteRequiredValues(original, requiredValues);

            // Assert
            Assert.Collection(
                actual.Defaults.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("action", "Index"), kvp),
                kvp => Assert.Equal(new KeyValuePair <string, object>("controller", "Home"), kvp)); // default is preserved

            Assert.Collection(
                actual.RequiredValues.OrderBy(kvp => kvp.Key),
                kvp => Assert.Equal(new KeyValuePair <string, object>("controller", RoutePattern.RequiredValueAny), kvp));
        }