예제 #1
0
        public void Accept_ForNoMatchingCandidates_SelectsTheFirstCandidate(string contentType)
        {
            // Arrange
            var constraint1 = new ConsumesAttribute("application/json", "text/xml");
            var action1     = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List <FilterDescriptor>()
                {
                    new FilterDescriptor(constraint1, FilterScope.Action)
                }
            };

            var constraint2 = new Mock <ITestConsumeConstraint>();
            var action2     = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List <FilterDescriptor>()
                {
                    new FilterDescriptor(constraint2.Object, FilterScope.Action)
                }
            };

            constraint2.Setup(o => o.Accept(It.IsAny <ActionConstraintContext>()))
            .Returns(false);

            var context = new ActionConstraintContext();

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

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

            // Act & Assert
            Assert.True(constraint1.Accept(context));
        }
예제 #2
0
        public void Accept_MatchesForMachingRequestContentType(string contentType)
        {
            // Arrange
            var constraint = new ConsumesAttribute("application/json", "text/xml");
            var action = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint, FilterScope.Action) }
            };

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

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

            // Act & Assert
            Assert.True(constraint.Accept(context));
        }
예제 #3
0
        public void OnResourceExecuting_NullOrEmptyRequestContentType_IsNoOp(string contentType)
        {
            // Arrange
            var httpContext = new DefaultHttpContext();
            httpContext.Request.ContentType = contentType;
            var consumesFilter = new ConsumesAttribute("application/json");
            var actionWithConstraint = new ActionDescriptor()
            {
                ActionConstraints = new List<IActionConstraintMetadata>() { consumesFilter },
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(consumesFilter, FilterScope.Action) }
            };
            var actionContext = new ActionContext(httpContext, new RouteData(), actionWithConstraint);

            var resourceExecutingContext = new ResourceExecutingContext(actionContext, new[] { consumesFilter });

            // Act
            consumesFilter.OnResourceExecuting(resourceExecutingContext);

            // Assert
            Assert.Null(resourceExecutingContext.Result);
        }
예제 #4
0
        public void Accept_MatchesForMachingRequestContentType(string contentType)
        {
            // Arrange
            var constraint = new ConsumesAttribute("application/json", "text/xml");
            var action = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint, FilterScope.Action) }
            };

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

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

            // Act & Assert
            Assert.True(constraint.Accept(context));
        }
예제 #5
0
        public void OnResourceExecuting_ForNoContentTypeMatch_SetsUnsupportedMediaTypeResult(string contentType)
        {
            // Arrange
            var httpContext = new DefaultHttpContext();
            httpContext.Request.ContentType = contentType;
            var consumesFilter = new ConsumesAttribute("application/json");
            var actionWithConstraint = new ActionDescriptor()
            {
                ActionConstraints = new List<IActionConstraintMetadata>() { consumesFilter },
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(consumesFilter, FilterScope.Action) }
            };
            var actionContext = new ActionContext(httpContext, new RouteData(), actionWithConstraint);

            var resourceExecutingContext = new ResourceExecutingContext(actionContext, new[] { consumesFilter });

            // Act
            consumesFilter.OnResourceExecuting(resourceExecutingContext);

            // Assert
            Assert.NotNull(resourceExecutingContext.Result);
            Assert.IsType<UnsupportedMediaTypeResult>(resourceExecutingContext.Result);
        }
예제 #6
0
        public void OnResourceExecuting_ForAContentTypeMatch_IsNoOp(string contentType)
        {
            // Arrange
            var httpContext = new DefaultHttpContext();
            httpContext.Request.ContentType = contentType;
            var consumesFilter = new ConsumesAttribute("application/json", "application/xml");
            var actionWithConstraint = new ActionDescriptor()
            {
                ActionConstraints = new List<IActionConstraintMetadata>() { consumesFilter },
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(consumesFilter, FilterScope.Action) }
            };
            var actionContext = new ActionContext(httpContext, new RouteData(), actionWithConstraint);
            var resourceExecutingContext = new ResourceExecutingContext(actionContext, new[] { consumesFilter });

            // Act
            consumesFilter.OnResourceExecuting(resourceExecutingContext);

            // Assert
            Assert.Null(resourceExecutingContext.Result);
        }
예제 #7
0
        public void Accept_ForNoRequestType_ReturnsTrueForAllConstraints(string contentType)
        {
            // Arrange
            var constraint1 = new ConsumesAttribute("application/json");
            var actionWithConstraint = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint1, FilterScope.Action) }
            };

            var constraint2 = new ConsumesAttribute("text/xml");
            var actionWithConstraint2 = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint2, FilterScope.Action) }
            };

            var actionWithoutConstraint = new ActionDescriptor();

            var context = new ActionConstraintContext();
            context.Candidates = new List<ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(actionWithConstraint, new [] { constraint1 }),
                new ActionSelectorCandidate(actionWithConstraint2, new [] { constraint2 }),
            };

            context.RouteContext = CreateRouteContext(contentType: contentType);

            // Act & Assert
            context.CurrentCandidate = context.Candidates[0];
            Assert.True(constraint1.Accept(context));
            context.CurrentCandidate = context.Candidates[1];
            Assert.True(constraint2.Accept(context));
        }
예제 #8
0
        public void Accept_UnrecognizedMediaType_SelectsTheCandidateWithoutConstraintIfPresent(string contentType)
        {
            // Arrange
            var actionWithoutConstraint = new ActionDescriptor();
            var constraint1 = new ConsumesAttribute("application/json");
            var actionWithConstraint = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint1, FilterScope.Action) }
            };

            var constraint2 = new ConsumesAttribute("text/xml");
            var actionWithConstraint2 = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint2, FilterScope.Action) }
            };

            var context = new ActionConstraintContext();
            context.Candidates = new List<ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(actionWithConstraint, new [] { constraint1 }),
                new ActionSelectorCandidate(actionWithConstraint2, new [] { constraint2 }),
                new ActionSelectorCandidate(actionWithoutConstraint, new List<IActionConstraint>()),
            };

            context.RouteContext = CreateRouteContext(contentType: contentType);

            // Act & Assert
            context.CurrentCandidate = context.Candidates[0];
            Assert.False(constraint1.Accept(context));

            context.CurrentCandidate = context.Candidates[1];
            Assert.False(constraint2.Accept(context));
        }
예제 #9
0
        public void Accept_ForNoMatchingCandidates_SelectsTheFirstCandidate(string contentType)
        {
            // Arrange
            var constraint1 = new ConsumesAttribute("application/json", "text/xml");
            var action1 = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint1, FilterScope.Action) }
            };

            var constraint2 = new Mock<ITestConsumeConstraint>();
            var action2 = new ActionDescriptor()
            {
                FilterDescriptors =
                    new List<FilterDescriptor>() { new FilterDescriptor(constraint2.Object, FilterScope.Action) }
            };

            constraint2.Setup(o => o.Accept(It.IsAny<ActionConstraintContext>()))
                       .Returns(false);

            var context = new ActionConstraintContext();
            context.Candidates = new List<ActionSelectorCandidate>()
            {
                new ActionSelectorCandidate(action1, new [] { constraint1 }),
                new ActionSelectorCandidate(action2, new [] { constraint2.Object }),
            };

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

            // Act & Assert
            Assert.True(constraint1.Accept(context));
        }