Пример #1
0
 public void InvalidTemplate_WithMismatchedBraces(string template)
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         @"There is an incomplete parameter in the route template. Check that each '{' character has a " +
         "matching '}' character.");
 }
Пример #2
0
 public void InvalidTemplate_CannotHaveMoreThanOneCatchAllInMultiSegment()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{*p1}abc{*p2}"),
         "A path segment that contains more than one section, such as a literal section or a parameter, " +
         "cannot contain a catch-all parameter.");
 }
Пример #3
0
 public void InvalidTemplate_CannotHaveConsecutiveCloseBrace()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foo/{p1}}"),
         "There is an incomplete parameter in the route template. Check that each '{' character has a " +
         "matching '}' character.");
 }
Пример #4
0
 [InlineData(@"{p1:regex(^\d{{3}}-\d{{3}}-\d{4}}$)}")]   // Not escaped {
 public void Parse_RegularExpressions_Unescaped(string template)
 {
     // Act and Assert
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         "In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.");
 }
Пример #5
0
 public void InvalidTemplate_ConsecutiveSeparatorsSlashSlashThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}//{z}"),
         "The route template separator character '/' cannot appear consecutively. It must be separated by " +
         "either a parameter or a literal value.");
 }
Пример #6
0
 public void InvalidTemplate_RepeatedParametersThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foo/aa{p1}{p2}"),
         "A path segment cannot contain two consecutive parameters. They must be separated by a '/' or by " +
         "a literal string.");
 }
Пример #7
0
 public void InvalidTemplate_InvalidParameterNameWithCloseBracketThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{aa}a}/{z}"),
         "There is an incomplete parameter in the route template. Check that each '{' character has a " +
         "matching '}' character.");
 }
Пример #8
0
 public void Parse_ComplexSegment_OptionalParametersSeperatedByPeriod_Invalid(string template, string parameter)
 {
     // Act and Assert
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         "In the segment '" + template + "', the optional parameter 'p2' is preceded by an invalid " +
         "segment '" + parameter + "'. Only a period (.) can precede an optional parameter.");
 }
Пример #9
0
 public void Parse_RegularExpressions_Invalid(string template)
 {
     // Act and Assert
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse(template),
         "There is an incomplete parameter in the route template. Check that each '{' character has a matching " +
         "'}' character.");
 }
Пример #10
0
        public void ValidTemplate_CanStartWithSlashOrTildeSlash(string routePattern)
        {
            // Arrange & Act
            var pattern = RoutePatternParser.Parse(routePattern);

            // Assert
            Assert.Equal(routePattern, pattern.RawText);
        }
Пример #11
0
 public void InvalidTemplate_InvalidParameterNameWithQuestionThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{Controller}.mvc/{?}"),
         "The route parameter name '' 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.");
 }
Пример #12
0
 public void InvalidTemplate_ParameterCannotContainQuestionMark_UnlessAtEnd()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{foor?b}"),
         "The route parameter name 'foor?b' 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.");
 }
Пример #13
0
 public void InvalidTemplate_CannotHaveCatchAllWithNoName()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("foo/{*}"),
         "The route parameter name '' 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.");
 }
Пример #14
0
        /// <summary>
        /// Creates a <see cref="RoutePattern"/> from its string representation.
        /// </summary>
        /// <param name="pattern">The route pattern string to parse.</param>
        /// <returns>The <see cref="RoutePattern"/>.</returns>
        public static RoutePattern Parse(string pattern)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            return(RoutePatternParser.Parse(pattern));
        }
Пример #15
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 + "'.");
 }
Пример #16
0
        /// <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));
        }
Пример #17
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());
        }
Пример #18
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());
        }
Пример #19
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);
        }
Пример #20
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());
        }
Пример #21
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());
        }
Пример #22
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());
        }
Пример #23
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());
        }
Пример #24
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());
        }
Пример #25
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());
        }
Пример #26
0
 public void InvalidTemplate_CatchAllMarkedOptional()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{*b?}"),
         "A catch-all parameter cannot be marked optional.");
 }
Пример #27
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.");
 }
Пример #28
0
 public void InvalidTemplate_CannotStartWithTilde()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("~foo"),
         "The route template cannot start with a '~' character unless followed by a '/'.");
 }
Пример #29
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.");
 }
Пример #30
0
 public void InvalidTemplate_InvalidParameterNameWithOpenBracketThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{a{aa}/{z}"),
         "In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.");
 }