public void CreateRoute_IfBuilderContraintsIsNotNullAndConstraintsPropertyIsNull_UsesBuilderConstraints()
        {
            // Arrange
            HttpRouteValueDictionary existingConstraints = new HttpRouteValueDictionary();

            Mock <RouteProviderAttribute> productMock = CreateProductUnderTestMock();

            productMock.SetupGet(p => p.Constraints).Returns((HttpRouteValueDictionary)null);
            IDirectRouteProvider product = productMock.Object;

            HttpRouteEntry expectedEntry = CreateEntry();

            HttpRouteValueDictionary constraints = null;
            DirectRouteBuilder       builder     = null;

            builder = CreateBuilder(() =>
            {
                constraints = builder.Constraints;
                return(null);
            });

            builder.Constraints = existingConstraints;

            DirectRouteProviderContext context = CreateContext((i) => builder);

            // Act
            HttpRouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(existingConstraints, constraints);
        }
        private static HttpRouteEntry CreateRouteEntry(string routePrefix, IDirectRouteProvider provider,
                                                       IEnumerable <ReflectedHttpActionDescriptor> actions, IInlineConstraintResolver constraintResolver)
        {
            Contract.Assert(provider != null);

            DirectRouteProviderContext context = new DirectRouteProviderContext(routePrefix, actions,
                                                                                constraintResolver);
            HttpRouteEntry entry = provider.CreateRoute(context);

            if (entry == null)
            {
                throw Error.InvalidOperation(SRResources.TypeMethodMustNotReturnNull,
                                             typeof(IDirectRouteProvider).Name, "CreateRoute");
            }

            return(entry);
        }
        public void CreateRoute_IfBuilderContraintsIsNotNull_AddsConstraintsFromPropertyWhenBuilding()
        {
            // Arrange
            HttpRouteValueDictionary existingConstraints = new HttpRouteValueDictionary();
            string existingConstraintKey   = "ExistingContraintKey";
            object existingConstraintValue = "ExistingContraint";

            existingConstraints.Add(existingConstraintKey, existingConstraintValue);

            HttpRouteValueDictionary additionalConstraints = new HttpRouteValueDictionary();
            string additionalConstraintKey   = "NewConstraintKey";
            string additionalConstraintValue = "NewConstraint";

            additionalConstraints.Add(additionalConstraintKey, additionalConstraintValue);

            Mock <RouteProviderAttribute> productMock = CreateProductUnderTestMock();

            productMock.SetupGet(p => p.Constraints).Returns(additionalConstraints);
            IDirectRouteProvider product = productMock.Object;

            HttpRouteEntry expectedEntry = CreateEntry();

            HttpRouteValueDictionary constraints = null;
            DirectRouteBuilder       builder     = null;

            builder = CreateBuilder(() =>
            {
                constraints = builder.Constraints;
                return(null);
            });

            builder.Constraints = existingConstraints;

            DirectRouteProviderContext context = CreateContext((i) => builder);

            // Act
            HttpRouteEntry ignore = product.CreateRoute(context);

            // Assert
            Assert.Same(existingConstraints, constraints);
            Assert.Equal(2, constraints.Count);
            Assert.True(constraints.ContainsKey(existingConstraintKey));
            Assert.Same(existingConstraintValue, constraints[existingConstraintKey]);
            Assert.True(constraints.ContainsKey(additionalConstraintKey));
            Assert.Same(additionalConstraintValue, constraints[additionalConstraintKey]);
        }
        public void CreateRoute_DelegatesToContextCreateBuilderBuild()
        {
            // Arrange
            string expectedTemplate      = "RouteTemplate";
            IDirectRouteProvider product = CreateProductUnderTest(expectedTemplate);

            HttpRouteEntry expectedEntry = CreateEntry();

            DirectRouteBuilder         builder = CreateBuilder(() => expectedEntry);
            DirectRouteProviderContext context = CreateContext((template) =>
                                                               template == expectedTemplate ? builder : new DirectRouteBuilder(new ReflectedHttpActionDescriptor[0]));

            // Act
            HttpRouteEntry entry = product.CreateRoute(context);

            // Assert
            Assert.Same(expectedEntry, entry);
        }