ExecuteStep_should_run_middleware_chain_completing_in_reverse_order_and_step_when_multiple_middleware() { // Arrange var middleware1 = BuildStepMiddleware(1); var middleware2 = BuildStepMiddleware(2); var middleware3 = BuildStepMiddleware(3); Middleware.AddRange(new[] { middleware1, middleware2, middleware3 }); // Act var result = await Runner.ExecuteStep(Context, Body); // Assert result.Should().Be(DummyResult); A .CallTo(RunMethodFor(Body)) .MustHaveHappenedOnceExactly() .Then(A .CallTo(HandleMethodFor(middleware3)) .MustHaveHappenedOnceExactly()) .Then(A .CallTo(HandleMethodFor(middleware2)) .MustHaveHappenedOnceExactly()) .Then(A .CallTo(HandleMethodFor(middleware1)) .MustHaveHappenedOnceExactly()); }
public async Task RunPostMiddleware_should_call_top_level_error_handler_when_middleware_throws() { // Arrange var middleware = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PostWorkflow, 1); A.CallTo(HandleMethodFor(middleware)).ThrowsAsync(new ApplicationException("Something went wrong")); Middleware.AddRange(new[] { middleware }); // Act await Runner.RunPostMiddleware(Workflow, Definition); // Assert A .CallTo(HandleMethodFor(TopLevelErrorHandler)) .MustHaveHappenedOnceExactly(); }
RunPostMiddleware_should_call_error_handler_on_workflow_def_when_middleware_throws_and_def_has_handler() { // Arrange var middleware = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PostWorkflow, 1); A.CallTo(HandleMethodFor(middleware)).ThrowsAsync(new ApplicationException("Something went wrong")); Middleware.AddRange(new[] { middleware }); Definition.OnPostMiddlewareError = typeof(IDefLevelErrorHandler); // Act await Runner.RunPostMiddleware(Workflow, Definition); // Assert A .CallTo(HandleMethodFor(TopLevelErrorHandler)) .MustNotHaveHappened(); A .CallTo(HandleMethodFor(DefLevelErrorHandler)) .MustHaveHappenedOnceExactly(); }
public void ExecuteStep_should_bubble_up_exceptions_in_middleware() { // Arrange var middleware1 = BuildStepMiddleware(1); var middleware2 = BuildStepMiddleware(2); var middleware3 = BuildStepMiddleware(3); Middleware.AddRange(new[] { middleware1, middleware2, middleware3 }); A .CallTo(HandleMethodFor(middleware2)) .Throws(new ApplicationException("Failed")); // Act Func <Task <ExecutionResult> > action = async() => await Runner.ExecuteStep(Context, Body); // Assert action .ShouldThrow <ApplicationException>() .WithMessage("Failed"); }
public async Task RunExecuteMiddleware_should_only_run_middleware_in_ExecuteWorkflow_phase() { // Arrange var executeMiddleware = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.ExecuteWorkflow, 1); var postMiddleware = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PostWorkflow, 2); var preMiddleware = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 3); Middleware.AddRange(new[] { preMiddleware, postMiddleware, executeMiddleware }); // Act await Runner.RunExecuteMiddleware(Workflow, Definition); // Assert A .CallTo(HandleMethodFor(executeMiddleware)) .MustHaveHappenedOnceExactly(); A.CallTo(HandleMethodFor(preMiddleware)).MustNotHaveHappened(); A.CallTo(HandleMethodFor(postMiddleware)).MustNotHaveHappened(); }
public async Task RunPreMiddleware_should_run_all_middleware_when_multiple_middleware() { // Arrange var middleware1 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 1); var middleware2 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 2); var middleware3 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 3); Middleware.AddRange(new[] { middleware1, middleware2, middleware3 }); // Act await Runner.RunPreMiddleware(Workflow, Definition); // Assert A .CallTo(HandleMethodFor(middleware3)) .MustHaveHappenedOnceExactly() .Then(A .CallTo(HandleMethodFor(middleware2)) .MustHaveHappenedOnceExactly()) .Then(A .CallTo(HandleMethodFor(middleware1)) .MustHaveHappenedOnceExactly()); }
public async Task RunPostMiddleware_should_only_run_middleware_in_PostWorkflow_phase() { // Arrange var postMiddleware1 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PostWorkflow, 1); var postMiddleware2 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PostWorkflow, 2); var preMiddleware1 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 3); var preMiddleware2 = BuildWorkflowMiddleware(WorkflowMiddlewarePhase.PreWorkflow, 4); Middleware.AddRange(new[] { preMiddleware1, postMiddleware1, preMiddleware2, postMiddleware2 }); // Act await Runner.RunPostMiddleware(Workflow, Definition); // Assert A .CallTo(HandleMethodFor(postMiddleware2)) .MustHaveHappenedOnceExactly() .Then(A .CallTo(HandleMethodFor(postMiddleware1)) .MustHaveHappenedOnceExactly()); A.CallTo(HandleMethodFor(preMiddleware1)).MustNotHaveHappened(); A.CallTo(HandleMethodFor(preMiddleware1)).MustNotHaveHappened(); }