/// <summary> /// Handle the given <paramref name="response" />, but delegate to the next handler if you can't. /// </summary> /// <param name="response"></param> /// <returns></returns> public Task <StepResult> HandleResponse(IAS4Response response) { response.OriginalRequest.ModifyContext(response.ReceivedStream, response.OriginalRequest.Mode); response.OriginalRequest.ModifyContext(response.ReceivedAS4Message); return(StepResult.SuccessAsync(response.OriginalRequest)); }
/// <summary> /// Handle the given <paramref name="response" />, but delegate to the next handler if you can't. /// </summary> /// <param name="response"></param> /// <returns></returns> public async Task <StepResult> HandleResponse(IAS4Response response) { if (response.ReceivedAS4Message.IsEmpty) { if (response.StatusCode == HttpStatusCode.Accepted) { response.OriginalRequest.ModifyContext(response.ReceivedAS4Message, MessagingContextMode.Send); return(StepResult.Success(response.OriginalRequest).AndStopExecution()); } Logger.Error($"Response with HTTP status: {response.StatusCode}"); if (Logger.IsErrorEnabled) { using (var r = new StreamReader(response.ReceivedStream.UnderlyingStream)) { string content = await r.ReadToEndAsync(); if (!string.IsNullOrEmpty(content)) { Logger.Error("Response with HTTP content: " + content); } } } response.OriginalRequest.ModifyContext(response.ReceivedStream, MessagingContextMode.Send); return(StepResult.Failed(response.OriginalRequest).AndStopExecution()); } return(await _nextHandler.HandleResponse(response)); }
public async Task ThenCannotProceed_IfStatusIsErroneous() { // Arrange IAS4Response as4Response = CreateEmptyAS4ResponseWithStatus(HttpStatusCode.InternalServerError); var handler = new EmptyBodyResponseHandler(CreateAnonymousNextHandler()); // Act StepResult actualResult = await handler.HandleResponse(as4Response); // Assert Assert.False(actualResult.CanProceed); AssertNoChangeInPModes(as4Response, actualResult); }
public async Task ThenHandlerReturnsSameResultedMessage_IfStatusIsAccepted() { // Arrange IAS4Response as4Response = CreateEmptyAS4ResponseWithStatus(HttpStatusCode.Accepted); var handler = new EmptyBodyResponseHandler(CreateAnonymousNextHandler()); // Act StepResult actualResult = await handler.HandleResponse(as4Response); // Assert Assert.False(actualResult.CanProceed); AssertNoChangeInPModes(as4Response, actualResult); }
public async Task HandlerStopsExecution_IfResponseIsWarning() { // Arrange IAS4Response stubAS4Response = await CreatePullRequestWarning(); var sut = new PullRequestResponseHandler(() => null, CreateAnonymousNextHandler()); // Act StepResult actualResult = await sut.HandleResponse(stubAS4Response); // Assert Assert.False(actualResult.CanProceed); AssertNoChangeInPModes(stubAS4Response, actualResult); }
public async Task ThenNextHandlerGetsTheResponse_IfAS4MessageIsReceived() { // Arrange AS4Message as4Message = AS4Message.Create(new Error($"error-{Guid.NewGuid()}", $"user-{Guid.NewGuid()}")); IAS4Response as4Response = CreateAS4ResponseWithResultedMessage(as4Message); var spyHandler = new SpyAS4ResponseHandler(); var handler = new EmptyBodyResponseHandler(spyHandler); // Act await handler.HandleResponse(as4Response); // Assert Assert.True(spyHandler.IsCalled); }
public async Task ThenHandlerReturnsStoppedExecutionStepResult() { // Arrange IAS4Response stubAS4Response = CreateResponseWith( request: new PullRequest($"pr-{Guid.NewGuid()}", "some-mpc"), response: Error.CreatePullRequestWarning($"error-{Guid.NewGuid()}")); var handler = new PullRequestResponseHandler(() => null, CreateAnonymousNextHandler()); // Act StepResult actualResult = await handler.HandleResponse(stubAS4Response); // Assert Assert.False(actualResult.CanProceed); AssertNoChangeInPModes(stubAS4Response, actualResult); }
/// <summary> /// Handle the given <paramref name="response" />, but delegate to the next handler if you can't. /// </summary> /// <param name="response"></param> /// <returns></returns> public async Task <StepResult> HandleResponse(IAS4Response response) { if (response == null) { throw new ArgumentNullException(nameof(response)); } MessagingContext request = response.OriginalRequest; if (request?.AS4Message?.IsPullRequest == true) { bool pullRequestWasPiggyBacked = request.AS4Message.SignalMessages.Any(s => !(s is PullRequest)); if (pullRequestWasPiggyBacked) { using (DatastoreContext ctx = _createContext()) { SendResult result = response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK ? SendResult.Success : SendResult.RetryableFail; var service = new PiggyBackingService(ctx); service.ResetSignalMessagesToBePiggyBacked(request.AS4Message.SignalMessages, result); await ctx.SaveChangesAsync().ConfigureAwait(false); } } bool isEmptyChannelWarning = (response.ReceivedAS4Message?.FirstSignalMessage as Error)?.IsPullRequestWarning == true; if (isEmptyChannelWarning) { request.ModifyContext(response.ReceivedAS4Message, MessagingContextMode.Send); return(StepResult.Success(response.OriginalRequest).AndStopExecution()); } } return(await _nextHandler.HandleResponse(response)); }
private static void AssertNoChangeInPModes(IAS4Response expected, StepResult actual) { Assert.Same(expected.OriginalRequest.SendingPMode, actual.MessagingContext.SendingPMode); Assert.Same(expected.OriginalRequest.ReceivingPMode, actual.MessagingContext.ReceivingPMode); }
/// <summary> /// Handle the given <paramref name="response" />, but delegate to the next handler if you can't. /// </summary> /// <param name="response"></param> /// <returns></returns> public Task <StepResult> HandleResponse(IAS4Response response) { IsCalled = true; return(StepResult.SuccessAsync(new MessagingContext(response.ReceivedStream, MessagingContextMode.Send))); }