Exemple #1
0
        public void CombineAMiddlewareAndAFuncShouldReturnAFuncThatCombinesThem()
        {
            // Given
            var calls = new List <string>();

            SyncMiddlewareFunc <string, int> func = str =>
            {
                calls.Add("Func");
                return(str.Length);
            };

            SyncMiddleware <string, int> middleware = (str, next) =>
            {
                calls.Add("Before");
                var result = next(str);
                calls.Add("After");
                return(result);
            };

            var context = "Hello world!";

            // When
            var result = middleware.Combine(func)(context);

            // Then
            result.Should().Be(context.Length);
            calls.Should().Equal("Before", "Func", "After");
        }
Exemple #2
0
        public void ComposingMiddlewaresShouldProducesACompositeMiddleware()
        {
            // Given
            var calls = new List <string>();

            SyncMiddlewareFunc <string, int> func = str =>
            {
                calls.Add("Func");
                return(str.Length);
            };

            SyncMiddleware <string, int> MakeMiddleware(int count)
            {
                return((str, next) =>
                {
                    calls !.Add("Before" + count);
                    var result = next(str);
                    calls.Add("After" + count);
                    return result;
                });
            }

            var middleware1 = MakeMiddleware(1);
            var middleware2 = MakeMiddleware(2);
            var middleware3 = MakeMiddleware(3);

            var context = "Hello world!";

            // When
            var middleware = middleware1.Compose(middleware2, middleware3);
            var result     = func.Combine(middleware)(context);

            // Then
            result.Should().Be(context.Length);
            calls.Should().Equal("Before1", "Before2", "Before3", "Func", "After3", "After2", "After1");
        }
 public static SyncMiddlewareFunc <TContext, TResult> Combine <TContext, TResult>(
     this SyncMiddleware <TContext, TResult> middleware,
     SyncMiddlewareFunc <TContext, TResult> func) =>
 context => middleware(context, func);