public void GetRouteDataWithConstraintsThatIsNotStringOrCustomConstraint_Throws() { // Arrange var dictionary = new RouteValueDictionary(new { controller = new RouteValueDictionary() }); ExceptionAssert.Throws <InvalidOperationException>( () => RouteConstraintBuilder.BuildConstraints(dictionary), "The constraint entry 'controller' must have a string value or be of a type which implements '" + typeof(IRouteConstraint) + "'."); }
public void GetRouteDataWithConstraintsThatIsAStringCreatesARegex() { // Arrange var dictionary = new RouteValueDictionary(new { controller = "abc" }); var constraintDictionary = RouteConstraintBuilder.BuildConstraints(dictionary); // Assert Assert.Equal(1, constraintDictionary.Count); Assert.Equal("controller", constraintDictionary.First().Key); var constraint = constraintDictionary["controller"]; Assert.IsType <RegexRouteConstraint>(constraint); }
public void GetRouteDataWithConstraintsThatIsCustomConstraint_IsPassThrough() { // Arrange var originalConstraint = new Mock <IRouteConstraint>().Object; var dictionary = new RouteValueDictionary(new { controller = originalConstraint }); var constraintDictionary = RouteConstraintBuilder.BuildConstraints(dictionary); // Assert Assert.Equal(1, constraintDictionary.Count); Assert.Equal("controller", constraintDictionary.First().Key); var constraint = constraintDictionary["controller"]; Assert.Equal(originalConstraint, constraint); }
[InlineData("Abc", " abc", false)] // Matches whole (less one char) public void StringConstraintsMatchingScenarios(string routeValue, string constraintValue, bool shouldMatch) { // Arrange var dictionary = new RouteValueDictionary(new { controller = routeValue }); var constraintDictionary = RouteConstraintBuilder.BuildConstraints( new RouteValueDictionary(new { controller = constraintValue })); var constraint = constraintDictionary["controller"]; Assert.Equal(shouldMatch, constraint.Match( httpContext: new Mock <HttpContext>().Object, route: new Mock <IRouter>().Object, routeKey: "controller", values: dictionary, routeDirection: RouteDirection.IncomingRequest)); }
public TemplateRoute([NotNull] IRouter target, string routeName, string routeTemplate, IDictionary <string, object> defaults, IDictionary <string, object> constraints, IDictionary <string, object> dataTokens, IInlineConstraintResolver inlineConstraintResolver) { _target = target; _routeTemplate = routeTemplate ?? string.Empty; Name = routeName; _defaults = defaults ?? new RouteValueDictionary(); _constraints = RouteConstraintBuilder.BuildConstraints(constraints, _routeTemplate) ?? new Dictionary <string, IRouteConstraint>(); _dataTokens = dataTokens ?? new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); // The parser will throw for invalid routes. _parsedTemplate = TemplateParser.Parse(RouteTemplate, inlineConstraintResolver); UpdateInlineDefaultValuesAndConstraints(); _matcher = new TemplateMatcher(_parsedTemplate); _binder = new TemplateBinder(_parsedTemplate, _defaults); }
public void ConstraintBuilderWithTemplateReturnsNull_OnNullOrEmptyInput(IDictionary <string, object> input) { var result = RouteConstraintBuilder.BuildConstraints(input, "{controller}"); Assert.Null(result); }
public void ConstraintBuilderReturnsNull_OnNullOrEmptyInput(IDictionary <string, object> input) { var result = RouteConstraintBuilder.BuildConstraints(input); Assert.Null(result); }