Example #1
0
        /// <summary>
        /// Creates a route and interprets the contraints from it
        /// </summary>
        /// <param name="value"></param>
        public HttpRoute(string value)
        {
            Value = value;

            Constraints = GetRouteParameters(Value)
                          .SelectMany(x => x.Value.Select(y => RouteConstraint.GetConstraint(x.Key, y)))
                          .Where(x => x != null)
                          .ToList();
        }
Example #2
0
    public void Parse_CreatesDifferentConstraints_ForDifferentKinds()
    {
        // Arrange
        var original = RouteConstraint.Parse("ignore", "ignore", "int");

        // Act
        var another = RouteConstraint.Parse("ignore", "ignore", "guid");

        // Assert
        Assert.NotSame(original, another);
    }
Example #3
0
    public void Parse_CachesCreatedConstraint_ForSameKind()
    {
        // Arrange
        var original = RouteConstraint.Parse("ignore", "ignore", "int");

        // Act
        var another = RouteConstraint.Parse("ignore", "ignore", "int");

        // Assert
        Assert.Same(original, another);
    }
        private static string WriteRouteConstraint(RouteConstraint constraint)
        {
            if (constraint is AlphaConstraint)
            {
                return($@"
			if(string.IsNullOrWhiteSpace({constraint.ParameterName}) || {constraint.ParameterName}.Any(x=>char.IsNumber(x)))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} must only contain characters that are not numbers."");
			}}"            );
            }
            else if (constraint is BoolConstraint)
            {
                return($@"
			if(!bool.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an bool."");
			}}"            );
            }
            else if (constraint is DateTimeConstraint)
            {
                return($@"
			if(!DateTime.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an DateTime."");
			}}"            );
            }
            else if (constraint is DecimalConstraint)
            {
                return($@"
			if(!decimal.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an decimal."");
			}}"            );
            }
            else if (constraint is FloatConstraint)
            {
                return($@"
			if(!float.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an float."");
			}}"            );
            }
            else if (constraint is GuidConstraint)
            {
                return($@"
			if(!Guid.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an Guid."");
			}}"            );
            }
            else if (constraint is IntConstraint)
            {
                return($@"
			if(!int.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an int."");
			}}"            );
            }
            else if (constraint is LengthConstraint)
            {
                var value = constraint.GetConstraintValue();
                if (value.Contains(','))
                {
                    var    split = value.Split(',');
                    string minL  = split[0];
                    string maxL  = split[1];

                    return($@"
			if({constraint.ParameterName}.Length <= {minL} || {constraint.ParameterName}.Length >= {maxL})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a length that is not between {minL} and {maxL}."");
			}}"            );
                }
                else
                {
                    return($@"
			if({constraint.ParameterName}.Length == {value})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a length that is not {value}."");
			}}"            );
                }
            }
            else if (constraint is LongConstraint)
            {
                return($@"
			if(!long.TryParse({constraint.ParameterName}.ToString(),out _))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not parse into an long."");
			}}"            );
            }
            else if (constraint is MaxConstraint)
            {
                var value = constraint.GetConstraintValue();

                return($@"
			if({constraint.ParameterName} >= {value})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a value more than {value}."");
			}}"            );
            }
            else if (constraint is MaxLengthConstraint)
            {
                var value = constraint.GetConstraintValue();

                return($@"
			if({constraint.ParameterName}.Length >= {value})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a length greater than {value}."");
			}}"            );
            }
            else if (constraint is MinConstraint)
            {
                var value = constraint.GetConstraintValue();

                return($@"
			if({constraint.ParameterName} <= {value})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a value less than {value}."");
			}}"            );
            }
            else if (constraint is MinLengthConstraint)
            {
                var value = constraint.GetConstraintValue();

                return($@"
			if({constraint.ParameterName}.Length <= {value})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a length less than {value}."");
			}}"            );
            }
            else if (constraint is RangeConstraint)
            {
                var value = constraint.GetConstraintValue();

                var    split = value.Split(',');
                string minL  = split[0];
                string maxL  = split[1];

                return($@"
			if({constraint.ParameterName} <= {minL} || {constraint.ParameterName} >= {maxL})
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} has a value that is not between {minL} and {maxL}."");
			}}"            );
            }
            else if (constraint is RegexConstraint)
            {
                var value = constraint.GetConstraintValue();

                return($@"
			if(!(new Regex(@""{value}"").IsMatch({constraint.ParameterName})))
			{{
				throw new InvalidRouteException(""Parameter {constraint.ParameterName} does not follow the regex \""{value}\""."");
			}}"            );
            }
            else if (constraint is RequiredConstraint)
            {
                return(null);
            }
            else
            {
                return($@"#error A route constraint of type {constraint.GetType().Name} and text of {constraint.Constraint} is not supported");
            }
        }
Example #5
0
        public void SetProxyFactory(IProxyFactory proxyFactory)
        {
            AlternateType <System.Web.Routing.IRouteConstraint> alternationImplementation = new RouteConstraint(proxyFactory);

            Assert.Equal(proxyFactory, alternationImplementation.ProxyFactory);
        }
Example #6
0
        public void ReturnOneMethod(IProxyFactory proxyFactory)
        {
            AlternateType <System.Web.Routing.IRouteConstraint> alternationImplementation = new RouteConstraint(proxyFactory);

            Assert.Equal(1, alternationImplementation.AllMethods.Count());
        }
        public void SetProxyFactory(IProxyFactory proxyFactory)
        {
            AlternateType<System.Web.Routing.IRouteConstraint> alternationImplementation = new RouteConstraint(proxyFactory);

            Assert.Equal(proxyFactory, alternationImplementation.ProxyFactory);
        }
        public void ReturnOneMethod(IProxyFactory proxyFactory)
        {
            AlternateType<System.Web.Routing.IRouteConstraint> alternationImplementation = new RouteConstraint(proxyFactory);

            Assert.Equal(1, alternationImplementation.AllMethods.Count());
        }