Esempio n. 1
0
        /// <summary>
        /// Creates a <see cref="RoutePattern"/> from its string representation along
        /// with provided default values and constraints.
        /// </summary>
        /// <param name="pattern">The route pattern string to parse.</param>
        /// <param name="defaults">
        /// Additional default values to associated with the route pattern. May be null.
        /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
        /// and then merged into the parsed route pattern.
        /// </param>
        /// <param name="constraints">
        /// Additional constraints to associated with the route pattern. May be null.
        /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
        /// and then merged into the parsed route pattern.
        /// </param>
        /// <returns>The <see cref="RoutePattern"/>.</returns>
        public static RoutePattern Parse(string pattern, object defaults, object constraints)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            var original = RoutePatternParser.Parse(pattern);

            return(Pattern(original.RawText, defaults, constraints, original.PathSegments));
        }
        /// <summary>
        /// Creates a <see cref="RoutePattern"/> from its string representation along
        /// with provided default values and parameter policies.
        /// </summary>
        /// <param name="pattern">The route pattern string to parse.</param>
        /// <param name="defaults">
        /// Additional default values to associated with the route pattern. May be null.
        /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
        /// and then merged into the parsed route pattern.
        /// </param>
        /// <param name="parameterPolicies">
        /// Additional parameter policies to associated with the route pattern. May be null.
        /// The provided object will be converted to key-value pairs using <see cref="RouteValueDictionary"/>
        /// and then merged into the parsed route pattern.
        /// Multiple policies can be specified for a key by providing a collection as the value.
        /// </param>
        /// <param name="requiredValues">
        /// Route values that can be substituted for parameters in the route pattern. See remarks on <see cref="RoutePattern.RequiredValues"/>.
        /// </param>
        /// <returns>The <see cref="RoutePattern"/>.</returns>
        public static RoutePattern Parse(string pattern, object?defaults, object?parameterPolicies, object?requiredValues)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            var original = RoutePatternParser.Parse(pattern);

            return(PatternCore(original.RawText, Wrap(defaults), Wrap(parameterPolicies), Wrap(requiredValues), original.PathSegments));
        }
Esempio n. 3
0
 public void Parse_ComplexSegment_OptionalParameter_NotTheLastPart(
     string template,
     string parameter,
     string invalid)
 {
     // Act and Assert
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         "An optional parameter must be at the end of the segment. In the segment '" + template +
         "', optional parameter '" + parameter + "' is followed by '" + invalid + "'.");
 }
Esempio n. 4
0
        public void Parse_OptionalParameter()
        {
            // Arrange
            var template = "{p?}";

            var expected = Pattern(template, Segment(ParameterPart("p", null, RoutePatternParameterKind.Optional)));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 5
0
        public void ParseRouteParameter_ThrowsIf_ParameterContainsSpecialCharacters(
            string template,
            string parameterName)
        {
            // Arrange
            var expectedMessage = "The route parameter name '" + parameterName + "' is invalid. Route parameter " +
                                  "names must be non-empty and cannot contain these characters: '{', '}', '/'. The '?' character " +
                                  "marks a parameter as optional, and can occur only at the end of the parameter. The '*' character " +
                                  "marks a parameter as catch-all, and can occur only at the start of the parameter.";

            // Act & Assert
            ExceptionAssert.Throws <RoutePatternException>(() => RoutePatternParser.Parse(template), expectedMessage);
        }
Esempio n. 6
0
        public void Parse_SingleParameter()
        {
            // Arrange
            var template = "{p}";

            var expected = Pattern(template, Segment(ParameterPart("p")));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 7
0
        public void Parse_MultipleParameters()
        {
            // Arrange
            var template = "{p1}/{p2}/{*p3}";

            var expected = Pattern(
                template,
                Segment(ParameterPart("p1")),
                Segment(ParameterPart("p2")),
                Segment(ParameterPart("p3", null, RoutePatternParameterKind.CatchAll)));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 8
0
        public void Parse_MultipleLiterals()
        {
            // Arrange
            var template = "cool/awesome/super";

            var expected = Pattern(
                template,
                Segment(LiteralPart("cool")),
                Segment(LiteralPart("awesome")),
                Segment(LiteralPart("super")));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 9
0
        public void Parse_ComplexSegment_PL()
        {
            // Arrange
            var template = "{p1}-cool";

            var expected = Pattern(
                template,
                Segment(
                    ParameterPart("p1"),
                    LiteralPart("-cool")));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 10
0
        [InlineData(@"{p1:regex(([{{(])\w+)}", @"regex(([{(])\w+)")]                                      // Not balanced {
        public void Parse_RegularExpressions(string template, string constraint)
        {
            // Arrange
            var expected = Pattern(
                template,
                Segment(
                    ParameterPart(
                        "p1",
                        null,
                        RoutePatternParameterKind.Standard,
                        Constraint(constraint))));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 11
0
        public void Parse_ComplexSegment_OptionalParameterFollowingPeriod_PeriodAfterSlash()
        {
            // Arrange
            var template = "{p2}/.{p3?}";

            var expected = Pattern(
                template,
                Segment(ParameterPart("p2")),
                Segment(
                    SeparatorPart("."),
                    ParameterPart("p3", null, RoutePatternParameterKind.Optional)));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 12
0
        public void Parse_ComplexSegment_ParametersFollowingPeriod()
        {
            // Arrange
            var template = "{p1}.{p2}";

            var expected = Pattern(
                template,
                Segment(
                    ParameterPart("p1"),
                    LiteralPart("."),
                    ParameterPart("p2")));

            // Act
            var actual = RoutePatternParser.Parse(template);

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Esempio n. 13
0
 public void InvalidTemplate_CatchAllMarkedOptional()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{*b?}"),
         "A catch-all parameter cannot be marked optional.");
 }
Esempio n. 14
0
 public void InvalidTemplate_CannotContainQuestionMark()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foor?bar"),
         "The literal section 'foor?bar' is invalid. Literal sections cannot contain the '?' character.");
 }
Esempio n. 15
0
 public void InvalidTemplate_CannotStartWithTilde()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("~foo"),
         "The route template cannot start with a '~' character unless followed by a '/'.");
 }
Esempio n. 16
0
 public void InvalidTemplate_WithCatchAllNotAtTheEndThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foo/{p1}/{*p2}/{p3}"),
         "A catch-all parameter can only appear as the last segment of the route template.");
 }
Esempio n. 17
0
 public void InvalidTemplate_InvalidParameterNameWithOpenBracketThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{a{aa}/{z}"),
         "In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.");
 }
Esempio n. 18
0
 public void InvalidTemplate_SameParameterTwiceAndOneCatchAllThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{aaa}/{*AAA}"),
         "The route parameter name 'AAA' appears more than one time in the route template.");
 }
Esempio n. 19
0
 public void InvalidTemplate_CannotHaveMoreThanOneCatchAll()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{*p1}/{*p2}"),
         "A catch-all parameter can only appear as the last segment of the route template.");
 }
Esempio n. 20
0
 public void InvalidTemplate_WithRepeatedParameter()
 {
     var ex = ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{Controller}.mvc/{id}/{controller}"),
         "The route parameter name 'controller' appears more than one time in the route template.");
 }