public async Task RouteAsync_InlineConstraint_OptionalParameter_ConstraintFails() { // Arrange var template = "{controller}/{action}/{id:range(1,20)?}"; var context = CreateRouteContext("/Home/Index/100"); IDictionary<string, object> routeValues = null; var mockTarget = new Mock<IRouter>(MockBehavior.Strict); mockTarget .Setup(s => s.RouteAsync(It.IsAny<RouteContext>())) .Callback<RouteContext>(ctx => { routeValues = ctx.RouteData.Values; ctx.IsHandled = true; }) .Returns(Task.FromResult(true)); var route = new TemplateRoute( mockTarget.Object, template, defaults: null, constraints: null, dataTokens: null, inlineConstraintResolver: _inlineConstraintResolver); Assert.NotEmpty(route.Constraints); Assert.IsType<OptionalRouteConstraint>(route.Constraints["id"]); // Act await route.RouteAsync(context); // Assert Assert.False(context.IsHandled); }
public async Task RouteAsync_InlineConstraint_OptionalParameter_WithInConstructorConstraint() { // Arrange var template = "{controller}/{action}/{id:int?}"; var context = CreateRouteContext("/Home/Index/5"); IDictionary<string, object> routeValues = null; var mockTarget = new Mock<IRouter>(MockBehavior.Strict); mockTarget .Setup(s => s.RouteAsync(It.IsAny<RouteContext>())) .Callback<RouteContext>(ctx => { routeValues = ctx.RouteData.Values; ctx.IsHandled = true; }) .Returns(Task.FromResult(true)); var constraints = new Dictionary<string, object>(); constraints.Add("id", new RangeRouteConstraint(1, 20)); var route = new TemplateRoute( mockTarget.Object, template, defaults: null, constraints: constraints, dataTokens: null, inlineConstraintResolver: _inlineConstraintResolver); Assert.NotEmpty(route.Constraints); Assert.IsType<OptionalRouteConstraint>(route.Constraints["id"]); var innerConstraint = ((OptionalRouteConstraint)route.Constraints["id"]).InnerConstraint; Assert.IsType<CompositeRouteConstraint>(innerConstraint); var compositeConstraint = (CompositeRouteConstraint)innerConstraint; Assert.Equal(compositeConstraint.Constraints.Count<IRouteConstraint>(), 2); Assert.Single(compositeConstraint.Constraints, c => c is IntRouteConstraint); Assert.Single(compositeConstraint.Constraints, c => c is RangeRouteConstraint); // Act await route.RouteAsync(context); // Assert Assert.True(context.IsHandled); Assert.True(routeValues.ContainsKey("id")); Assert.Equal("5", routeValues["id"]); Assert.True(context.RouteData.Values.ContainsKey("id")); Assert.Equal("5", context.RouteData.Values["id"]); }
public async Task RouteAsync_CleansUpMergedRouteData_IfConstraintThrows() { // Arrange var template = "{controller}/{action}/{id:int}"; var context = CreateRouteContext("/Home/Index/5"); var originalRouteDataValues = context.RouteData.Values; originalRouteDataValues.Add("country", "USA"); var originalDataTokens = context.RouteData.DataTokens; originalDataTokens.Add("company", "Contoso"); var mockTarget = new Mock<IRouter>(MockBehavior.Strict); mockTarget .Setup(s => s.RouteAsync(It.IsAny<RouteContext>())) .Callback<RouteContext>(ctx => { ctx.IsHandled = true; }) .Returns(Task.FromResult(true)); var constraint = new Mock<IRouteConstraint>(MockBehavior.Strict); constraint .Setup(c => c.Match( It.IsAny<HttpContext>(), It.IsAny<IRouter>(), It.IsAny<string>(), It.IsAny<IDictionary<string, object>>(), It.IsAny<RouteDirection>())) .Callback(() => { throw new Exception(); }); var route = new TemplateRoute( mockTarget.Object, template, defaults: null, constraints: new RouteValueDictionary(new { action = constraint.Object }), dataTokens: new RouteValueDictionary(new { today = "Friday" }), inlineConstraintResolver: _inlineConstraintResolver); // Act var ex = await Assert.ThrowsAsync<Exception>(() => route.RouteAsync(context)); // Assert Assert.True(context.RouteData.Values.ContainsKey("country")); Assert.Equal("USA", context.RouteData.Values["country"]); Assert.False(context.RouteData.Values.ContainsKey("id")); Assert.Same(originalRouteDataValues, context.RouteData.Values); Assert.Equal("Contoso", context.RouteData.DataTokens["company"]); Assert.False(context.RouteData.DataTokens.ContainsKey("today")); Assert.Same(originalDataTokens, context.RouteData.DataTokens); }
public async Task RouteAsync_InlineConstraint_Regex() { // Arrange var template = @"{controller}/{action}/{ssn:regex(^\d{{3}}-\d{{3}}-\d{{4}}$)}"; var context = CreateRouteContext("/Home/Index/123-456-7890"); IDictionary<string, object> routeValues = null; var mockTarget = new Mock<IRouter>(MockBehavior.Strict); mockTarget .Setup(s => s.RouteAsync(It.IsAny<RouteContext>())) .Callback<RouteContext>(ctx => { routeValues = ctx.RouteData.Values; ctx.IsHandled = true; }) .Returns(Task.FromResult(true)); var route = new TemplateRoute( mockTarget.Object, template, defaults: null, constraints: null, dataTokens: null, inlineConstraintResolver: _inlineConstraintResolver); Assert.NotEmpty(route.Constraints); Assert.IsType<RegexInlineRouteConstraint>(route.Constraints["ssn"]); // Act await route.RouteAsync(context); // Assert Assert.True(context.IsHandled); Assert.True(routeValues.ContainsKey("ssn")); Assert.Equal("123-456-7890", routeValues["ssn"]); Assert.True(context.RouteData.Values.ContainsKey("ssn")); Assert.Equal("123-456-7890", context.RouteData.Values["ssn"]); }
public async Task RouteAsync_CleansUpMergedRouteData_IfRouteDoesNotMatch() { // Arrange var template = "{controller}/{action}/{id:int}"; var context = CreateRouteContext("/Home/Index/5"); var originalRouteDataValues = context.RouteData.Values; originalRouteDataValues.Add("country", "USA"); var originalDataTokens = context.RouteData.DataTokens; originalDataTokens.Add("company", "Contoso"); IDictionary<string, object> routeValues = null; var mockTarget = new Mock<IRouter>(MockBehavior.Strict); mockTarget .Setup(s => s.RouteAsync(It.IsAny<RouteContext>())) .Callback<RouteContext>(ctx => { routeValues = ctx.RouteData.Values; ctx.IsHandled = false; }) .Returns(Task.FromResult(true)); var route = new TemplateRoute( mockTarget.Object, template, defaults: null, constraints: null, dataTokens: new RouteValueDictionary(new { today = "Friday" }), inlineConstraintResolver: _inlineConstraintResolver); // Act await route.RouteAsync(context); // Assert Assert.NotNull(routeValues); Assert.True(routeValues.ContainsKey("country")); Assert.Equal("USA", routeValues["country"]); Assert.True(routeValues.ContainsKey("id")); Assert.Equal("5", routeValues["id"]); Assert.True(context.RouteData.Values.ContainsKey("country")); Assert.Equal("USA", context.RouteData.Values["country"]); Assert.False(context.RouteData.Values.ContainsKey("id")); Assert.Same(originalRouteDataValues, context.RouteData.Values); Assert.Equal("Contoso", context.RouteData.DataTokens["company"]); Assert.False(context.RouteData.DataTokens.ContainsKey("today")); Assert.Same(originalDataTokens, context.RouteData.DataTokens); }