Example #1
0
        public void Accept_RejectsActionMatchWithMissingParameter()
        {
            // Arrange
            var action = new ActionDescriptor();

            action.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
            };

            var constraint = new OverloadActionConstraint();

            var context = new ActionConstraintContext();

            context.Candidates = new List <ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action, new [] { constraint }),
            };

            context.CurrentCandidate = context.Candidates[0];
            context.RouteContext     = CreateRouteContext();

            // Act & Assert
            Assert.False(constraint.Accept(context));
        }
Example #2
0
        public void Accept_AcceptsAction_WithFewerParameters_WhenOtherIsNotOverloaded_FromBodyAttribute()
        {
            // Arrange
            var action1 = new ActionDescriptor();

            action1.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
            };

            var action2 = new ActionDescriptor();

            action2.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromBodyAttribute()).BindingSource,
                    },
                    Name          = "quantity",
                    ParameterType = typeof(int),
                },
            };

            var constraint = new OverloadActionConstraint();

            var context = new ActionConstraintContext();

            context.Candidates = new List <ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action1, new [] { constraint }),
                new ActionSelectorCandidate(action2, new IActionConstraint[] { }),
            };

            context.CurrentCandidate = context.Candidates[0];
            context.RouteContext     = CreateRouteContext("?quantity=5", new { id = 17 });

            // Act & Assert
            Assert.True(constraint.Accept(context));
        }
Example #3
0
        public void Accept_AcceptsActionWithUnsatisfiedOptionalParameter()
        {
            // Arrange
            var action = new ActionDescriptor();

            action.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "quantity",
                    ParameterType = typeof(int),
                },
            };

            var optionalParameters = new HashSet <string>();

            optionalParameters.Add("quantity");

            action.Properties.Add("OptionalParameters", optionalParameters);
            var constraint = new OverloadActionConstraint();

            var context = new ActionConstraintContext();

            context.Candidates = new List <ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action, new [] { constraint }),
            };

            context.CurrentCandidate = context.Candidates[0];
            context.RouteContext     = CreateRouteContext("?store=5", new { id = 17 });

            // Act & Assert
            Assert.True(constraint.Accept(context));
        }
Example #4
0
        public void Accept_AcceptsActionWithSatisfiedParameters_QueryStringOnly()
        {
            // Arrange
            var action = new ActionDescriptor();

            action.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "quantity",
                    ParameterType = typeof(int),
                },
            };

            var constraint = new OverloadActionConstraint();

            var context = new ActionConstraintContext();

            context.Candidates = new List <ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action, new [] { constraint }),
            };

            context.CurrentCandidate = context.Candidates[0];
            context.RouteContext     = CreateRouteContext("?quantity=5&id=7", new { });

            // Act & Assert
            Assert.True(constraint.Accept(context));
        }
Example #5
0
        public void Accept_AcceptsActionsOnSameTier()
        {
            // Arrange
            var action1 = new ActionDescriptor();

            action1.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "quantity",
                    ParameterType = typeof(int),
                },
            };

            var action2 = new ActionDescriptor();

            action2.Parameters = new List <ParameterDescriptor>()
            {
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "id",
                    ParameterType = typeof(int),
                },
                new ParameterDescriptor()
                {
                    BindingInfo = new BindingInfo()
                    {
                        BindingSource = (new FromUriAttribute()).BindingSource,
                    },
                    Name          = "price",
                    ParameterType = typeof(decimal),
                },
            };

            var constraint = new OverloadActionConstraint();

            var context = new ActionConstraintContext();

            context.Candidates = new List <ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action1, new [] { constraint }),
                new ActionSelectorCandidate(action2, new [] { constraint }),
            };

            context.CurrentCandidate = context.Candidates[0];
            context.RouteContext     = CreateRouteContext("?quantity=5&price=5.99", new { id = 17 });

            // Act & Assert
            Assert.True(constraint.Accept(context));

            context.CurrentCandidate = context.Candidates[1];
            Assert.True(constraint.Accept(context));
        }