Esempio n. 1
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new WebHostBuilder()
                              .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
                              .ConfigureServices(services =>
            {
                configureServices(services);

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);
                    return(new KestrelServer(_transportFactory, context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }
Esempio n. 2
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new WebHostBuilder()
                              .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString())
                              .ConfigureServices(services =>
            {
                configureServices(services);

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);

                    // Prevent ListenOptions reuse. This is easily done accidentally when trying to debug a test by running it
                    // in a loop, but will cause problems because only the app func from the first loop will ever be invoked.
                    Assert.All(context.ServerOptions.ListenOptions, lo =>
                               Assert.Equal(context.ExpectedConnectionMiddlewareCount, lo._middleware.Count));

                    return(new KestrelServer(_transportFactory, context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }
Esempio n. 3
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new HostBuilder()
                              .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString(CultureInfo.InvariantCulture))
                .Configure(app => { app.Run(_app); });
            })
                              .ConfigureServices(services =>
            {
                configureServices(services);

                // Ensure there is at least one multiplexed connection lister factory if none was added to services.
                if (!services.Any(d => d.ServiceType == typeof(IMultiplexedConnectionListenerFactory)))
                {
                    // Mock multiplexed connection listner is added so Kestrel doesn't error
                    // when a HTTP/3 endpoint is configured.
                    services.AddSingleton <IMultiplexedConnectionListenerFactory>(new MockMultiplexedConnectionListenerFactory());
                }

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);
                    return(new KestrelServerImpl(
                               new IConnectionListenerFactory[] { _transportFactory },
                               sp.GetServices <IMultiplexedConnectionListenerFactory>(),
                               context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }
Esempio n. 4
0
        public TestServer(RequestDelegate app, TestServiceContext context, Action <KestrelServerOptions> configureKestrel, Action <IServiceCollection> configureServices)
        {
            _app              = app;
            Context           = context;
            _memoryPool       = context.MemoryPoolFactory();
            _transportFactory = new InMemoryTransportFactory();
            HttpClientSlim    = new InMemoryHttpClientSlim(this);

            var hostBuilder = new HostBuilder()
                              .ConfigureWebHost(webHostBuilder =>
            {
                webHostBuilder
                .UseSetting(WebHostDefaults.ShutdownTimeoutKey, TestConstants.DefaultTimeout.TotalSeconds.ToString(CultureInfo.InvariantCulture))
                .Configure(app => { app.Run(_app); });
            })
                              .ConfigureServices(services =>
            {
                configureServices(services);

                services.AddSingleton <IStartup>(this);
                services.AddSingleton(context.LoggerFactory);

                services.AddSingleton <IServer>(sp =>
                {
                    context.ServerOptions.ApplicationServices = sp;
                    configureKestrel(context.ServerOptions);
                    return(new KestrelServerImpl(
                               new IConnectionListenerFactory[] { _transportFactory },
                               sp.GetServices <IMultiplexedConnectionListenerFactory>(),
                               context));
                });
            });

            _host = hostBuilder.Build();
            _host.Start();
        }