public void WhenErrorCodeThenProcessorReturnsBody()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
                Content     = "{\"StatusCode\": 500, \"Message\": \"Generic error\", \"Details\": \"Details\"}"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

            // Act
            try
            {
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);
            }
            // arrange
            catch (RestException ex)
            {
                Assert.AreEqual(HttpStatusCode.InternalServerError, ex.HttpError.StatusCode);
                Assert.AreEqual("Generic error", ex.HttpError.Message);
                Assert.AreEqual("Details", ex.HttpError.Details);
                throw;
            }

            Assert.IsTrue(processor.CanProcess(response));
        }
示例#2
0
            public void WhenErrorRestThenReturnLeft()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.InternalServerError,
                    ContentType = "application/json",
                    Content     = "{'StatusCode': 400, 'Message': 'Message', 'Details': 'Details'}"
                };

                var processor = new ProcessorStructure <EitherStrict <RestBusinessError, int>, IJsonSerializer>(
                    new EitherRestErrorProcessor <int>().Default()
                    .AddProcessors(new SuccessProcessor <int>().Default())
                    );

                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.IsTrue(res.IsLeft);
                Assert.AreEqual(RestErrorType.ValidationError, res.Left.ErrorType);
                Assert.AreEqual("Message", res.Left.Message);
                Assert.AreEqual("Details", res.Left.Details);
            }
示例#3
0
            public void WhenOkThenReturnRight()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.OK,
                    ContentType = "application/json",
                    Content     = "10"
                };
                var processor = new ProcessorStructure <EitherStrict <RestBusinessError, OptionStrict <int> >, IJsonSerializer>(
                    new EitherRestErrorProcessor <OptionStrict <int> >().Default()
                    .AddProcessors(new OptionAsNotFoundProcessor <int>()
                                   .AddProcessors(new SuccessProcessor <int>().Default()))
                    );

                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.IsTrue(res.IsRight);
                Assert.IsTrue(res.Right.HasValue);
                Assert.AreEqual(10, res.Right.Value);
            }
示例#4
0
 protected override EitherStrict <TLeft, TRight> ProcessSub(IRestResponse response, TSerializer serializer)
 {
     if (ProcessorStructure.CanProcess(response))
     {
         return(ProcessorStructure.Process(response, serializer));
     }
     else
     {
         return(leftProcessorStructure.Process(response, serializer));
     }
 }
        public void WhenErrorIsNotRestErrorThenReturnsAnotherError()
        {
            // Arrange
            string        bodyContents = "{\"@class\": 'Clase', \"MessageError\": \"MessageError\", \"Otro\": \"Otro\"}";
            IRestResponse response     = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
                Content     = bodyContents
            };
            var jsonConverter       = new JsonSerializer();
            var jsonErrorSerializer = new JsonSerializer();

            jsonErrorSerializer.Settings.MissingMemberHandling           = Newtonsoft.Json.MissingMemberHandling.Error;
            jsonErrorSerializer.Settings.ContractResolver.ObjectContract = new RequiredAttributesObjectContract(RequiredLevel.AllowNull);

            var processor = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonErrorSerializer);

            // Act
            try
            {
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);
            }
            // arrange
            catch (RestException ex)
            {
                Assert.AreEqual(HttpStatusCode.InternalServerError, ex.HttpError.StatusCode);
                Assert.AreEqual(bodyContents, ex.HttpError.Details);
                throw;
            }

            Assert.IsTrue(processor.CanProcess(response));
        }
示例#6
0
        public void WhenDifferentCodeThenCantProcessIt()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.InternalServerError,
                ContentType = "application/json",
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <int>, IJsonSerializer>(
                new OptionAsNotFoundProcessor <int>().AddProcessors(new SuccessProcessor <int>().Default())
                );

            // Act - Assert
            Assert.IsFalse(processor.CanProcess(response));
        }
        public void WhenOkCodeThenProcessorCantProcessIt()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.OK,
                ContentType = "application/json",
                Content     = "{\"StatusCode\": 200, \"Message\": \"Generic error\", \"Details\": \"Details\"}"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <int, IJsonSerializer>(
                new RestExceptionProcessor <int>().Default());

            // Act - Assert
            Assert.IsFalse(processor.CanProcess(response));
        }
示例#8
0
        public void WhenOptionAndUnitProcessorWith404ThenReturnsNothing()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.NotFound,
                ContentType = "application/json",
                Content     = "10"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <Unit>, IJsonSerializer>(new OptionAsNotFoundProcessor <Unit>().AddProcessors(new UnitAsSuccessProcessor()));

            // Act
            Assert.IsTrue(processor.CanProcess(response));
            var res = processor.Process(response, jsonConverter);

            // Assert
            Assert.IsFalse(res.HasValue);
        }
            public void When500ThenThrowsException()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.InternalServerError,
                    ContentType = "application/json",
                    Content     = "\"Error\""
                };
                var jsonConverter = new JsonSerializer();
                var processor     = new ProcessorStructure <string, IJsonSerializer>(
                    new RestExceptionProcessor <string>().Default());

                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);
                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                processor.Process(response, jsonConverter);
            }
示例#10
0
        public void When200ThenProcessorReturnsJust()
        {
            // Arrange
            IRestResponse response = new RestResponse()
            {
                StatusCode  = HttpStatusCode.OK,
                ContentType = "application/json",
                Content     = "10"
            };
            var jsonConverter = new JsonSerializer();
            var processor     = new ProcessorStructure <OptionStrict <int>, IJsonSerializer>(
                new OptionAsNotFoundProcessor <int>().AddProcessors(new SuccessProcessor <int>().Default())
                );

            // Act
            Assert.IsTrue(processor.CanProcess(response));
            var res = processor.Process(response, jsonConverter);

            // Assert
            Assert.IsTrue(res.HasValue);
            Assert.AreEqual(10, res.Value);
        }
示例#11
0
            public void When200ThenReturnsData()
            {
                // Arrange
                IRestResponse response = new RestResponse()
                {
                    StatusCode  = HttpStatusCode.OK,
                    ContentType = "application/json",
                    Content     = "\"Hello\""
                };
                var jsonConverter = new JsonSerializer();
                var processor     = new ProcessorStructure <string, IJsonSerializer>(
                    new RestExceptionProcessor <string>().Default());

                ProcessorUtilities.AddNeutralProcessorAtEndsForStructure(processor);
                ProcessorUtilities.SetErrorSerializerForStructure(processor, jsonConverter);

                // Act
                Assert.IsTrue(processor.CanProcess(response));
                var res = processor.Process(response, jsonConverter);

                // Assert
                Assert.AreEqual("Hello", res);
            }
示例#12
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     //Can only process it if the recursive node can process it and it's succesful.
     return(response.StatusCode.IsSuccessful() && ProcessorStructure.CanProcess(response));
 }
示例#13
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     return(ProcessorStructure.CanProcess(response));
 }
示例#14
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     //It can process it only if either the Left or Right processors can
     return(ProcessorStructure.CanProcess(response) || leftProcessorStructure.CanProcess(response));
 }
示例#15
0
 protected override bool CanProcessSub(IRestResponse response)
 {
     return(!response.StatusCode.IsSuccessful() && ProcessorStructure.CanProcess(response));
 }
 protected override bool CanProcessSub(IRestResponse response)
 {
     //It processes the response if it's 404 or the inner processor can
     return(response.StatusCode == HttpStatusCode.NotFound || ProcessorStructure.CanProcess(response));
 }