Ejemplo n.º 1
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 '}}'.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 3
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.");
 }
Ejemplo n.º 4
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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 7
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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 10
0
        public void ValidTemplate_CanStartWithSlashOrTildeSlash(string routePattern)
        {
            // Arrange & Act
            var pattern = RoutePatternParser.Parse(routePattern);

            // Assert
            Assert.Equal(routePattern, pattern.RawText);
        }
Ejemplo n.º 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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 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.");
 }
Ejemplo n.º 14
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 + "'.");
 }
Ejemplo n.º 15
0
 public static RoutePattern Parse(string pattern)
 {
     try
     {
         return(RoutePatternParser.Parse(pattern));
     }
     catch (RoutePatternException ex)
     {
         throw new ArgumentException(ex.Message, nameof(pattern), ex);
     }
 }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
0
        public void Parse_SingleLiteral()
        {
            // Arrange
            var template = "cool";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("cool", RoutePatternPart.CreateLiteralFromText("cool", "cool"));

            var expected = builder.Build();

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

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Ejemplo n.º 18
0
        public void Parse_OptionalParameter()
        {
            // Arrange
            var template = "{p?}";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p?}", RoutePatternPart.CreateParameterFromText("{p?}", "p", null, RoutePatternParameterKind.Optional));

            var expected = builder.Build();

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

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

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p1}", RoutePatternPart.CreateParameterFromText("{p1}", "p1"));
            builder.AddPathSegmentFromText("{p2}", RoutePatternPart.CreateParameterFromText("{p2}", "p2"));
            builder.AddPathSegmentFromText("{*p3}", RoutePatternPart.CreateParameterFromText("{*p3}", "p3", null, RoutePatternParameterKind.CatchAll));

            var expected = builder.Build();

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

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

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText("{p2}", RoutePatternPart.CreateParameterFromText("{p2}", "p2"));
            builder.AddPathSegmentFromText(".{p3?}",
                                           RoutePatternPart.CreateSeparatorFromText(".", "."),
                                           RoutePatternPart.CreateParameterFromText("{p3?}", "p3", null, RoutePatternParameterKind.Optional));

            var expected = builder.Build();

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

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Ejemplo n.º 21
0
        public void Parse_ComplexSegment_LPL()
        {
            // Arrange
            var template = "cool-{p1}-awesome";

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                template,
                RoutePatternPart.CreateLiteralFromText("cool-", "cool-"),
                RoutePatternPart.CreateParameterFromText("{p1}", "p1"),
                RoutePatternPart.CreateLiteralFromText("-awesome", "-awesome"));

            var expected = builder.Build();

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

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

            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                "{p1}.{p2}",
                RoutePatternPart.CreateParameterFromText("{p1}", "p1"),
                RoutePatternPart.CreateLiteralFromText(".", "."),
                RoutePatternPart.CreateParameterFromText("{p2}", "p2"));

            var expected = builder.Build();

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

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Ejemplo n.º 23
0
        [InlineData(@"{p1:regex(([{{(])\w+)}", @"regex(([{(])\w+)")]                                      // Not balanced {
        public void Parse_RegularExpressions(string template, string constraint)
        {
            // Arrange
            var builder = RoutePatternBuilder.Create(template);

            builder.AddPathSegmentFromText(
                template,
                RoutePatternPart.CreateParameterFromText(
                    template,
                    "p1",
                    null,
                    RoutePatternParameterKind.Standard,
                    ConstraintReference.CreateFromText(constraint, constraint)));

            var expected = builder.Build();

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

            // Assert
            Assert.Equal <RoutePattern>(expected, actual, new RoutePatternEqualityComparer());
        }
Ejemplo n.º 24
0
 public void InvalidTemplate_CatchAllMarkedOptional()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{*b?}"),
         "A catch-all parameter cannot be marked optional.");
 }
Ejemplo n.º 25
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.");
 }
Ejemplo n.º 26
0
 public void InvalidTemplate_CannotStartWithTilde()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("~foo"),
         "The route template cannot start with a '~' character unless followed by a '/'.");
 }
Ejemplo n.º 27
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.");
 }
Ejemplo n.º 28
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.");
 }
Ejemplo n.º 29
0
 public void InvalidTemplate_InvalidParameterNameWithOpenBracketThrows()
 {
     ExceptionAssert.Throws <RoutePatternException>(
         () => RoutePatternParser.Parse("{a}/{a{aa}/{z}"),
         "In a route parameter, '{' and '}' must be escaped with '{{' and '}}'.");
 }
Ejemplo n.º 30
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.");
 }