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)); }
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)); }
public void ActionConstraint_Accept_TheFirstCandidateReturnsFalse_IfALaterOneMatches() { // 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 <ITestActionConsumeConstraint>(); var action2 = new ActionDescriptor() { FilterDescriptors = new List <FilterDescriptor>() { new FilterDescriptor(constraint2.Object, FilterScope.Action) } }; constraint2.Setup(o => o.Accept(It.IsAny <ActionConstraintContext>())) .Returns(true); 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: "application/custom"); // Act & Assert Assert.False(constraint1.Accept(context)); }
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)); }
public void SetContentTypes_ClearsAndSetsContentTypes() { // Arrange var attribute = new ConsumesAttribute("application/json", "text/json"); var contentTypes = new MediaTypeCollection() { MediaTypeHeaderValue.Parse("application/xml"), MediaTypeHeaderValue.Parse("text/xml"), }; // Act attribute.SetContentTypes(contentTypes); // Assert Assert.Collection( contentTypes.OrderBy(t => t), t => Assert.Equal("application/json", t), t => Assert.Equal("text/json", t)); }
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); }
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)); }
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 }, new List<IValueProviderFactory>()); // Act consumesFilter.OnResourceExecuting(resourceExecutingContext); // Assert Assert.NotNull(resourceExecutingContext.Result); Assert.IsType<UnsupportedMediaTypeResult>(resourceExecutingContext.Result); }