// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if NET45 using System.Collections.Generic; using Microsoft.AspNet.Http; using Moq; using Xunit; namespace Microsoft.AspNet.Routing.Tests { public class ConstraintMatcherTests { [Fact] public void ReturnsTrueOnValidConstraints() { var constraints = new Dictionary { {"a", new PassConstraint()}, {"b", new PassConstraint()} }; var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); Assert.True(RouteConstraintMatcher.Match( constraints: constraints, routeValues: routeValueDictionary, httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } [Fact] public void ConstraintsGetTheRightKey() { var constraints = new Dictionary { {"a", new PassConstraint("a")}, {"b", new PassConstraint("b")} }; var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); Assert.True(RouteConstraintMatcher.Match( constraints: constraints, routeValues: routeValueDictionary, httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } [Fact] public void ReturnsFalseOnInvalidConstraintsThatDontMatch() { var constraints = new Dictionary { {"a", new FailConstraint()}, {"b", new FailConstraint()} }; var routeValueDictionary = new RouteValueDictionary(new { c = "value", d = "value" }); Assert.False(RouteConstraintMatcher.Match( constraints: constraints, routeValues: routeValueDictionary, httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } [Fact] public void ReturnsFalseOnInvalidConstraintsThatMatch() { var constraints = new Dictionary { {"a", new FailConstraint()}, {"b", new FailConstraint()} }; var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); Assert.False(RouteConstraintMatcher.Match( constraints: constraints, routeValues: routeValueDictionary, httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } [Fact] public void ReturnsFalseOnValidAndInvalidConstraintsMixThatMatch() { var constraints = new Dictionary { {"a", new PassConstraint()}, {"b", new FailConstraint()} }; var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" }); Assert.False(RouteConstraintMatcher.Match( constraints: constraints, routeValues: routeValueDictionary, httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } [Fact] public void ReturnsTrueOnNullInput() { Assert.True(RouteConstraintMatcher.Match( constraints: null, routeValues: new RouteValueDictionary(), httpContext: new Mock().Object, route: new Mock().Object, routeDirection: RouteDirection.IncomingRequest)); } private class PassConstraint : IRouteConstraint { private readonly string _expectedKey; public PassConstraint(string expectedKey = null) { _expectedKey = expectedKey; } public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary values, RouteDirection routeDirection) { if (_expectedKey != null) { Assert.Equal(_expectedKey, routeKey); } return true; } } private class FailConstraint : IRouteConstraint { public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary values, RouteDirection routeDirection) { return false; } } } } #endif