Exemplo n.º 1
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Result == null)
            {
                return;
            }

            if (BadActionResultTransformer.IsBadAction(context.Result))
            {
                var transformed = BadActionResultTransformer.Transform(context.Result);

                context.Result = new ObjectResult(transformed)
                {
                    StatusCode = transformed.Errors.First().Status
                };
                return;
            }

            var responseResult    = (ObjectResult)context.Result;
            var relationshipPaths = FindRelationshipPathsToInclude(context.HttpContext.Request);

            if (!configuration.ValidateIncludedRelationshipPaths(relationshipPaths, responseResult.Value))
            {
                context.Result = new HttpStatusCodeResult(400);
                return;
            }

            var jsonApiContext = new Context(
                configuration,
                new Uri(context.HttpContext.Request.GetDisplayUrl()),
                relationshipPaths);

            responseResult.Value = jsonApiTransformer.Transform(responseResult.Value, jsonApiContext);
        }
Exemplo n.º 2
0
        public void GIVEN_AGoodAction_WHEN_Transform_THEN_Exception()
        {
            // Arrange
            var objectResult = new ObjectResult(new Post());

            // Act - Throw
            Assert.Throws <InvalidOperationException>(() => BadActionResultTransformer.Transform(objectResult));

            // Assert
        }
Exemplo n.º 3
0
        public void GIVEN_ABadAction_WHEN_IsBadAction_THEN_True()
        {
            // Arrange
            var objectResult = new HttpNotFoundObjectResult("1234");

            // Act
            var result = BadActionResultTransformer.IsBadAction(objectResult);

            // Assert
            Assert.True(result);
        }
Exemplo n.º 4
0
        public void GIVEN_AGoodAction_WHEN_IsBadAction_THEN_False()
        {
            // Arrange
            var objectResult = new JsonResult(new Post());

            // Act
            var result = BadActionResultTransformer.IsBadAction(objectResult);

            // Assert
            Assert.False(result);
        }
Exemplo n.º 5
0
        public void GIVEN_ABadAction_WHEN_Transform_THEN_CompoundDocumentWithError()
        {
            // Arrange
            var objectResult = new HttpNotFoundObjectResult(123);

            // Act
            var result = BadActionResultTransformer.Transform(objectResult);

            // Assert
            Assert.NotEmpty(result.Errors);
            Assert.Equal(404, result.Errors.First().Status);
            Assert.NotEmpty(result.Errors.First().Title);
        }
Exemplo n.º 6
0
        public void OnResourceExecuted(ResourceExecutedContext context)
        {
            if (context.Result == null || context.Result is NoContentResult)
            {
                return;
            }

            if (BadActionResultTransformer.IsBadAction(context.Result))
            {
                var transformed = BadActionResultTransformer.Transform(context.Result);

                context.Result = new ObjectResult(transformed)
                {
                    StatusCode = transformed.Errors.First().Status
                };
            }
        }