コード例 #1
0
        public async Task UseMiddlewareWithIMiddlewareThrowsIfMiddlewareFactoryCreateReturnsNull()
        {
            var mockServiceProvider = new DummyServiceProvider();
            var builder             = new ApplicationBuilder(mockServiceProvider);

            builder.UseMiddleware(typeof(Middleware));
            var app       = builder.Build();
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                var context = new DefaultHttpContext();
                var sp      = new DummyServiceProvider();
                sp.AddService(typeof(IMiddlewareFactory), new BadMiddlewareFactory());
                context.RequestServices = sp;
                await app(context);
            });

            Assert.Equal(
                Resources.FormatException_UseMiddlewareUnableToCreateMiddleware(
                    typeof(BadMiddlewareFactory),
                    typeof(Middleware)),
                exception.Message);
        }
コード例 #2
0
        public async Task UseMiddlewareWithIMiddlewareWorks()
        {
            var mockServiceProvider = new DummyServiceProvider();
            var builder             = new ApplicationBuilder(mockServiceProvider);

            builder.UseMiddleware(typeof(Middleware));
            var app               = builder.Build();
            var context           = new DefaultHttpContext();
            var sp                = new DummyServiceProvider();
            var middlewareFactory = new BasicMiddlewareFactory();

            sp.AddService(typeof(IMiddlewareFactory), middlewareFactory);
            context.RequestServices = sp;
            await app(context);

            Assert.True(Assert.IsType <bool>(context.Items["before"]));
            Assert.True(Assert.IsType <bool>(context.Items["after"]));
            Assert.NotNull(middlewareFactory.Created);
            Assert.NotNull(middlewareFactory.Released);
            Assert.IsType <Middleware>(middlewareFactory.Created);
            Assert.IsType <Middleware>(middlewareFactory.Released);
            Assert.Same(middlewareFactory.Created, middlewareFactory.Released);
        }