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)); }
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); }
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); }
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)); }
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)); }
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); }
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); }
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); }
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)); }
protected override bool CanProcessSub(IRestResponse response) { return(ProcessorStructure.CanProcess(response)); }
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)); }
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)); }