public async Task CombineAFuncAndAMiddlewareShouldReturnAFuncThatCombinesThemTask() { // Given var calls = new List <string>(); TaskMiddlewareFunc <string> func = async(str, _) => { calls.Add($"Func: {str}"); await Task.CompletedTask; }; TaskMiddleware <string> middleware = async(str, next, ct) => { calls.Add($"Before: {str}"); await next(str, ct); calls.Add($"After: {str}"); }; var context = "Hello"; // When await func.Combine(middleware)(context, default); // Then calls.Should().Equal("Before: Hello", "Func: Hello", "After: Hello"); }
public async Task ComposingMiddlewaresShouldProducesACompositeMiddlewareTask() { // Given var calls = new List <string>(); TaskMiddlewareFunc <string> func = async(str, _) => { calls.Add("Func"); await Task.CompletedTask; }; TaskMiddleware <string> MakeMiddleware(int count) { return(async(str, next, ct) => { calls !.Add("Before" + count); await next(str, ct); calls.Add("After" + count); }); } var middleware1 = MakeMiddleware(1); var middleware2 = MakeMiddleware(2); var middleware3 = MakeMiddleware(3); var context = "Hello world!"; // When var middleware = middleware1.Compose(middleware2, middleware3); await func.Combine(middleware)(context, default); // Then calls.Should().Equal("Before1", "Before2", "Before3", "Func", "After3", "After2", "After1"); }