public async Task Use_AddsMiddlewareUsingInlineSyntax()
        {
            IServiceProvider serviceProvider = new MockServiceProvider();
            OkraAppBuilder   app             = new OkraAppBuilder(serviceProvider);

            var middlewareCallList        = new List <string>();
            var middlewareCallContextList = new List <AppLaunchContext>();

            app.Use((context, next) =>
            {
                middlewareCallList.Add("Inline");
                middlewareCallContextList.Add(context);
                return(next());
            });

            app.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("Next");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            var appContext = new MockAppLaunchContext();
            var pipeline   = app.Build();

            await pipeline(appContext);

            Assert.Equal(new string[] { "Inline", "Next" }, middlewareCallList);
            Assert.Equal(new AppLaunchContext[] { appContext, appContext }, middlewareCallContextList);
        }
        public async Task UseMiddleware_AddsMiddleware()
        {
            IServiceProvider serviceProvider = new MockServiceProvider()
                                               .With <MockMiddleware>(new MockMiddleware());
            OkraAppBuilder app = new OkraAppBuilder(serviceProvider);

            var middlewareCallList        = new List <string>();
            var middlewareCallContextList = new List <AppLaunchContext>();
            var options = new MockMiddlewareOptions(middlewareCallList, middlewareCallContextList);

            app.UseMiddleware <MockMiddleware, MockMiddlewareOptions>(options);

            app.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("Next");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            var appContext = new MockAppLaunchContext();
            var pipeline   = app.Build();

            await pipeline(appContext);

            Assert.Equal(new string[] { "Middleware", "Next" }, middlewareCallList);
            Assert.Equal(new AppLaunchContext[] { appContext, appContext }, middlewareCallContextList);
        }
Exemplo n.º 3
0
        public void Use_ThrowsException_IfFunctionIsNull()
        {
            IServiceProvider serviceProvider = new MockServiceProvider();
            OkraAppBuilder   appBuilder      = new OkraAppBuilder(serviceProvider);

            Assert.Throws <ArgumentNullException>("middleware", () => appBuilder.Use(null));
        }
Exemplo n.º 4
0
        public async Task Build_BuildsTheOverallPipeline()
        {
            var serviceProvider = new MockServiceProvider();
            var appBuilder      = new OkraAppBuilder(serviceProvider);

            var middlewareCallList        = new List <string>();
            var middlewareCallContextList = new List <AppLaunchContext>();

            appBuilder.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("First");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            appBuilder.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("Second");
                    middlewareCallContextList.Add(context);
                    return Task.FromResult <bool>(true);
                });
            });

            appBuilder.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("Third");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            var appContext = new MockAppLaunchContext();
            var pipeline   = appBuilder.Build();

            await pipeline(appContext);

            Assert.Equal(new string[] { "First", "Second" }, middlewareCallList);
            Assert.Equal(new AppLaunchContext[] { appContext, appContext }, middlewareCallContextList);
        }
Exemplo n.º 5
0
        public async Task Build_BuildsAPipelineThatHasNoFinalStep()
        {
            var serviceProvider = new MockServiceProvider();
            var appBuilder      = new OkraAppBuilder(serviceProvider);

            var middlewareCallList        = new List <string>();
            var middlewareCallContextList = new List <AppLaunchContext>();

            appBuilder.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("First");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            appBuilder.Use(next =>
            {
                return(context =>
                {
                    middlewareCallList.Add("Second");
                    middlewareCallContextList.Add(context);
                    return next(context);
                });
            });

            var appContext = new MockAppLaunchContext();
            var pipeline   = appBuilder.Build();

            await pipeline(appContext);

            Assert.Equal(new string[] { "First", "Second" }, middlewareCallList);
            Assert.Equal(new AppLaunchContext[] { appContext, appContext }, middlewareCallContextList);
        }